<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="234168" data-post-id="234168">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="factoryd" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  factoryd
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my day 5 solution. After coding up a physical representation of the graph, I saw how slow it was. And it dawn on me that I don’t need a physical graph. Just the points touched.</p>
<p><a href="https://github.com/trbngr/advent_of_code/blob/main/lib/2021/5.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/trbngr/advent_of_code/blob/main/lib/2021/5.ex</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234168" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-234168" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234168"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="234169" data-post-id="234169">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="qhwa" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qhwa/120/17564_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  qhwa
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my take:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Y2021.Day05 do
  def p1 do
    AOC.Input.stream("2021/day05.txt", &amp;parse_seg/1)
    |&gt; Stream.filter(fn [x1, y1, x2, y2] -&gt; x1 == x2 || y1 == y2 end)
    |&gt; count_overlap()
  end

  def p2 do
    AOC.Input.stream("2021/day05.txt", &amp;parse_seg/1)
    |&gt; count_overlap()
  end

  defp parse_seg(line) do
    Regex.run(~r/(\d+),(\d+) -&gt; (\d+),(\d+)/, line, capture: :all_but_first)
    |&gt; Enum.map(&amp;String.to_integer/1)
  end

  defp count_overlap(segs) do
    segs
    |&gt; Stream.flat_map(fn [x1, y1, x2, y2] -&gt;
      max_step = max(abs(x2 - x1), abs(y2 - y1))
      dx = div(x2 - x1, max_step)
      dy = div(y2 - y1, max_step)

      Stream.iterate({x1, y1}, fn {x, y} -&gt; {x + dx, y + dy} end)
      |&gt; Stream.take(max_step + 1)
      |&gt; Enum.to_list()
    end)
    |&gt; Enum.frequencies()
    |&gt; Enum.count(fn {_, count} -&gt; count &gt; 1 end)
  end
end
</code></pre>
<p><a href="https://github.com/qhwa/AdventOfCode/blob/master/lib/aoc/2021/day05.exs" rel="noopener nofollow ugc">full file</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234169" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-234169" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234169"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="234171" data-post-id="234171">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="wasi0013" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/wasi0013/120/28765_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  wasi0013
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My naive solution: <a href="https://github.com/wasi0013/advent_of_code/blob/master/lib/y2021/day_05.ex" rel="noopener nofollow ugc">y2021/day_05.ex</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234171" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-234171" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234171"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="234176" data-post-id="234176">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="zevv" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zevv/120/25056_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  zevv
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Both part 1 and 2, full program with no other dependencies. pretty happy with the nice pipeline in there!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp sgn(0) do 0 end
  defp sgn(a) do div(abs(a), a) end

  defp draw(x, y, xend, yend, dx, dy, map) do
    map = Map.update(map, {x, y}, 1, &amp;(&amp;1+1))
    case {x, y} do
      {^xend, ^yend} -&gt; map
      _ -&gt; draw(x+dx, y+dy, xend, yend, dx, dy, map)
    end
  end

  defp draw([x1, y1, x2, y2], map) do 
    draw(x1, y1, x2, y2, sgn(x2-x1), sgn(y2-y1), map)  
  end

  def work(filt) do
    File.read!("data/05.txt")
    |&gt; String.split(~r/[^0-9]+/, trim: true)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; Enum.chunk_every(4, 4)
    |&gt; Enum.filter(filt)
    |&gt; Enum.reduce(%{}, &amp;draw/2)
    |&gt; Enum.filter(fn {_, v} -&gt; v &gt;= 2 end)
    |&gt; Enum.count()
  end

  def run() do
    [6311, 19929] = [ work(fn [x1,y1,x2,y2] -&gt; x1==x2 or y1==y2 end), work(&amp;(&amp;1)) ]
  end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234176" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-234176" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234176"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="234182" data-post-id="234182">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Klohto" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Klohto/120/26029_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Klohto
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Wow, today was an absolute breeze (looking at you Day 4…)</p>
<ul>
<li>Learned about pairing Stream.cycle with Enum.zip</li>
<li>Had to switch from Livebook native input (looks like it’s deprecated at <code>edge</code>) to Kino input</li>
<li>Pattern matching makes this easy</li>
<li>Just expanded all coordinates and counted frequencies bigger than 1</li>
</ul>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Day 4

## Deps &amp; preparation

```elixir
Mix.install([
  {:kino, "~&gt; 0.4.0"}
])

input = Kino.Input.textarea("Please paste your input file:")
```

## Input

```elixir
coordinates =
  Kino.Input.read(input)
  |&gt; String.split(["\n", ",", " -&gt; "], trim: true)
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; Enum.chunk_every(4)
```

## Part 1

```elixir
coordinates
|&gt; Enum.reduce(
  [],
  fn
    [x, y, x1, y1], map when y == y1 -&gt;
      Enum.zip(x..x1, Stream.cycle([y])) ++ map

    [x, y, x1, y1], map when x == x1 -&gt;
      Enum.zip(Stream.cycle([x]), y..y1) ++ map

    _, map -&gt;
      map
  end
)
|&gt; Enum.frequencies()
|&gt; Enum.count(fn {_k, v} -&gt; v &gt; 1 end)
```

## Part 2

```elixir
coordinates
|&gt; Enum.reduce(
  [],
  fn
    [x, y, x1, y1], map when y == y1 -&gt;
      Enum.zip(x..x1, Stream.cycle([y])) ++ map

    [x, y, x1, y1], map when x == x1 -&gt;
      Enum.zip(Stream.cycle([x]), y..y1) ++ map

    [x, y, x1, y1], map -&gt;
      Enum.zip(x..x1, y..y1) ++ map
  end
)
|&gt; Enum.frequencies()
|&gt; Enum.count(fn {_k, v} -&gt; v &gt; 1 end)
```
</code></pre>
<p>EDIT: Updated with <a class="mention" href="/u/epilgrim" rel="nofollow">@epilgrim</a> split approach, thanks!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234182" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-234182" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234182"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="234187" data-post-id="234187">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="egze" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/egze/120/13522_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  egze
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution: <a href="https://github.com/egze/aoc/blob/master/lib/aoc/y2021/d5.ex" rel="noopener nofollow ugc">y2021/d5.ex</a></p>
<p>Wanted to use <code>NimbleParsec</code> just for fun <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule VentParser do
  import NimbleParsec

  coordinates =
    integer(min: 1)
    |&gt; ignore(string(","))
    |&gt; integer(min: 1)

  defparsec(:parse, coordinates |&gt; ignore(string(" -&gt; ")) |&gt; concat(coordinates))
end
</code></pre>
<p><a class="mention" href="/u/klohto" rel="nofollow">@Klohto</a> Cool use of <code>Enum.zip</code>, going to borrow it <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234187" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-234187" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234187"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="234189" data-post-id="234189">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="camilleryr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/camilleryr/120/21069_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  camilleryr
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/klohto" rel="nofollow">@Klohto</a> - Ill one up your Stream.cycle / Enum.zip combo with a Stream.cycle / Stream.zip / Enum.take combo that works for horizontal, vertical, and 45deg diagonal lines all at the same time!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  @doc ~S"""
  ## Example

    iex&gt; build_line_segment({{0, 9}, {5, 9}})
    [{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}, {5, 9}]

    iex&gt; build_line_segment({{0, 0}, {2, 2}})
    [{0, 0}, {1, 1}, {2, 2}]
  """
  def build_line_segment({{x1, y1}, {x2, y2}}) do
    xs = x1..x2
    ys = y1..y2

    Stream.cycle(xs)
    |&gt; Stream.zip(Stream.cycle(ys))
    |&gt; Enum.take(max(Range.size(xs), Range.size(ys)))
  end
</code></pre>
<p>Here is the whole solutions</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day5 do
  import Advent2021

  @doc ~S"""
  --- Day 5: Hydrothermal Venture ---
  You come across a field of hydrothermal vents on the ocean floor! These vents
  constantly produce large, opaque clouds, so it would be best to avoid them if
  possible.

  They tend to form in lines; the submarine helpfully produces a list of nearby
  lines of vents (your puzzle input) for you to review. For example:

  #{test_input()}

  Each line of vents is given as a line segment in the format x1,y1 -&gt; x2,y2
  where x1,y1 are the coordinates of one end the line segment and x2,y2 are the
  coordinates of the other end. These line segments include the points at both
  ends. In other words:

  An entry like 1,1 -&gt; 1,3 covers points 1,1, 1,2, and 1,3.
  An entry like 9,7 -&gt; 7,7 covers points 9,7, 8,7, and 7,7.
  For now, only consider horizontal and vertical lines: lines where either x1 = x2 or y1 = y2.

  So, the horizontal and vertical lines from the above list would produce the following diagram:

  .......1..
  ..1....1..
  ..1....1..
  .......1..
  .112111211
  ..........
  ..........
  ..........
  ..........
  222111....

  In this diagram, the top left corner is 0,0 and the bottom right corner is
  9,9. Each position is shown as the number of lines which cover that point or
  . if no line covers that point. The top-left pair of 1s, for example, comes
  from 2,2 -&gt; 2,1; the very bottom row is formed by the overlapping lines 0,9
  -&gt; 5,9 and 0,9 -&gt; 2,9.

  To avoid the most dangerous areas, you need to determine the number of points
  where at least two lines overlap. In the above example, this is anywhere in
  the diagram with a 2 or larger - a total of 5 points.

  Consider only horizontal and vertical lines. At how many points do at least
  two lines overlap?

  ## Example

    iex&gt; part_1(test_input())
    5
  """
  def_solution part_1(stream_input) do
    do_solve(stream_input, &amp;filter_lines/1)
  end

  defp filter_lines({{x, _y1}, {x, _y2}}), do: true
  defp filter_lines({{_x1, y}, {_x2, y}}), do: true
  defp filter_lines(_), do: false

  @doc ~S"""
  --- Part Two ---
  Unfortunately, considering only horizontal and vertical lines doesn't give
  you the full picture; you need to also consider diagonal lines.

  Because of the limits of the hydrothermal vent mapping system, the lines in
  your list will only ever be horizontal, vertical, or a diagonal line at
  exactly 45 degrees. In other words:

  An entry like 1,1 -&gt; 3,3 covers points 1,1, 2,2, and 3,3.
  An entry like 9,7 -&gt; 7,9 covers points 9,7, 8,8, and 7,9.
  Considering all lines from the above example would now produce the following diagram:

  1.1....11.
  .111...2..
  ..2.1.111.
  ...1.2.2..
  .112313211
  ...1.2....
  ..1...1...
  .1.....1..
  1.......1.
  222111....

  You still need to determine the number of points where at least two lines
  overlap. In the above example, this is still anywhere in the diagram with a 2
  or larger - now a total of 12 points.

  Consider all of the lines. At how many points do at least two lines overlap?

  ## Example

    iex&gt; part_2(test_input())
    12
  """

  def_solution part_2(stream_input) do
    do_solve(stream_input)
  end

  defp do_solve(stream_input, filter \\ fn _ -&gt; true end) do
    stream_input
    |&gt; Stream.map(&amp;line_to_points/1)
    |&gt; Stream.filter(&amp;filter.(&amp;1))
    |&gt; Stream.flat_map(&amp;build_line_segment/1)
    |&gt; Enum.frequencies()
    |&gt; Enum.filter(fn {_point, frequency} -&gt; frequency &gt; 1 end)
    |&gt; Enum.count()
  end

  @doc ~S"""
  ## Example

    iex&gt; build_line_segment({{0, 9}, {5, 9}})
    [{0, 9}, {1, 9}, {2, 9}, {3, 9}, {4, 9}, {5, 9}]

    iex&gt; build_line_segment({{0, 0}, {2, 2}})
    [{0, 0}, {1, 1}, {2, 2}]
  """
  def build_line_segment({{x1, y1}, {x2, y2}}) do
    xs = x1..x2
    ys = y1..y2

    Stream.cycle(xs)
    |&gt; Stream.zip(Stream.cycle(ys))
    |&gt; Enum.take(max(Range.size(xs), Range.size(ys)))
  end

  @doc ~S"""
  ## Example

    iex&gt; line_to_points("0,9 -&gt; 5,9")
    {{0, 9}, {5, 9}}
  """
  def line_to_points(line) do
    line
    |&gt; String.split([",", " -&gt; "])
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; then(fn [x1, y1, x2, y2] -&gt; {{x1, y1}, {x2, y2}} end)
  end

  def test_input do
    """
    0,9 -&gt; 5,9
    8,0 -&gt; 0,8
    9,4 -&gt; 3,4
    2,2 -&gt; 2,1
    7,0 -&gt; 7,4
    6,4 -&gt; 2,0
    0,9 -&gt; 2,9
    3,4 -&gt; 1,4
    0,0 -&gt; 8,8
    5,5 -&gt; 8,2
    """
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234189" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-234189" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234189"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="234190" data-post-id="234190">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bengtson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bengtson/120/20947_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bengtson
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Was pretty much asleep when I wrote this solution but liked it when I woke up this morning. Concise but violates Elixir formatting rules.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc.Day05 do
  def part1, do: input() |&gt; Enum.reject(fn [x1, y1, x2, y2] -&gt; x1 != x2 and y1 != y2 end) |&gt; part2()
  def part2(), do: part2(input())
  def part2(input), do: input |&gt; Enum.reduce(%{}, &amp;draw_line/2) |&gt; Enum.filter(fn {_k, v} -&gt; v &gt; 1 end) |&gt; Enum.count()

  def draw_line([x, y1, x, y2], map), do: y1..y2 |&gt; Enum.reduce(map, fn y, map2 -&gt; Map.update(map2, {x,y}, 1, &amp;(&amp;1 + 1)) end)
  def draw_line([x1, y, x2, y], map), do: x1..x2 |&gt; Enum.reduce(map, fn x, map2 -&gt; Map.update(map2, {x,y}, 1, &amp;(&amp;1 + 1)) end)
  def draw_line([x1, y1, x2, y2], map), do: x1..x2 |&gt; Enum.reduce(map, fn x, map2 -&gt; Map.update(map2, {x, div(y2 - y1, x2 - x1) * (x - x1) + y1}, 1, &amp;(&amp;1 + 1)) end)

  def input() do
      "lib/day05/input.txt"
      |&gt; File.read!()
      |&gt; String.split("\n")
      |&gt; Enum.map(fn s -&gt; String.split(s, [",", " -&gt; "]) end)
      |&gt; Enum.map(fn l -&gt; Enum.map(l, fn s -&gt; String.to_integer(s) end) end)
  end
end

</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234190" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-234190" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234190"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="234196" data-post-id="234196">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ramuuns" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ramuuns/120/24984_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ramuuns
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Another highly functional tail-recursive approach from me:<br>
<a href="https://github.com/ramuuns/aoc/blob/master/2021/day-05.exs" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ramuuns/aoc/blob/master/2021/day-05.exs</a></p>
<p>Edit: the following two paragraphs are my previous approach</p>
<p>Where the interesting part is that I’m using an accumulator that consists of two maps and a count, where the first map contains all the points that have at least one line pass through them, the second map, all the points that have at least <em>two</em> points pass through them, and the count is incremented whenever we add an element to the second map. Thus at the end we simply return the count.</p>
<p>This is slightly slower than using just one map and doing an if to see if we need to increment the counter, however the approach has the benefit of code elegance because we can outsource the checking if the map contains a point to the function guards, thus the function bodies become trivial.</p>
<p>Edit: And now the 2x faster approach</p>
<p>So a thought hit me - calling <code>Map.put/3</code> a lot is actually kind of slow - this is because what you do is you copy the old map and create a new one with the one new key added. A list however is fast (if you add an item at the head), because you only create the new element, put the old head as the next pointer, and return the pointer to this thing (as a simplification obviously). So how about I just create a huge list that has all of the points that are part of any of the lines, then sort that list, and look for patterns in this new list that look like <code>[x, x, y | tail ]</code> and count those.</p>
<p>To my slight surprise this was indeed 2x faster for the input data.</p>
<p>EDIT2: of course approach 1 is faster if the data grows <em>much</em> larger</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234196" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-234196" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234196"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="234208" data-post-id="234208">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mruoss" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mruoss/120/34561_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mruoss
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My approach in a nutshell: convert lines to list of points and find points that occur more than once in the (flattened) list of points.</p>
<p>Part 1: ignore diagonals<br>
Part 2: include diagonals</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC2021.Day05.Solver do
  def solve(stream, :first), do: solve(stream, fn _ -&gt; [] end)
  def solve(stream, :second), do: solve(stream, fn [x1, y1, x2, y2] -&gt; Enum.zip(x1..x2, y1..y2) end)

  def solve(stream, get_diagonal) do
    stream
    |&gt; Stream.map(&amp;String.trim/1)
    |&gt; Stream.map(&amp;String.split(&amp;1, [" -&gt; ", ","]))
    |&gt; Stream.map(fn line -&gt; Enum.map(line, &amp;String.to_integer/1) end)
    |&gt; Stream.flat_map(fn
      [x, y1, x, y2] -&gt; Enum.map(y1..y2, fn y -&gt; {x, y} end)
      [x1, y, x2, y] -&gt; Enum.map(x1..x2, fn x -&gt; {x, y} end)
      diagonal_line -&gt; get_diagonal.(diagonal_line)
    end)
    |&gt; Enum.group_by(&amp;Function.identity/1)
    |&gt; Enum.count(fn {_, val} -&gt; Enum.count(val) &gt;= 2 end)
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="234208" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/advent-of-code-2021-day-5/44266/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-234208" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="234208"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/44266/load_more?page=3">Load more posts (5 remaining)</a>
</div></template></turbo-stream>