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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I finally got back to it!</p>
<p>this day is Kida fun and part2 can be fast achieved by comparing Range. But I was lazy so I just interacted everything through a list and used the same logic. as part 1, but with Task.async between all ranges.</p>
<p>It took 30 min to compute everything on my MacBook Air M2. XD Basically the time I had to do the dishes and see the result.</p>
<p><a href="https://github.com/pedrosnk/coffee_and_kata/blob/main/aoc_2023/day_05/lib/day05.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/pedrosnk/coffee_and_kata/blob/main/aoc_2023/day_05/lib/day05.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="310699" 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-2023-day-5/60170/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-310699" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310699"
                     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="310700" data-post-id="310700">
  <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>Wow, you are way more patient than me. I will give a solution maybe 5 minutes before I just kill it.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="310700" 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-2023-day-5/60170/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-310700" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310700"
                     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="310701" data-post-id="310701">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was basically multitasking at the time. If I was focusing on the problem probably would start over again after 1 min. Humans are definitely not meant to paralyze everything.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="310701" 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-2023-day-5/60170/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-310701" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310701"
                     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="310946" data-post-id="310946">
  <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>Another “calculate the result of applying maps to input ranges, splitting as necessary” solution for part 2.</p>
<p>Getting the heart of <code>intersect</code> right was really tricky, but it ended up very symmetrical.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day5Part2 do
  def read(filename) do
    [{:seeds, seeds} | mappings] =
      File.read!(filename)
      |&gt; String.split("\n\n")
      |&gt; Enum.map(&amp;String.split(&amp;1, ~r{:\s+}))
      |&gt; Enum.map(fn [label, value] -&gt; parse(label, value) end)

    {seeds, Map.new(mappings)}
  end

  defp parse("seeds", value) do
    value
    |&gt; parse_numbers()
    |&gt; parse_ranges()
    |&gt; then(&amp;{:seeds, &amp;1})
  end

  defp parse(label, value) do
    %{"from" =&gt; from, "to" =&gt; to} = parse_label(label)

    {from, {to, parse_mapping(value)}}
  end

  defp parse_numbers(value) do
    value
    |&gt; String.split(" ")
    |&gt; Enum.map(&amp;String.to_integer/1)
  end

  defp parse_ranges(numbers) do
    numbers
    |&gt; Enum.chunk_every(2)
    |&gt; Enum.map(fn [start, len] -&gt; {start..start+len, 0} end)
    |&gt; normalize()
  end

  @regex ~r{(?&lt;from&gt;\w+)-to-(?&lt;to&gt;\w+) map}
  defp parse_label(label) do
    Regex.named_captures(@regex, label)
  end

  defp parse_mapping(value) do
    value
    |&gt; String.split("\n")
    |&gt; Enum.map(&amp;parse_numbers/1)
    |&gt; Enum.map(&amp;parse_one_mapping/1)
  end

  defp parse_one_mapping([dest_start, source_start, len]) do
    {source_start..(source_start+len-1), dest_start - source_start}
  end

  def intersect(source, []), do: [source]

  def intersect({r_source, o_source}, [{r_map, o_map} | rest] = mapping) do
    cond do
      disjoint?({r_source, o_source}, r_map) -&gt;
        intersect({r_source, o_source}, rest)

      r_source.first + o_source &lt; r_map.first -&gt;
        # extra at the front
        thru = Range.new(r_source.first, r_map.first-1-o_source, 1)
        new_r1 = Range.new(r_map.first-o_source, r_source.last, 1)

        [{thru, o_source} | intersect({new_r1, o_source}, mapping)]

      r_source.last + o_source &gt; r_map.last -&gt;
        # extra at the end
        thru = Range.new(r_map.last+1-o_source, r_source.last, 1)
        new_r1 = Range.new(r_source.first, r_map.last-o_source, 1)

        [{new_r1, o_source+o_map} | intersect({thru, o_source}, mapping)]

      true -&gt;
        # completely inside r_map
        [{r_source, o_source+o_map}]
    end
  end

  defp disjoint?({r1, off}, r2) do
    r1
    |&gt; then(&amp;Range.new(&amp;1.first + off, &amp;1.last + off, 1))
    |&gt; Range.disjoint?(r2)
  end

  def mapping_product(mapping) do
    "seed"
    |&gt; Stream.unfold(&amp;next_mapping(&amp;1, mapping))
    |&gt; Stream.map(&amp;normalize/1)
    |&gt; Enum.to_list()
  end

  defp next_mapping(label, mapping) do
    case Map.get(mapping, label) do
      nil -&gt; nil
      {next_label, ranges} -&gt;
        {ranges, next_label}
    end
  end

  def normalize(ranges) do
    Enum.sort_by(ranges, &amp;elem(&amp;1, 0).first)
  end
end

{seeds, mapping} = Day5Part2.read("input.txt")

product = Day5Part2.mapping_product(mapping)

Enum.reduce(product, seeds, fn mapping, acc -&gt;
  acc
  |&gt; Enum.flat_map(&amp;Day5Part2.intersect(&amp;1, mapping))
  |&gt; Day5Part2.normalize()
end)
|&gt; Enum.map(fn {r, o} -&gt; r.first + o end)
|&gt; Enum.min()
|&gt; IO.inspect
</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="310946" 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-2023-day-5/60170/26">Post #25</a>
	                </div>
	            </div>
              <div id="likers-container-310946" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="310946"
                     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="311709" data-post-id="311709">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I managed to bring down the execution time from ~30 minutes (laptop did not enjoy this) to ~1ms using your approach. Really nice and elegant solution, thank you!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="311709" 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-2023-day-5/60170/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-311709" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="311709"
                     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>