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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Wow, some really interesting solutions here. Mine is a fairly basic recursive approach that relies on processing the input into a list of strings. It is brittle because if the strings are of unequal length it would fail. I also hardcoded the string length in the <code>rem/2</code> call, which I could have avoided by introducing another module attribute like <code>@line_length</code> or something.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day3 do
  @input File.stream!("lib/input") |&gt; Enum.into([])
  @finish Kernel.length(@input)

  def progress(_, y, _, _, trees) when y &gt;= @finish do
    trees
  end

  def progress(x, y, run, rise, trees) do
    case @input
         |&gt; Enum.at(y)
         |&gt; String.at(rem(x, 31)) do
      "#" -&gt; progress(x + run, y + rise, run, rise, trees + 1)
      _ -&gt; progress(x + run, y + rise, run, rise, trees)
    end
  end
end

(Day3.progress(0, 0, 1, 1, 0) * Day3.progress(0, 0, 3, 1, 0) * Day3.progress(0, 0, 5, 1, 0) *
   Day3.progress(0, 0, 7, 1, 0) * Day3.progress(0, 0, 1, 2, 0))
|&gt; IO.inspect()

</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="196326" 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/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-196326" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196326"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="196464" data-post-id="196464">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>And here my Erlang solution, part 2 with skipping down gave me some troubles first too</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">-module(day3).
-export([run/0]).

run()-&gt;
    Lines = load_file("day3input.txt"),
    {part1(Lines), part2(Lines)}.

% Part 1 %
part1(Lines)-&gt;
    count_trees(Lines, 0, 0).

count_trees([H | T], Pos, Count)-&gt;
    case string:slice(H, Pos, 1) of
        "." -&gt; count_trees(T, (Pos + 3) rem length(H), Count);
        "#" -&gt; count_trees(T, (Pos + 3) rem length(H), Count + 1)
    end;
count_trees([], _, Count) -&gt;
    Count.

% Part 2 %
part2(Lines)-&gt;
    C1 = count_trees(Lines, 0, 0, 1, 1),
    C2 = count_trees(Lines, 0, 0, 3, 1),
    C3 = count_trees(Lines, 0, 0, 5, 1),
    C4 = count_trees(Lines, 0, 0, 7, 1),
    C5 = count_trees(Lines, 0, 0, 1, 2),
    {C1, C2, C3, C4, C5, C1*C2*C3*C4*C5}.

count_trees([H | T], Pos, Count, Right, Down)-&gt;
    case string:slice(H, Pos, 1) of
        "." -&gt; count_trees(nthtail(Down-1, T), (Pos + Right) rem length(H), Count, Right, Down);
        "#" -&gt; count_trees(nthtail(Down-1, T), (Pos + Right) rem length(H), Count + 1, Right, Down)
    end;
count_trees([], _, Count,_,_) -&gt;
    Count.

nthtail(_, [])-&gt;[];
nthtail(N, L)-&gt; lists:nthtail(N, L).

% Helper %
load_file(Filename)-&gt;
    {ok, Binary} = file:read_file(Filename),
    StringContent = unicode:characters_to_list(Binary),
    [ Line || Line &lt;- string:tokens(StringContent, "\n")].
</code></pre>
<p>Even it was fun, I’m not shure whether I’ll progress, as my time is quite limited atm.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="196464" 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/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-196464" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196464"
                     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 #22"></div>
  </section>
</div>
    <div class="postbit" id="196472" data-post-id="196472">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There’s plenty of overlap in my solution, but didn’t see the exact one, so figured I’d share. (Brand new to Elixir, Advent of code has been a great way to practice.)</p>
<ol>
<li>Assume input is an enumerable of strings (e.g. file stream)</li>
<li>Calculates the width (assumes all rows are uniform)</li>
<li>Stream every <code>down</code>’th row</li>
<li>Stream an index, which represents the number of times you’ve moved right (i.e. each row visited)</li>
<li>Count the number of times the String at <code>rem(right * idx, with)</code> is <code>#</code></li>
</ol>
<p>Idea was to stream the file and only aggregate at the count step, as to not use intermediate lists</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2020.D03 do
  @spec part_one(rows :: Enumerable.t(String.t())) :: integer
  def part_one(rows) do
    count_trees(rows, 3, 1)
  end

  @spec part_two(rows :: Enumerable.t(String.t()), strategies :: Enumerable.t({integer, integer})) ::
          integer
  def part_two(rows, strategies) do
    Enum.reduce(strategies, 1, fn {right, down}, product -&gt;
      product * count_trees(rows, right, down)
    end)
  end

  defp count_trees(rows, right, down) do
    # Assumes all rows are uniform in width
    width = rows |&gt; Enum.at(0) |&gt; String.length()

    rows
    |&gt; Stream.take_every(down)
    |&gt; Stream.with_index()
    |&gt; Enum.count(fn {row, idx} -&gt;
      "#" == String.at(row, rem(right * idx, width))
    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="196472" 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/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-196472" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="196472"
                     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 #23"></div>
  </section>
</div>
    <div class="postbit" id="197760" data-post-id="197760">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a href="https://gitlab.com/NobbZ/aoc_ex/-/blob/master/lib/y2020/d03.ex" class="onebox" target="_blank" rel="noopener nofollow">https://gitlab.com/NobbZ/aoc_ex/-/blob/master/lib/y2020/d03.ex</a></p>
<p>Better late than never!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="197760" 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/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-197760" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197760"
                     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 #24"></div>
  </section>
</div>
    <div class="postbit" id="197762" data-post-id="197762">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Part 1: Basic modulo wrap</p>
<p>Part 2: In the Functional style:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">part_2 =
  Enum.reduce([1, 3, 5, 7], 1, &amp;(&amp;2 * travel(input, &amp;1)))
  |&gt; (fn x -&gt; x * travel2(input) end).() 
</code></pre>
<p>where ‘travel’ traverses the map with slope -1 / n, and ‘travel2’ uses slope -2.</p>
<p><a href="https://github.com/APB9785/AoC-2020-elixir/blob/main/day_03.ex" rel="noopener nofollow ugc">Full solution @ Github</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="197762" 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/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-197762" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="197762"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>