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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is another aspect of this problem that I think works just by chance, or by dataset design. If two or more pairs’ distances were the same, then the sorting would be indeterminant, and the solution to “last two coords that combine all nodes into one circuit” could also be indeterminant.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution, very late (I couldn’t find time today to work on it before 22:30);<br>
Nothing new/fancy when I look at those already posted: pair distances are pre-computed and sorted, the main data structure is a map %{index of the circuit =&gt; MapSet of the box coordinates in the circuit}</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmodule AdventOfCode.Solution.Year2025.Day08 do
  # Launch processing for part1 &amp; 2
  def part1(input), do: input |&gt; do_connect(1000) |&gt; mul_3_largest()
  def part2(input), do: input |&gt; do_connect(:full_connect) |&gt; mul_xs()

  # Post processing part 1
  def mul_3_largest(circuits) do
    circuits
    |&gt; Enum.map(fn {_, c} -&gt; MapSet.size(c) end)
    |&gt; Enum.sort(:desc)
    |&gt; Enum.take(3)
    |&gt; Enum.reduce(1, &amp;(&amp;1 * &amp;2))
  end

  # Post processing part 2
  def mul_xs({[x1, _, _], [x2, _, _]}), do: x1 * x2

  # The common algorithm : preparing the wiring process
  def do_connect(input, stop_at) do
    boxes_i = parse(input)

    sorted_connects =
      for({b1, i1} &lt;- boxes_i, {b2, i2} &lt;- boxes_i, i1 &lt; i2, do: add_distance(b1, b2))
      |&gt; Enum.sort_by(&amp;elem(&amp;1, 2))

    initial_circuits = for {b, i} &lt;- boxes_i, into: %{}, do: {i, MapSet.new([b])}

    wire(sorted_connects, initial_circuits, 0, stop_at, nil)
  end

  def add_distance(b1, b2),
    do: {b1, b2, Enum.zip(b1, b2) |&gt; Enum.map(fn {a, b} -&gt; (a - b) * (a - b) end) |&gt; Enum.sum()}

  # Wiring process itself
  # Stop if there is one circuit left
  def wire(_, circuits, _n, _stop_at, last) when map_size(circuits) == 1, do: last

  # Stop if we reach "stop_at" if stop_at is a number
  def wire(_, circuits, n, stop_at, _last) when stop_at != :full_connect and n == stop_at,
    do: circuits

  def wire([{b1, b2, _} | rest], circuits, n, stop_at, _last) do
    c_b1 = which_circuit(circuits, b1)
    c_b2 = which_circuit(circuits, b2)

    if c_b1 == c_b2 do
      # They are in the same circuit =&gt; do nothing
      wire(rest, circuits, n + 1, stop_at, {b1, b2})
    else
      # Different circuit, merge circuit c_b2 into c_b1
      new_circuits =
        circuits |&gt; Map.delete(c_b2) |&gt; Map.update(c_b1, nil, &amp;MapSet.union(&amp;1, circuits[c_b2]))

      wire(rest, new_circuits, n + 1, stop_at, {b1, b2})
    end
  end

  def which_circuit(circuits, b) do
    # returns the index of the circuit containing the box b
    Enum.reduce_while(circuits, nil, fn {i, c}, _acc -&gt;
      if b in c, do: {:halt, i}, else: {:cont, nil}
    end)
  end

  def parse(input) do
    input
    |&gt; String.split("\n", trim: true)
    |&gt; Enum.map(fn line -&gt;
      line |&gt; String.split(",", trim: true) |&gt; Enum.map(&amp;String.to_integer/1)
    end)
    |&gt; Enum.with_index()
  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="379609" 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-2025-day-8/73576/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-379609" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379609"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="379885" data-post-id="379885">
  <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>This one had me going for a long time trying various optimisations, but I eventually went with just keeping it simple.</p>
<p>One thing I struggled with was finding all the pairs - I was computing duplicates because I was adding <code>a, b</code> and <code>b, a</code>. I worked around it by sorting and skipping every other result - but this only worked by chance because they all had unique distances. After seeing <a class="mention" href="/u/vkryukov" rel="nofollow">@vkryukov</a>’s solution I realised the <code>a &lt; b</code> filter is the trick - I had been only filtering <code>a != b</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def calculate_distances(points) do
    for(a &lt;- points, b &lt;- points, a &lt; b, do: {a, b, distance(a, b)})
    |&gt; Enum.sort_by(&amp;elem(&amp;1, 2))
  end

  def part2(points) do
    points
    |&gt; calculate_distances()
    |&gt; Enum.reduce_while([], fn {a, b, _dist}, circuits -&gt;
      circuits = connect(a, b, circuits)

      if length(circuits) == 1 and MapSet.size(hd(circuits)) == 1000 do
        {:halt, hd(a) * hd(b)}
      else
        {:cont, circuits}
      end
    end)
  end
</code></pre>
<p><a href="https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2025/day8.exs" class="onebox" target="_blank" rel="noopener nofollow ugc">https://git.adamu.jp/adam/AdventOfCode/src/branch/main/2025/day8.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="379885" 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-2025-day-8/73576/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-379885" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="379885"
                     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>