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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>now all my advent of code doctests completes in 62s, async test mode. Which is a lot .. but i might not bother</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You will get better performance if you stick with integers.<br>
If you want to switch to strings for number concatenation, there are simple ways to do it with integers <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="348975" 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-2024-day-7/67938/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-348975" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348975"
                     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 #42"></div>
  </section>
</div>
    <div class="postbit" id="348976" data-post-id="348976">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>side note btw .. in my mix project</p>
<p><code>mix setup_day 2024 8</code></p>
<p>lazy devs …</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Ah i didnt mean strings for numbers. I meant the operators. Because then its easier to short curcuit all operations. My initial version was &amp;+/2 etc</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>A day late and nearly past the next day, but I’m here. My original attempts had a bug in concatenation that had me in the weeds. After looking through this thread, I landed on the following. Still an interesting thought exercise to figure out why my old implementation was so out of whack.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

defmodule Day7.Part2 do
  defp parse(str) do
    str
    |&gt; String.trim()
    |&gt; String.split([": ", " "], trim: true)
    |&gt; Enum.map(&amp;String.to_integer/1)
  end

  def concat(a, b) when b &lt; 10, do: a * 10 + b
  def concat(a, b), do: concat(a, div(b, 10)) * 10 + rem(b, 10)

  def to_count([target | argv]),
    do: to_count([&amp;Kernel.+/2, &amp;Kernel.*/2, &amp;concat(&amp;1, &amp;2)], target, argv)

  def to_count(_, target, [a]) do
    if target == a, do: target, else: nil
  end

  def to_count(ops, target, [a, b | rest]) do
    Enum.find_value(ops, fn op -&gt;
      to_count(ops, target, [op.(a, b) | rest])
    end)
  end

  def solve() do
    File.stream!("07/input.txt")
    |&gt; Stream.map(&amp;parse/1)
    |&gt; Task.async_stream(&amp;to_count/1)
    |&gt; Stream.map(fn
      {:ok, nil} -&gt; 0
      {:ok, n} -&gt; n
    end)
    |&gt; Enum.sum()
    |&gt; IO.puts()
  end
end

Day7.Part2.solve()
</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="349061" 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-7/67938/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-349061" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349061"
                     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 #45"></div>
  </section>
</div>
    <div class="postbit" id="349317" data-post-id="349317">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Perhaps I’m just getting more familiar with writing Elixir code, but this was the easiest one yet for me. Didn’t do anything fancy, just tried every combination of calculations.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day7 do
  @moduledoc false

  defp get_input(file) do
    File.read!(file)
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(&amp;String.split(&amp;1, ~r/[: ]+/, trim: true))
    |&gt; Enum.map(fn l -&gt; Enum.map(l, &amp;String.to_integer/1) end)
  end

  defp pipepipe(left, right) do
    String.to_integer(Integer.to_string(left) &lt;&gt; Integer.to_string(right))
  end

  defp solvable([target | [first | rest]], operators) do
    solvable = Kernel.in(target,
      Enum.reduce(rest, [first], fn right, lefts -&gt;
        for left &lt;- lefts, op &lt;- operators do
          op.(left, right)
        end
      end))
    if solvable, do: target, else: 0
  end

  def part1(file) do
    operators = [&amp;+/2, &amp;*/2]
    get_input(file)
    |&gt; Enum.map(&amp;solvable(&amp;1, operators))
    |&gt; Enum.sum()
  end

  def part2(file) do
    operators = [&amp;+/2, &amp;*/2, &amp;pipepipe/2]
    get_input(file)
    |&gt; Enum.map(&amp;solvable(&amp;1, operators))
    |&gt; Enum.sum()
  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="349317" 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-7/67938/47">Post #46</a>
	                </div>
	            </div>
              <div id="likers-container-349317" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349317"
                     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 #46"></div>
  </section>
</div>
    <div class="postbit" id="349927" data-post-id="349927">
  <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>One of the good days, when the abstraction I picked made part 2 <em>easier</em> for once:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule BridgeFix do
  def read(filename) do
    File.stream!(filename)
    |&gt; Stream.map(&amp;String.trim/1)
    |&gt; Stream.map(&amp;String.split(&amp;1, ~r/[: ]+/))
    |&gt; Stream.map(fn l -&gt; Enum.map(l, &amp;String.to_integer/1) end)
    |&gt; Stream.map(fn [r | rest] -&gt; {r, rest} end)
  end

  def can_work?({desired, values}) do
    values
    |&gt; then(&amp; length(&amp;1) - 1)
    |&gt; operators()
    |&gt; Enum.any?(fn ops -&gt;
      run(values, ops) == desired
    end)
  end

  @operators [:plus, :times, :concat]

  defp operators(0), do: [[]]

  defp operators(n) do
    prev = operators(n-1)

    @operators
    |&gt; Enum.map(fn op -&gt; Stream.map(prev, &amp;[op | &amp;1]) end)
    |&gt; Stream.concat()
  end

  defp do_op(:plus, x, y), do: x + y
  defp do_op(:times, x, y), do: x * y
  defp do_op(:concat, x, y), do: String.to_integer("#{x}#{y}")

  defp run([x], []), do: x
  defp run([x, y | rest], [operator | op_rest]) do
    run([do_op(operator, x, y) | rest], op_rest)
  end
end

BridgeFix.read("input.txt")
|&gt; Task.async_stream(&amp;{BridgeFix.can_work?(&amp;1), &amp;1}, ordered: false)
|&gt; Stream.flat_map(fn
  {:ok, {true, {x, _}}} -&gt; [x]
  _ -&gt; []
end)
|&gt; Enum.sum()
|&gt; IO.inspect()
</code></pre>
<p>(part 1 is the same code, just without <code>:concat</code> in <code>@operators</code>)</p>
<p><code>Task.async_stream</code> helped quite a bit, since this is the textbook definition of “embarrassingly parallel” <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" 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="349927" 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-7/67938/48">Post #47</a>
	                </div>
	            </div>
              <div id="likers-container-349927" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349927"
                     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 #47"></div>
  </section>
</div>
    <div class="postbit" id="351213" data-post-id="351213">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Came back to this since I skipped it at the time.</p>
<p>I used the new <a href="https://hexdocs.pm/elixir/Enum.html#sum_by/2" rel="noopener nofollow ugc"><code>Enum.sum_by/2</code></a> from Elixir 1.18</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def part2(input) do
  Enum.sum_by(input, fn {answer, values} -&gt;
    if solvable2?(answer, values), do: answer, else: 0
  end)
end

def solvable2?(answer, [result]), do: answer == result

def solvable2?(answer, [a, b | rest]) do
  solvable2?(answer, [a * b | rest]) or solvable2?(answer, [a + b | rest]) or
    solvable2?(answer, [String.to_integer("#{a}#{b}") | rest])
end
</code></pre>
<p><a href="https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2024/day7.exs" class="onebox" target="_blank" rel="noopener nofollow ugc">https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2024/day7.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="351213" 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-7/67938/49">Post #48</a>
	                </div>
	            </div>
              <div id="likers-container-351213" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351213"
                     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 #48"></div>
  </section>
</div>
    <div class="postbit" id="351230" data-post-id="351230">
  <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>Naive but quick enough for me. Initially thought about doing some sort of tree structure and then traversing the tree to do the calculation steps but quickly realized that was pointless. I like the recursion, not so much the nested list generation and <code>List.flatten</code> step.</p>
<p><a href="https://github.com/stevensonmt/advent_of_code/blob/02061f6bc06937701d95e274d990801a49eeb93a/2024/day7/lib/day7.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/stevensonmt/advent_of_code/blob/02061f6bc06937701d95e274d990801a49eeb93a/2024/day7/lib/day7.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="351230" 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-7/67938/50">Post #49</a>
	                </div>
	            </div>
              <div id="likers-container-351230" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351230"
                     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>