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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yeah, I feel <code>Stream.unfold</code> is overlooked quite often. It’s a really great api for places where one would use <code>while</code> in other languages. My solution in particular feels quite declarative even, given it deals at least as much with start, trajectory and moving as it does with actually figuring out if there’s a tree at the coordinate and counting them.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="196234" 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-2020-day-3/35948/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-196234" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196234"
                     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="196241" data-post-id="196241">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I also went immediately for <code>Stream.unfold</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Day03 do
  def part1(input) do
    input
    |&gt; String.trim()
    |&gt; String.split("\n")
    |&gt; walk_map(3, 1)
    |&gt; Enum.count(&amp; &amp;1)
  end

  def part2(input) do
    rows =
      input
      |&gt; String.trim()
      |&gt; String.split("\n")

    slope_1 = rows |&gt; walk_map(1, 1) |&gt; Enum.count(&amp; &amp;1)
    slope_2 = rows |&gt; walk_map(3, 1) |&gt; Enum.count(&amp; &amp;1)
    slope_3 = rows |&gt; walk_map(5, 1) |&gt; Enum.count(&amp; &amp;1)
    slope_4 = rows |&gt; walk_map(7, 1) |&gt; Enum.count(&amp; &amp;1)
    slope_5 = rows |&gt; walk_map(1, 2) |&gt; Enum.count(&amp; &amp;1)

    slope_1 * slope_2 * slope_3 * slope_4 * slope_5
  end

  def walk_map(rows, right, down) do
    Stream.unfold({0, 0}, fn {x, y} -&gt;
      case rows |&gt; Enum.at(y) |&gt; get_x_position(x) do
        "." -&gt; {false, {x + right, y + down}}
        "#" -&gt; {true, {x + right, y + down}}
        nil -&gt; nil
      end
    end)
  end

  defp get_x_position(nil, _), do: nil

  defp get_x_position(row, position) do
    if position &gt;= String.length(row) do
      get_x_position(row, rem(position, String.length(row)))
    else
      String.at(row, position)
    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="196241" 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-2020-day-3/35948/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-196241" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196241"
                     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="196242" data-post-id="196242">
  <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>Precomputed the grid as a <code>map</code>, then I also compute the path coordinates and just check the grid if it has trees on these coordinates.</p>
<p><a href="https://github.com/egze/aoc/blob/master/lib/aoc/y2020/d3.ex" rel="noopener nofollow ugc">GitHub link</a></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc.Y2020.D3 do
  use Aoc.Boilerplate,
    transform: fn raw -&gt;
      grid =
        raw
        |&gt; String.split("\n", trim: true)
        |&gt; Enum.with_index()
        |&gt; Enum.flat_map(fn {line, index} -&gt;
          line
          |&gt; String.split("", trim: true)
          |&gt; Enum.with_index()
          |&gt; Enum.map(fn {char, char_index} -&gt;
            {{index, char_index}, char}
          end)
        end)
        |&gt; Enum.into(%{})

      width =
        raw
        |&gt; String.split("\n")
        |&gt; Enum.at(0)
        |&gt; String.split("", trim: true)
        |&gt; Enum.count()

      height =
        raw
        |&gt; String.split("\n", trim: true)
        |&gt; Enum.count()

      %{width: width, height: height, grid: grid}
    end

  @part_1_operations [{3, 1}]
  @part_2_operations [[{1, 1}], [{3, 1}], [{5, 1}], [{7, 1}], [{1, 2}]]

  @doc """
  Receives input in form of `%{width: 31, height: 323, grid: %{{0,0} =&gt; ".", {0,1} =&gt; "#"}, ...}`
  """
  def part1(input \\ processed()) do
    @part_1_operations
    |&gt; Stream.cycle()
    |&gt; Enum.take(input.height)
    |&gt; count_trees(input)
  end

  def part2(input \\ processed()) do
    @part_2_operations
    |&gt; Enum.map(fn ops -&gt;
      ops
      |&gt; Stream.cycle()
      |&gt; Enum.take(input.height)
      |&gt; count_trees(input)
    end)
    |&gt; Enum.reduce(&amp;(&amp;1 * &amp;2))
  end

  defp count_trees(moves, input) do
    moves
    |&gt; Enum.reduce({0, {0, 0}}, fn {move_right, move_down}, {trees, {current_row, current_column}} -&gt;
      row = current_row + move_down
      column = Integer.mod(current_column + move_right, input.width)

      case Map.get(input.grid, {row, column}, ".") do
        "." -&gt; {trees, {row, column}}
        "#" -&gt; {trees + 1, {row, column}}
      end
    end)
    |&gt; elem(0)
  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="196242" 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-2020-day-3/35948/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-196242" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196242"
                     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="196243" data-post-id="196243">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution with streams keeping only 1 line of map in memory at any given time.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Event3 do
  def run do
    part1_ruleset = [{3, 1}]
    part2_ruleset = [{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}]
    IO.puts("Test part1: #{solver("input/event3/test.txt", part1_ruleset)}")
    IO.puts("Puzzle part1: #{solver("input/event3/puzzle.txt", part1_ruleset)}")
    IO.puts("Test part2: #{solver("input/event3/test.txt", part2_ruleset)}")
    IO.puts("Puzzle part2: #{solver("input/event3/puzzle.txt", part2_ruleset)}")
  end

  def solver(path, ruleset) do
    accs = Enum.map(ruleset, &amp;rule_to_acc/1)

    input_stream(path)
    |&gt; Stream.drop(1)
    |&gt; Stream.transform(accs, &amp;step_all/2)
    |&gt; Stream.take(-length(ruleset))
    |&gt; Stream.flat_map(&amp; &amp;1)
    |&gt; Enum.reduce(&amp;(&amp;1 * &amp;2))
  end

  def input_stream(path), do: path |&gt; File.stream!() |&gt; Stream.map(&amp;parse_input/1)

  def parse_input(input), do: String.trim(input) |&gt; String.graphemes() |&gt; Enum.map(&amp;(&amp;1 == "#"))

  def step_all(input, acc), do: Enum.map(acc, &amp;step(input, &amp;1)) |&gt; Enum.unzip()

  def step(input, {count, index, step, step_down, step_down}) do
    width = length(input)
    count = count + ((Enum.at(input, index) &amp;&amp; 1) || 0)
    {[count], {count, rem(index + step, width), step, step_down, 1}}
  end

  def step(_input, {count, index, step, step_down, down_counter}),
    do: {[count], {count, index, step, step_down, down_counter + 1}}

  def rule_to_acc({right, down}), do: {0, right, right, down, 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="196243" data-batch-url="/posts/batch_likers">
                        3
                      </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-2020-day-3/35948/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-196243" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196243"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="196257" data-post-id="196257">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule TreeMap do
  defstruct ~w[map max_x max_y]a

  def new(txt) when is_binary(txt) do
    %__MODULE__{}
    |&gt; parse(txt)
  end

  def is_tree?(%__MODULE__{map: map, max_x: max_x, max_y: max_y} = _map, {x, y})
      when y &lt;= max_y do
    map[y][rem(x, max_x + 1)] == "#"
  end

  def in_bounds?(%__MODULE__{max_y: max_y} = _map, {_x, y} = _coords) when y &lt;= max_y, do: true

  def in_bounds?(%__MODULE__{} = _map, _coords), do: false

  defp parse(result, txt) do
    {map, max_y, max_x} =
      txt
      |&gt; String.split()
      |&gt; Enum.map(&amp;String.trim/1)
      |&gt; Enum.reduce({%{}, 0, 0}, fn line, {result, y, _max_x} = _acc -&gt;
        line_as_map =
          0..String.length(line)
          |&gt; Enum.zip(String.graphemes(line))
          |&gt; Enum.into(%{})

        {Map.put(result, y, line_as_map), y + 1, String.length(line) - 1}
      end)

    %{result | map: map, max_y: max_y - 1, max_x: max_x}
  end
end

map =
  File.read!("day3.txt")
  |&gt; TreeMap.new()

slope1 = fn {x, y} -&gt;
  {x + 1, y + 1}
end

slope2 = fn {x, y} -&gt;
  {x + 3, y + 1}
end

slope3 = fn {x, y} -&gt;
  {x + 5, y + 1}
end

slope4 = fn {x, y} -&gt;
  {x + 7, y + 1}
end

slope5 = fn {x, y} -&gt;
  {x + 1, y + 2}
end

[slope1, slope2, slope3, slope4, slope5]
|&gt; Enum.map(fn fun -&gt;
  Stream.iterate({0, 0}, fun)
  |&gt; Enum.take_while(&amp;TreeMap.in_bounds?(map, &amp;1))
  |&gt; Enum.map(fn coords -&gt;
    (TreeMap.is_tree?(map, coords) &amp;&amp; 1) || 0
  end)
  |&gt; Enum.sum()
end)
|&gt; Enum.reduce(1, &amp;*/2)
|&gt; IO.puts()
</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="196257" 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-2020-day-3/35948/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-196257" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196257"
                     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="196262" data-post-id="196262">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><code>Stream.transform</code> has a bit of a learning curve, but you can make it do basically <em>anything</em> that involves “look at each element of this list, plus some information from <em>previous</em> iterations, and return some results and information for the next iteration”.</p>
<p><a href="https://github.com/al2o3cr/advent-of-code-2020/blob/main/day3/part1.exs" class="onebox" target="_blank" rel="noopener nofollow">https://github.com/al2o3cr/advent-of-code-2020/blob/main/day3/part1.exs</a></p>
<p>This solution transforms the file (one line at a time) into a stream of <code>{"..#", row#, col#}</code> tuples representing the path, then counts the ones that have a tree at the correct column.</p>
<p>The day2 version uses a neat property of <code>transform</code> - returning <code>[]</code> works like it does in <code>flat_map</code> and produces no output, so <em>skipping</em> rows (for the “down 2 over 1”) case is easy.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="196262" 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-2020-day-3/35948/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-196262" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196262"
                     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="196264" data-post-id="196264">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="dominicletz" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dominicletz/120/27657_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  dominicletz
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Elixir Desktop</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think my solution is similar to that of <a class="mention" href="/u/lostkobrakai" rel="nofollow">@LostKobrakai</a>  <a class="mention" href="/u/egze" rel="nofollow">@egze</a>, using <code>rem(position, length(line))</code> to map the large position into the small map.</p>
<p>The two novelties I can offer are:</p>
<ol>
<li>Used elixir scripts for everything (no modules/functions)</li>
<li>It’s a one-pass solution going through all lines only once</li>
</ol>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir
require Integer

map = File.read!("3.csv")
|&gt; String.split("\n", trim: true)
|&gt; Enum.map(fn line -&gt;
  String.to_charlist(line)
    |&gt; Enum.map(fn char -&gt; char == ?# end)
end)
|&gt; Enum.reduce(List.duplicate({0, 0}, 5), fn trees, slopes -&gt;
  Enum.with_index(slopes)
  |&gt; Enum.map(fn {{pos, count}, slope} -&gt;
    nextpos = case slope do
      0 -&gt; pos+1
      1 -&gt; pos+3
      2 -&gt; pos+5
      3 -&gt; pos+7
      4 -&gt; pos+0.5
    end
    if trunc(pos) == pos and Enum.at(trees, rem(trunc(pos), length(trees))) do
      {nextpos, count + 1}
    else
      {nextpos, count}
    end
  end)
end)

result = Enum.map(map, fn {_pos, count} -&gt; count end)
  |&gt; Enum.reduce(1, fn count, product -&gt; count * product end)

:io.format("~p~n", [result])
</code></pre>
<p><a href="https://github.com/dominicletz/advent_of_code_2020_elixir/blob/main/3b.ex" rel="noopener nofollow ugc">Git Repo</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="196264" 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-2020-day-3/35948/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-196264" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196264"
                     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="196266" data-post-id="196266">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I clearly need to spend more time with the <code>Stream</code> module.  I picked an easy way to do it:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day03.Forest do
  defstruct forest: [], width: 0, height: 0
end

defmodule Day03 do
  alias Day03.Forest

  def readinput() do
    input =
      File.stream!("3.input.txt")
      |&gt; Enum.map(fn line -&gt; String.trim(line) |&gt; String.graphemes() end)

    %Forest{forest: input, height: length(input), width: length(Enum.at(input, 0))}
  end

  def part1(forest \\ readinput()) do
    move(forest, 3, 1, 0, 0, 0)
  end

  def part2(forest \\ readinput()) do
    [{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}]
    |&gt; Enum.map(fn {right, down} -&gt;
      move(forest, right, down, 0, 0, 0)
    end)
    |&gt; Enum.reduce(1, &amp;*/2)
  end

  def move(forest, right, down, x, y, numtrees) do
    newx = rem(x + right, forest.width)
    newy = y + down

    if newy &gt;= forest.height do
      numtrees + under(forest, x, y)
    else
      move(forest, right, down, newx, newy, numtrees + under(forest, x, y))
    end
  end

  def under(forest, x, y) do
    if Enum.at(forest.forest, y) |&gt; Enum.at(x) == "#", do: 1, else: 0
  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="196266" 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-2020-day-3/35948/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-196266" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196266"
                     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="196321" data-post-id="196321">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><strong>O(mn)</strong> solution</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Day3b do
    @doc """
      right_down part 1 -&gt; [{3, 1}]
      right_down part 2 -&gt; [{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}]
    """
    def start(right_down \\ [{3, 1}], file \\ "/tmp/input.txt"), do:
      File.read!(file) |&gt; String.split("\n") |&gt; process_paths(right_down)

    defp process_paths(path, right_down), do:
      Enum.reduce(right_down, 1, fn {right, down}, acc -&gt;
          path |&gt; find_bottom(right, down - 1) |&gt; (fn trees -&gt; acc * trees end).()
      end)

    def find_bottom([h|t], right, down), do: find_bottom(t, byte_size(h), right, right, down, down, 0)
    def find_bottom(lst, _, _, _, _, _, acc) when lst in [[], [""]], do: acc
    def find_bottom([h|t], line_length, position, right, 0, down, acc), do:
      find_bottom(t, line_length, position + right, right, down, down, acc + is_tree(binary_part(h, rem(position, line_length), 1)))
    def find_bottom([h|t], line_length, position, right, skip, down, acc), do:
      find_bottom(t, line_length, position, right, skip - 1, down, acc)

    def is_tree("#"), do: 1
    def is_tree(_), do: 0

  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="196321" 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-2020-day-3/35948/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-196321" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196321"
                     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="196322" data-post-id="196322">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Aetherus" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Aetherus/120/17203_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Aetherus
                      <span class="op-star" title="Thread Starter">
                        <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                      </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p><code>Stream.transform</code> has a bit of a learning curve, but you can make it do basically <em>anything</em> that involves “look at each element of this list, plus some information from <em>previous</em> iterations, and return some results and information for the next iteration”.</p>
</blockquote>
<p>Yay! I learned it in Day 1 <img src="https://forum.elixirforum.com/images/emoji/apple/grin.png?v=15" title=":grin:" class="emoji" alt=":grin:" loading="lazy" width="20" height="20"></p>
<p>I used it to create a stream that lazily yields k-element combinations of a given list, just like Ruby’s <code>Array#combination</code> without a block.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="196322" 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-2020-day-3/35948/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-196322" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196322"
                     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/35948/load_more?page=3">Load more posts</a>
</div></template></turbo-stream>