<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="347999" data-post-id="347999">
  <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">
								<pre data-code-wrap="elixir"><code class="lang-elixir"># part 1
File.stream!("input.txt")
|&gt; Enum.reduce({[], []}, fn line, {lst1, lst2} -&gt;
  String.split(line)
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; then(fn [n1, n2] -&gt; {[n1 | lst1], [n2 | lst2]} end)
end)
|&gt; then(fn {lst1, lst2} -&gt; Stream.zip_with(Enum.sort(lst1), Enum.sort(lst2), fn a, b -&gt; abs(a-b) end) end)
|&gt; Enum.sum()
|&gt; IO.inspect(label: "part 1")

# part 2
File.stream!("input.txt")
|&gt; Enum.reduce({[], %{}}, fn line, {lst, map} -&gt;
  String.split(line)
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; then(fn [n1, n2] -&gt; {[n1 | lst], Map.update(map, n2, 1, fn n -&gt; n+1 end)} end)
end)
|&gt; then(fn {lst, map} -&gt; Enum.reduce(lst, 0, fn n, acc -&gt; acc + n * Map.get(map, n, 0) end) end)
|&gt; IO.inspect(label: "part 2")
</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="347999" 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-2024-day-1/67786/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-347999" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="347999"
                     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="348000" data-post-id="348000">
  <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>I liked this solution for part 1 because of the <code>unzip / process / zip</code> pattern:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">File.stream!("example.txt")
|&gt; Stream.map(&amp;String.trim/1)
|&gt; Stream.map(&amp;String.split/1)
|&gt; Stream.map(fn [a, b] -&gt; {String.to_integer(a), String.to_integer(b)} end)
|&gt; Enum.unzip()
|&gt; then(fn {l, r} -&gt; [Enum.sort(l), Enum.sort(r)] end)
|&gt; Stream.zip()
|&gt; Stream.map(fn {a, b} -&gt; abs(b-a) end)
|&gt; Enum.sum()
|&gt; IO.inspect()
</code></pre>
<p>Part 2 wasn’t quite as symmetrical:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{left, right} =
  File.stream!("input.txt")
  |&gt; Stream.map(&amp;String.trim/1)
  |&gt; Stream.map(&amp;String.split/1)
  |&gt; Stream.map(fn [a, b] -&gt; {String.to_integer(a), String.to_integer(b)} end)
  |&gt; Enum.unzip()

f_right = Enum.frequencies(right)

left
|&gt; Stream.map(fn l -&gt; {l, Map.get(f_right, l, 0)} end)
|&gt; Stream.map(fn {l, n} -&gt; l * n end)
|&gt; Enum.sum()
|&gt; IO.inspect()
</code></pre>
<p>Technically it would be faster to fuse successive <code>Stream.map</code>s into a single one with a more-complicated function, but writing in this style keeps each line focused on a specific transformation.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Our solutions are quite similar  <img src="https://forum.elixirforum.com/images/emoji/apple/wink.png?v=15" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>
<p>Personally I like to write unit tests and save input as a file in the test folder.<br>
Pretty useful when you want to do a bit of refactor while doing part 2 to ensure you don’t break part 1.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule D1 do
  def p1(file) do
    file
    |&gt; build_lists()
    |&gt; Enum.map(&amp;Enum.sort/1)
    |&gt; Enum.zip_reduce(0, fn [a, b], acc -&gt; acc + abs(b - a) end)
  end

  def p2(file) do
    [l1, l2] = build_lists(file)
    freq = Enum.frequencies(l2)
    Enum.reduce(l1, 0, fn a, acc -&gt; acc + a * Map.get(freq, a, 0) end)
  end

  defp build_lists(file) do
    file
    |&gt; File.stream!(:line)
    |&gt; Enum.reduce([[], []], fn line, [l1, l2] -&gt;
      [a, b] =
        line
        |&gt; String.split()
        |&gt; Enum.map(&amp;String.to_integer/1)

      [[a | l1], [b | l2]]
    end)
  end
end
</code></pre>
<p>unit tests:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule D1Test do
  use ExUnit.Case

  test "p1" do
    assert D1.p1("test/d1.txt") == 2_970_687
  end

  test "p2" do
    assert D1.p2("test/d1.txt") == 23_963_899
  end
end
</code></pre>
<p>It could also be useful when things are getting harder to write a unit test with the given examples.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348004" 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-2024-day-1/67786/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-348004" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348004"
                     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="348016" data-post-id="348016">
  <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>Someone’s getting into the typing mindset before the big release it seems <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> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I wish. I am currently jobless. <img src="https://forum.elixirforum.com/uploads/default/original/2X/6/6c3193d1dd46244da3c8c6f719c9f5e2abdd5ae8.gif?v=15" title=":003:" class="emoji emoji-custom" alt=":003:" loading="lazy" width="20" height="20"></p>
<p>I was referring to being thorough always though.</p>
<p>That being said, I am ashamed to have forgotten about <code>Enum.zip_with</code> and <code>Enum.zip_reduce</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="348017" 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-2024-day-1/67786/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-348017" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348017"
                     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="348018" data-post-id="348018">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was looking for a way to read file from web so this is very handy, also the left right into tuple if something I haven’t worked out in elixir yet, so again useful.</p>
<p>Part 1 you could<br>
!&gt; Enum.reduce(0, fn {a, b}, acc → acc + abs(a - b) end)</p>
<p>and remove the sum step I believe</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348018" 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-2024-day-1/67786/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-348018" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348018"
                     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="348019" data-post-id="348019">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>context: I’m new to Elixir and not a massive user of FP in general outside of LINQ in C#, I’m very interested in the Elixir ecosystem.</p>
<p>What I love about this challenge is that it shows how many ways there are to solve the same problem, in the same language. I hoping to use this AoC to find some idiomatic Elixir (and functional) ways to do things I just haven’t thought of.</p>
<p>I’ve solved Day 1, using Livebook so no fancy modules or anything, but already I’m noticing some patterns. For instance, I see a lot of |&gt; Enum.map |&gt; Enum.sum being used when a |&gt; Enum.reduce would do the step in a single line. Although both are correct, I’m wondering what the mental leap is to merge those map/sum steps to a reduce is. It wasn’t until a colleague said I realised it too. I solved the problem and didn’t go back to use reduce until after.I’m curious as to when reduce becomes ‘normal’ thinking in FP.</p>
<p>My pre ‘reduce’ solution is here [<a href="https://github.com/davewil/aoc-2024/blob/main/day1.livemd" class="inline-onebox" rel="noopener nofollow ugc">aoc-2024/day1.livemd at main · davewil/aoc-2024 · GitHub</a>] very happy for constructive feedback.</p>
<p>My post ‘reduce’ solution generally merges the final map/sum into a reduce accumulate with 0 initial value</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348019" 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-2024-day-1/67786/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-348019" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348019"
                     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="348020" data-post-id="348020">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Signed up just to say thank you for this comment! Didn’t know about the <code>then</code> function. Subsequently have learned about the <code>tap</code> function as well thanks to this article: <a href="https://blixtdev.com/two-useful-elixir-functions-you-may-not-know/" class="inline-onebox" rel="noopener nofollow ugc">Useful Elixir Functions You May Not Know: tap() &amp; then()</a></p>
<p>I guess it’s not that different from <code>|&gt; fn x -&gt; x end.()</code>, but it at least feels a bit nicer.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>input<br>
|&gt; Enum.Map()<br>
|&gt; Enum.Sum()</p>
<p>can usually be replaced with a<br>
input<br>
|&gt; Enum.reduce()</p>
<p>if you want to</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348022" 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-2024-day-1/67786/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-348022" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348022"
                     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="348026" data-post-id="348026">
  <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>By the way, you can also achieve similar memory footprint with stream.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">input
|&gt; Stream.map(...)
|&gt; Enum.sum()
</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="348026" 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-2024-day-1/67786/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-348026" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348026"
                     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/67786/load_more?page=4">Load more posts (15 remaining)</a>
</div></template></turbo-stream>