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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I also took the most direct approach, although I did “refactor” slightly in part to to use reduce to avoid excessive iterations over the list. I wish I had thought of File.stream!</p>
<aside class="onebox githubgist" data-onebox-src="https://gist.github.com/tfwright/1eff92471eb4ab727ec301851e31c230">
  <header class="source">

      <a href="https://gist.github.com/tfwright/1eff92471eb4ab727ec301851e31c230" target="_blank" rel="noopener nofollow ugc">gist.github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://gist.github.com/tfwright/1eff92471eb4ab727ec301851e31c230" target="_blank" rel="noopener nofollow ugc">https://gist.github.com/tfwright/1eff92471eb4ab727ec301851e31c230</a></h4>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My lazy solution<br>
<a href="https://github.com/adolfont/pensandoemelixir/blob/main/adventofcode/2022/day01_2022/day1.exs" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/adolfont/pensandoemelixir/blob/main/adventofcode/2022/day01_2022/day1.exs</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="270024" 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-2022-day-1/52153/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-270024" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270024"
                     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="270025" data-post-id="270025">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>lazy one<br>
<a href="https://github.com/newton-peixoto/advent-of-code/blob/main/2022/livebooks/day-01.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/newton-peixoto/advent-of-code/blob/main/2022/livebooks/day-01.livemd</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="270025" 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-2022-day-1/52153/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-270025" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270025"
                     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="270032" data-post-id="270032">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Shortest and the <strong>fastest</strong> solution I wrote in 15 minutes</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC1 do
  def traverse(input, number \\ 0, current \\ 0, maximum \\ 0) do
    case input do
      "" -&gt;
        max(maximum, number + current)

      "\n\n" &lt;&gt; tail -&gt;
        traverse(tail, 0, 0, max(maximum, number + current))

      "\n" &lt;&gt; tail -&gt;
        traverse(tail, 0, current + number, maximum)

      &lt;&lt;char, tail :: binary&gt;&gt; -&gt;
        number = number * 10 + char - ?0
        traverse(tail, number, current, maximum)
    end
  end
end

IO.inspect AOC1.traverse IO.read(:eof)
</code></pre>
<hr>
<p>Part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC12 do
  def traverse(input, number \\ 0, current \\ 0, maximum \\ {0, 0, 0}) do
    case input do
      "" -&gt;
        {first, second, third} = put_max(number + current, maximum)
        first + second + third

      "\n\n" &lt;&gt; tail -&gt;
        traverse(tail, 0, 0, put_max(number + current, maximum))

      "\n" &lt;&gt; tail -&gt;
        traverse(tail, 0, current + number, maximum)

      &lt;&lt;char, tail :: binary&gt;&gt; -&gt;
        number = number * 10 + char - ?0
        traverse(tail, number, current, maximum)
    end
  end

  defp put_max(new, {_first, second, third}) when new &gt; third, do: {second, third, new}
  defp put_max(new, {_first, second, third}) when new &gt; second, do: {second, new, third}
  defp put_max(new, {first, second, third}) when new &gt; first, do: {new, second, third}
  defp put_max(_, maximum), do: maximum
end

IO.inspect AOC1.traverse IO.read :eof
</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="270032" 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-2022-day-1/52153/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-270032" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270032"
                     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="270034" data-post-id="270034">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This is pretty identical to my <a href="https://github.com/christhekeele/aoc/blob/d592656dcf8b9a39e26bea0554a33b04c1dc9a5f/year/2022/day/1/part/1.ex#L3-L12" rel="noopener nofollow ugc">part 1</a> and <a href="https://github.com/christhekeele/aoc/blob/d592656dcf8b9a39e26bea0554a33b04c1dc9a5f/year/2022/day/1/part/2.ex#L3-L14" rel="noopener nofollow ugc">part 2</a> solutions! Seems like it’s the easiest iterate-function-call-by-function-call pipeline approach to develop, though not parallelized.</p>
<p>TL;DR:</p>
<details>
  <summary>Part 1:</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">input_file
|&gt; File.read!()
|&gt; String.split("\n\n")
|&gt; Enum.map(&amp;String.split(&amp;1, "\n"))
|&gt; Enum.map(fn inputs -&gt;
 inputs
 |&gt; Enum.map(&amp;String.to_integer/1)
 |&gt; Enum.sum
end)
|&gt; Enum.max()
</code></pre>
</details>
<details>
  <summary>Part 2:</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">input_file
|&gt; File.read!()
|&gt; String.split("\n\n")
|&gt; Enum.map(&amp;String.split(&amp;1, "\n"))
|&gt; Enum.map(fn inputs -&gt;
  inputs
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; Enum.sum
end)
|&gt; Enum.sort(:desc)
|&gt; Enum.take(3)
|&gt; Enum.sum()
</code></pre></details> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270034" 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-2022-day-1/52153/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-270034" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270034"
                     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="270035" data-post-id="270035">
  <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>Like others, I used <a href="https://hexdocs.pm/elixir/1.14.2/Stream.html#chunk_while/4" rel="noopener nofollow ugc"><code>Stream.chunk_while/4</code></a></p>
<p><a href="https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d01.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/ed-flanagan/advent-of-code-solutions-elixir/blob/main/lib/advent/y2022/d01.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="270035" 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-2022-day-1/52153/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-270035" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270035"
                     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="270036" data-post-id="270036">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Outside of a hardcoded N with pattern matching tuples.. whats the fastest way to get the top N elements in a list?</p>
<p>Benchee puts this at much faster than <code>Enum.sort(:desc) |&gt; Enum.take(n)</code> (it assumes there are &gt;N elements in the list…):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">fn n, list -&gt;
  topN = for _ &lt;- 1..n, do: 0
  Enum.reduce(list, topN, fn
    el, [min | rest] when el &gt; min -&gt; Enum.sort([el|rest])
    _, topN -&gt; topN
  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="270036" 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-2022-day-1/52153/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-270036" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270036"
                     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="270039" data-post-id="270039">
  <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
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>If you just want to get the top 3 (not top N with N as a variable), then just</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp get_top3([], top1, top2, top3), do: [top1, top2, top3]

defp get_top3([h|t], top1, top2, _top3) when h &gt; top1, do: get_top3(t, h, top1, top2)

defp get_top3([h|t], top1, top2, _top3) when h &gt; top2, do: get_top3(t, top1, h, top2)

defp get_top3([h|t], top1, top2, top3) when h &gt; top3, do: get_top3(t, top1, top2, h)

defp get_top3([_h|t], top1, top2, top3), do: get_top3(t, top1, top2, top3)
</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="270039" 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-2022-day-1/52153/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-270039" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270039"
                     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="270041" data-post-id="270041">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The fastest way is to traverse list and perform binary search in tuple of maximums (if it’s size more than 5), or linear search (for small tuples of maximums). Something like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># All indeces are 1-based, because it is more efficient in BEAM
def put_max(maxs, new) do
  if new &lt;= :erlang.element(1, maxs) do
    maxs
  else
    put_max(maxs, new, 1, tuple_size(maxs))
  end
end

def put_max(maxs, new, index, index) do
  put_truncating(maxs, index, new)  
end

def put_max(maxs, new, left, right) do
  middle = div(left + right, 2)
  case :erlang.element(middle, maxs) do
    ^new -&gt;
      put_truncating(tuple, middle, new)

    less when less &lt; new -&gt;
      put_max(maxs, new, left, middle)
 
    _ -&gt;
      put_max(maxs, new, middle, right)
  end
end

defp put_truncating(tuple, index, value) do
  tuple = :erlang.insert_element(index, tuple,value)
  :erlang.delete_element(1, tuple)
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="270041" 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-2022-day-1/52153/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-270041" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270041"
                     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="270044" data-post-id="270044">
  <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
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>For top N when N is a variable, then we can also use <code>:gb_sets</code> or build a priority queue to do the job.</p> 
	            </div>

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