<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="270045" data-post-id="270045">
  <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>I was going to suggest that but if I remember correctly it does not allow for duplicate values. Elves with equal calories were not explicitly forbidden from being in the top 3 so that could lead to a bad result. You’d have to build the queue including the index from the original input like `{calories, ndx}'. Tuples get sorted by elements in order, so the gb_set would still give you the right top N, you just have to then extract the calories from the tuples.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The solution I initially had (the one that got me the stars) was with <code>:gb_sets</code> but then I realized, what if one of the top 3 have dupes in them? One other option was to use <code>:gb_trees</code> and then use a <code>counter</code> as value that increments when the key exists (Not sure of how it would turn out but seemed okay in my head) but I was too tired so scraped that and did the good old sort and take. Also, performance didn’t seem to matter much between those two. However, performance mattered a little when I parsed for <code>\n\n</code> vs reducer with cons-swapping.</p>
<p>Anyways, here’s the solution: <a href="https://github.com/code-shoily/advent_of_code/blob/master/lib/2022/day_01.ex" class="inline-onebox" rel="noopener nofollow ugc">advent_of_code/lib/2022/day_01.ex at main · code-shoily/advent_of_code · GitHub</a></p>
<p>Also: Nice seeing you again <a class="mention" href="/u/aetherus" rel="nofollow">@Aetherus</a> hope to learn from your solutions this year again. <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="270053" 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/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-270053" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270053"
                     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="270054" data-post-id="270054">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wonder if we can design a heap that only contains 3 elements. And pass all the calorie sums in it and at the end, just sum all heap’s elements up. This could be slightly space efficient, but with the number of data given, shouldn’t improve much in terms of speed. (Although something tells me we are getting a massive version of calorie data soon)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270054" 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/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-270054" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270054"
                     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="270111" data-post-id="270111">
  <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>I’ve built my own priority queue in order to solve LeetCode problems <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule PriorityQueue do
  @moduledoc """
  A stateless priority queue implementation basing on pairing heap.
  """

  @initial_serial -Bitwise.bsl(1, 64)

  defstruct size: 0,
            serial: @initial_serial,
            heap: nil,
            key_fun: nil

  @typep serial :: integer()
  @typep pairing_heap(key, value) :: nil | {{key, serial()}, value, [pairing_heap(key, value)]}
  @opaque t(key, value) :: %__MODULE__{
            size: non_neg_integer,
            serial: serial(),
            heap: pairing_heap(key, value),
            key_fun: (value -&gt; key)
          }

  @doc """
  Create an empty priority queue.
  """
  @spec new((value -&gt; key)) :: t(key, value) when key: any(), value: any()
  def new(key_fun \\ nil), do: %__MODULE__{key_fun: key_fun || (&amp; &amp;1)}

  @doc """
  Get the size of a priority queue.
  """
  @spec size(t(any, any)) :: non_neg_integer()
  def size(%__MODULE__{size: size}), do: size

  @doc """
  Put an item into a priority queue.
  Returns a new priority queue.
  """
  @spec push(t(key, value), value) :: t(key, value) when key: any(), value: any()
  def push(%__MODULE__{size: size, heap: heap, key_fun: key_fun, serial: serial} = pq, value) do
    %{pq | size: size + 1, heap: meld(heap, to_heap(value, key_fun, serial)), serial: serial + 1}
  end

  @doc """
  Get and delete the smallest item in the priority queue.
  If the queue is empty, returns `{:error, :empty}`,
  otherwise returns `{:ok, smallest_item, new_priority_queue}`.
  """
  @spec pop(t(key, value)) :: {:ok, value, t(key, value)} | {:error, :empty}
        when key: any(), value: any()
  def pop(%__MODULE__{size: 0}), do: {:error, :empty}

  def pop(%__MODULE__{size: size, heap: {_key, value, sub_heaps}} = pq) do
    {:ok, value, %{pq | size: size - 1, heap: pair(sub_heaps)}}
  end

  @doc """
  Get the smallest element in the priority queue.
  """
  @spec top(t(key, value)) :: {:ok, value} | {:error, :empty}
        when key: any(), value: any()
  def top(%__MODULE__{size: 0}), do: {:error, :empty}
  def top(%__MODULE__{heap: {_, value, _}}), do: {:ok, value}

  @doc """
  Merge two priority queues. Crashes if the given priority queues have different key functions.
  """
  @spec merge!(t(key, value), t(key, value)) :: t(key, value)
        when key: any(), value: any()
  def merge!(%__MODULE__{key_fun: f} = pq1, %__MODULE__{key_fun: f} = pq2) do
    %__MODULE__{
      key_fun: f,
      serial: max(pq1.serial, pq2.serial),
      size: pq1.size + pq2.size,
      heap: meld(pq1.heap, pq2.heap)
    }
  end

  def merge!(%__MODULE__{}, %__MODULE__{}) do
    raise ArgumentError, "Can't merge priority queues with different key functions."
  end

  defp to_heap(value, key_fun, serial) do
    {{key_fun.(value), serial}, value, []}
  end

  @spec meld(pairing_heap(key, value), pairing_heap(key, value)) :: pairing_heap(key, value)
        when key: any(), value: any()
  defp meld(nil, heap), do: heap
  defp meld(heap, nil), do: heap
  defp meld({k1, v1, sub1}, {k2, _v2, _sub2} = heap2) when k1 &lt; k2, do: {k1, v1, [heap2 | sub1]}
  defp meld(heap1, {k2, v2, sub2}), do: {k2, v2, [heap1 | sub2]}

  @spec pair([pairing_heap(key, value)]) :: pairing_heap(key, value)
        when key: any(), value: any()
  defp pair([]), do: nil
  defp pair([node]), do: node
  defp pair([node1, node2 | rest]), do: meld(meld(node2, node1), pair(rest))
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="270111" 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/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-270111" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270111"
                     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="270137" data-post-id="270137">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My first go at AoC in Elixir:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">    {_ok, data} = File.read("input_day1.txt")
    data
    |&gt; String.split("\n")
    |&gt; Enum.chunk_by(&amp;(String.length(&amp;1) == 0))
    |&gt; Enum.filter(fn el -&gt; case el do
                              [""] -&gt; false
                              _ -&gt; true
                            end
    end)
    |&gt; Enum.map(fn sums -&gt; Enum.map(sums, &amp;String.to_integer/1) end)
    |&gt; Enum.map(&amp;Enum.sum/1)
    |&gt; Enum.sort(&amp;(&amp;1 &gt;= &amp;2))
    |&gt; Enum.take(3)
    |&gt; Enum.sum
</code></pre>
<p>Not very fond of the anonymous function in the <code>Enum.filter</code> line.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270137" 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/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-270137" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270137"
                     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 #25"></div>
  </section>
</div>
    <div class="postbit" id="270169" data-post-id="270169">
  <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>A bit cleaner way of doing it:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Enum.filter(fn
  [""] -&gt; false
  _ -&gt; true
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="270169" 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/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-270169" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270169"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="270170" data-post-id="270170">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>or maybe just <code>Enum.reject(&amp; &amp;1 == [""])</code></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="270170" 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/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-270170" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270170"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="270173" data-post-id="270173">
  <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>(deleted)</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Using streams.</p>
<p>part 1</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">start = System.monotonic_time(:microsecond)

File.stream!("input.txt")
|&gt; Stream.transform([], fn
  "\n", acc -&gt; {[acc], []}
  line, acc -&gt; {[], [(line |&gt; String.trim_trailing() |&gt; String.to_integer()) | acc]}
end)
|&gt; Stream.map(&amp;Enum.sum/1)
|&gt; Enum.max()
|&gt; tap(fn sum -&gt; IO.puts "The maximum is #{sum}" end)

elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"
</code></pre>
<p>part1 with the elf number</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">File.stream!("input.txt")
|&gt; Stream.transform([], fn
  "\n", acc -&gt; {[acc], []}
  line, acc -&gt; {[], [(line |&gt; String.trim_trailing() |&gt; String.to_integer()) | acc]}
end)
|&gt; Stream.map(&amp;Enum.sum/1)
|&gt; Stream.with_index(1)
|&gt; Enum.max_by(fn {v,_i} -&gt; v end)
|&gt; tap(fn {v,i} -&gt; IO.puts "The maximum is #{v} with elf number #{i}" end)

</code></pre>
<p>part 2</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">start = System.monotonic_time(:microsecond)

File.stream!("input.txt")
|&gt; Stream.transform([], fn
  "\n", acc -&gt; {[acc], []}
  line, acc -&gt; {[], [(line |&gt; String.trim_trailing() |&gt; String.to_integer()) | acc]}
end)
|&gt; Stream.map(&amp;Enum.sum/1)
|&gt; Enum.sort(:desc)
|&gt; Enum.take(3)
|&gt; Enum.sum()
|&gt; tap(fn sum -&gt; IO.puts "The maximum is #{sum}" end)

elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"
</code></pre>
<p>part 2 with the elves numbers</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
File.stream!("input.txt")
|&gt; Stream.transform([], fn
  "\n", acc -&gt; {[acc], []}
  line, acc -&gt; {[], [(line |&gt; String.trim_trailing() |&gt; String.to_integer()) | acc]}
end)
|&gt; Stream.map(&amp;Enum.sum/1)
|&gt; Stream.with_index(1)
|&gt; Enum.sort_by(fn {v,_i} -&gt; v end, :desc)
|&gt; Enum.take(3)
|&gt; Enum.reduce({0,[]}, fn {v,i}, {sum,lst} -&gt; {sum+v, [i | lst]} end)
|&gt; tap(fn {sum, lst} -&gt; IO.puts "The maximum is #{sum} with elves number #{inspect(lst)}" 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="270257" 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/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-270257" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270257"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="270471" data-post-id="270471">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m fairly new to Elixir and functional programming, I would love to get feedback on my solution. I have to admit it took me a while to figure out how <em>Enum.chunk_while/4</em> works. I have never used a similar function in another language, even after many years in object-oriented programming.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmodule AdventOfCode2022.Day1 do
  @moduledoc """
  https://adventofcode.com/2022/day/1
  """

  def part_1 do
    elves()
    |&gt; Enum.take(1)
  end

  def part_2 do
    elves()
    |&gt; Enum.take(3)
    |&gt; Enum.sum()
  end

  defp elves do
    "lib/advent_of_code2022/day1-input.txt"
    |&gt; File.stream!() # Read the input file line by line
    |&gt; Enum.map(&amp;String.trim/1) # Remove all leading and trailing whitespaces
    |&gt; Enum.chunk_while([], &amp;chunk_func/2, &amp;after_func/1) # Create a list of nested lists containing the calories of every elf
    |&gt; Enum.map(&amp;Enum.sum/1) # Calculate the sum of calories for every elf
    |&gt; Enum.sort(:desc) # Sort the elves based on their calories total
  end

  defp chunk_func(_separator = "", accumulator), do: { :cont, accumulator, [] }
  defp chunk_func(calories, accumulator), do: { :cont, [ String.to_integer(calories) | accumulator ] }

  defp after_func([]), do: { :cont, [] }
  defp after_func(accumulator), do: { :cont, accumulator, [] }
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="270471" 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/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-270471" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="270471"
                     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 #30"></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=4">Load more posts (3 remaining)</a>
</div></template></turbo-stream>