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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is mine:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Solutions.Y24.Day05 do
  alias AoC.Input

  def parse(input, _part) do
    [rules, updates] = input |&gt; Input.read!() |&gt; String.split("\n\n")
    {parser(rules, "|"), parser(updates, ",")}
  end

  def part_one({rules, updates}) do
    updates
    |&gt; Enum.map(&amp;{&amp;1, pair_up(&amp;1)})
    |&gt; Enum.filter(fn {_, list} -&gt; check_rules(list, rules) end)
    |&gt; Enum.map(fn {original, _} -&gt; original end)
    |&gt; Enum.reduce(0, &amp;(Enum.at(&amp;1, div(length(&amp;1), 2)) + &amp;2))
  end

  def part_two({rules, updates}) do
    updates
    |&gt; Enum.map(&amp;{&amp;1, pair_up(&amp;1)})
    |&gt; Enum.filter(fn {_, list} -&gt; not check_rules(list, rules) end)
    |&gt; Enum.map(fn {e, list} -&gt; switch_by_rules(list, rules, e) end)
    |&gt; Enum.reduce(0, &amp;(Enum.at(&amp;1, div(length(&amp;1), 2)) + &amp;2))
  end

  defp check_rules([], _rules), do: true

  defp check_rules([{a, b} | rest], rules) do
    case Enum.find(rules, &amp;(&amp;1 == [a, b])) do
      nil -&gt; false
      _ -&gt; check_rules(rest, rules)
    end
  end

  defp switch_by_rules([], _rules, solution), do: solution

  defp switch_by_rules([{a, b} | rest], rules, solution) do
    case Enum.find(rules, &amp;(&amp;1 == [a, b])) do
      nil -&gt;
        solution = switch_up(solution, a, b)
        switch_by_rules(pair_up(solution), rules, solution)

      _ -&gt;
        switch_by_rules(rest, rules, solution)
    end
  end

  defp pair_up(list) do
    Enum.reduce(Enum.with_index(list), [], fn {_v, i}, acc -&gt;
      {heads, rest} = Enum.split(list, i + 1)
      head = List.last(heads)
      Enum.map(rest, fn e -&gt; {head, e} end) ++ acc
    end)
  end

  defp switch_up(list, a, b) do
    Enum.map(list, fn e -&gt;
      cond do
        e == a -&gt; b
        e == b -&gt; a
        true -&gt; e
      end
    end)
  end

  defp parser(input, separator) do
    input
    |&gt; String.trim()
    |&gt; String.split("\n")
    |&gt; Enum.map(fn e -&gt;
      Enum.map(String.split(e, separator), &amp;String.to_integer(&amp;1))
    end)
  end
end

</code></pre>
<p>Bench:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Solution for 2024 day 5
part_one: 4609 in 114.35ms
part_two: 5723 in 2.95s
</code></pre>
<p>Almost 3 seconds for part 2. <img src="https://forum.elixirforum.com/images/emoji/apple/dizzy_face.png?v=15" title=":dizzy_face:" class="emoji" alt=":dizzy_face:" loading="lazy" width="20" height="20"> If I keep this up… I won’t keep up. <img src="https://forum.elixirforum.com/images/emoji/apple/face_with_peeking_eye.png?v=15" title=":face_with_peeking_eye:" class="emoji" alt=":face_with_peeking_eye:" loading="lazy" width="20" height="20"><br>
<img src="https://forum.elixirforum.com/uploads/default/original/3X/3/a/3a2903e082875d6abad2b34b6193e156c6e63600.png" alt="image" data-base62-sha1="8ivzcJhgfXRD2ARvZ5UTcADWlt6" width="70" height="78"></p>
<p>edit: I should make a comparison with stream. <img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji" alt=":thinking:" 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="348583" 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-5/67893/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-348583" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348583"
                     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="348586" data-post-id="348586">
  <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>Easy part 1, I had to think more for part 2. I thought my approach would take an infinite time, but it didn’t (about 40ms)</p>
<p>I implemented a <em>bubble sort</em> like approach:</p>
<ul>
<li>traverse the list of numbers</li>
<li>every time we find an invalid number (compared to all previous ones), swap them</li>
<li>then recursive call to traverse and fix the new swapped list.</li>
</ul>
<p><strong>Part 1</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2024.Day05.Part1 do
  def run(puzzle) do
    {rules, updates} = parse(puzzle)

    updates
    |&gt; Enum.filter(&amp;valid?(&amp;1, rules))
    |&gt; Enum.map(&amp;Enum.at(&amp;1, &amp;1 |&gt; length |&gt; div(2)))
    |&gt; Enum.sum()
  end

  def parse(puzzle) do
    [rules, updates] = String.split(puzzle, "\n\n")

    rules =
      for rule &lt;- String.split(rules, "\n"), reduce: MapSet.new() do
        acc -&gt;
          [p1, p2] = String.split(rule, "|")
          MapSet.put(acc, {String.to_integer(p1), String.to_integer(p2)})
      end

    updates =
      for pages &lt;- String.split(updates, "\n") do
        for page &lt;- String.split(pages, ","),
            page = String.to_integer(page),
            do: page
      end

    {rules, updates}
  end

  def valid?(update, rules) do
    update
    |&gt; Enum.reduce({[], true}, fn
      _page, {prev, false} -&gt;
        {prev, false}

      page, {prev, _valid?} -&gt;
        valid? = not Enum.any?(prev, &amp;MapSet.member?(rules, {page, &amp;1}))
        {[page | prev], valid?}
    end)
    |&gt; elem(1)
  end
end
</code></pre>
<p><strong>Part 2</strong></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2024.Day05.Part2 do
  alias Advent.Y2024.Day05.Part1

  def run(puzzle) do
    {rules, updates} = Part1.parse(puzzle)

    updates
    |&gt; Enum.reject(&amp;Part1.valid?(&amp;1, rules))
    |&gt; Enum.map(&amp;for {p, i} &lt;- Enum.with_index(&amp;1), into: %{}, do: {i, p})
    |&gt; Enum.map(&amp;fix_update(&amp;1, rules))
    |&gt; Enum.map(&amp;Map.get(&amp;1, &amp;1 |&gt; Enum.count() |&gt; div(2)))
    |&gt; Enum.sum()
  end

  def fix_update(update, rules) do
    for i &lt;- 1..(Enum.count(update) - 1), j &lt;- 0..(i - 1), reduce: {nil, true} do
      {indexes, false} -&gt;
        {indexes, false}

      {nil, true} -&gt;
        {first, second} = {Map.get(update, j), Map.get(update, i)}

        if MapSet.member?(rules, {second, first}) do
          {{i, j}, false}
        else
          {nil, true}
        end
    end
    |&gt; then(fn
      {nil, true} -&gt; update
      {{i, j}, false} -&gt; update |&gt; swap(i, j) |&gt; fix_update(rules)
    end)
  end

  defp swap(update, i, j) do
    update |&gt; Map.put(i, Map.get(update, j)) |&gt; Map.put(j, Map.get(update, i))
  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="348586" 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-5/67893/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-348586" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348586"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="348587" data-post-id="348587">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here is my pretty naive straightforward implementation :</p>
<pre data-code-wrap="ex"><code class="lang-ex">defmodule Y2024.D05 do
  use Day, input: "2024/05", part1: ~c"c", part2: ~c"c"

  defp part1(input) do
    {rules, updates} = parse_input(input)

    updates
    |&gt; Enum.reject(&amp;invalid?(&amp;1, rules))
    |&gt; Enum.map(&amp;Enum.at(&amp;1, div(Enum.count(&amp;1), 2)))
    |&gt; Enum.sum()
  end

  defp part2(input) do
    {rules, updates} = parse_input(input)

    updates
    |&gt; Enum.filter(&amp;invalid?(&amp;1, rules))
    |&gt; Enum.map(&amp;reorder(&amp;1, rules))
    |&gt; Enum.map(&amp;Enum.at(&amp;1, div(Enum.count(&amp;1), 2)))
    |&gt; Enum.sum()
  end

  defp invalid?(update, rule_or_rules), do: not valid?(update, rule_or_rules)

  defp valid?(update, {f, s}) do
    case {Enum.find_index(update, &amp;(&amp;1 == f)), Enum.find_index(update, &amp;(&amp;1 == s))} do
      {nil, _} -&gt; true
      {_, nil} -&gt; true
      {fi, si} when fi &lt; si -&gt; true
      _ -&gt; false
    end
  end

  defp valid?(update, rules), do: Enum.all?(rules, &amp;valid?(update, &amp;1))

  defp reorder(update, rules) do
    {f, s} = Enum.find(rules, &amp;invalid?(update, &amp;1))
    {fi, si} = {Enum.find_index(update, &amp;(&amp;1 == f)), Enum.find_index(update, &amp;(&amp;1 == s))}

    modified =
      update
      |&gt; List.replace_at(fi, s)
      |&gt; List.replace_at(si, f)

    if valid?(modified, rules), do: modified, else: reorder(modified, rules)
  end

  defp parse_input(input) do
    [rules_chunk, updates_chunk] = input
    {parse_rules(rules_chunk), parse_updates(updates_chunk)}
  end

  defp parse_rules(rules), do: Enum.map(rules, &amp;parse_rule/1)
  defp parse_updates(updates), do: Enum.map(updates, &amp;parse_update/1)

  defp parse_rule(&lt;&lt;f::bytes-size(2), "|", s::bytes-size(2)&gt;&gt;), do: {parse_page(f), parse_page(s)}

  defp parse_update(update) do
    update
    |&gt; Utils.splitrim(",")
    |&gt; Enum.map(&amp;parse_page/1)
  end

  defp parse_page(page), do: String.to_integer(page)
end
</code></pre>
<p>It solves part 2 in less than 800ms which is seems pretty decent for a very suboptimal solution !</p>
<p>Edit : simple optimisations reduce the execution time to &lt; 130ms for part 2 <img src="https://forum.elixirforum.com/images/emoji/apple/tada.png?v=15" title=":tada:" class="emoji" alt=":tada:" 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="348587" 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-5/67893/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-348587" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348587"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="348592" data-post-id="348592">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Naive solution using <code>sort_by</code>.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">input = """
"""

[rules, updates] = input |&gt; String.split("\n\n")

rules = for rule &lt;- rules |&gt; String.split("\n"),
    [a, b] = rule |&gt; String.split("|"),
    a = String.to_integer(a),
    b = String.to_integer(b)
  do
  {a, b}
end

updates = for update &lt;- updates |&gt; String.trim() |&gt; String.split("\n"),
  update = update |&gt; String.trim() |&gt; String.split(",") do
    for page &lt;- update,
      page = String.to_integer(page) do
      page
    end
  end

rule_sorter = fn a, b -&gt;
  a_precedes_b = Enum.any?(rules, &amp;(&amp;1 == {a, b}))
  b_precedes_a = Enum.any?(rules, &amp;(&amp;1 == {b, a}))
  cond do 
    b_precedes_a -&gt; false
    a_precedes_b -&gt; true
    true -&gt; true
  end
end

for update &lt;- updates,
  sorted = Enum.sort(update, rule_sorter),
  sorted != update, # change to == for part 1, != for part 2
  reduce: 0 do
  acc -&gt; 
    len = sorted |&gt; Enum.count
    mid_idx = floor(len / 2)
    acc + Enum.at(sorted, mid_idx)
end
</code></pre>
<p>Making the following changes reduces the part 2 solve time from 4.2s to 44ms:</p>
<p>Change <code>rules</code> to use <code>Map</code>s and <code>MapSet</code>s:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">rules = for rule &lt;- rules |&gt; String.split("\n"),
    [a, b] = rule |&gt; String.split("|"),
    a = String.to_integer(a),
    b = String.to_integer(b),
    reduce: %{}
  do
    acc -&gt; 
      Map.update(acc, a, MapSet.new([b]), fn c -&gt; MapSet.put(c, b) end)
end
</code></pre>
<p>Change the <code>rule_sorter</code> to accommodate:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">rule_sorter = fn a, b -&gt;
  a_precedes_b = Map.get(rules, a, MapSet.new()) |&gt; MapSet.member?(b)
  b_precedes_a = Map.get(rules, b, MapSet.new()) |&gt; MapSet.member?(a)
  cond do 
    b_precedes_a -&gt; false
    a_precedes_b -&gt; true
    true -&gt; true
  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="348592" 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-5/67893/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-348592" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348592"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="348596" data-post-id="348596">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>LOC: 21<br>
EDIT: 19 (found a shorter <code>reorder</code>)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day05 do
  def part1(file), do: file |&gt; main() |&gt; elem(0)
  def part2(file), do: file |&gt; main() |&gt; elem(1)

  def main(file) do
    {orders, [_ | updates]} = file |&gt; file_to_lines |&gt; Enum.split_while(&amp;(&amp;1 != ""))
    orders = orders |&gt; Enum.map(&amp;to_integers(&amp;1, "|")) |&gt; Enum.group_by(&amp;hd/1, &amp;List.last/1)
    updates = Enum.map(updates, &amp;to_integers(&amp;1, ","))
    {corrects, incorrects} = Enum.split_with(updates, &amp;c?(Enum.reverse(&amp;1), orders))
    {sum_middles(corrects), sum_middles(Enum.map(incorrects, &amp;reorder(&amp;1, orders, [])))}
  end

  def file_to_lines(file), do: file |&gt; File.read!() |&gt; String.trim() |&gt; String.split("\n")
  def to_integers(line, sep), do: line |&gt; String.split(sep) |&gt; Enum.map(&amp;String.to_integer/1)
  def sum_middles(lists), do: lists |&gt; Enum.map(&amp;Enum.at(&amp;1, div(length(&amp;1), 2))) |&gt; Enum.sum()
  def c?([h | t], o), do: if(Enum.any?(t, &amp;(&amp;1 in Map.get(o, h, []))), do: false, else: c?(t, o))
  def c?([], _), do: true
  def reorder(l, o), do: Enum.sort(l, &amp;(&amp;2 in Map.get(o, &amp;1, [])))
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="348596" 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-5/67893/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-348596" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348596"
                     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 #15"></div>
  </section>
</div>
    <div class="postbit" id="348597" data-post-id="348597">
  <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>I got two stars with solutions similar to most people here, but what I don’t understand is how this problem is solvable in general. It only works because the elements of the update list all appear in the ordering rules. But what if an update list were to have some elements that don’t have any rules? They could essentially appear anywhere in the otherwise sorted list, making it impossible to know what the center page number is. How do the instructions rule out this possibility?</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[rules, updates] = puzzle_input |&gt; String.split("\n\n", trim: true)

rules =
  rules
  |&gt; String.split(["\n", "|"])
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; Enum.chunk_every(2)
  |&gt; Enum.reduce(%{}, fn [x, y], acc -&gt; Map.update(acc, x, [y], &amp;[y | &amp;1]) end)

updates =
  updates
  |&gt; String.split("\n", trim: true)
  |&gt; Enum.map(fn update -&gt;
    update |&gt; String.split(",") |&gt; Enum.map(&amp;String.to_integer/1)
  end)

valid_update? = fn update, rules -&gt;
  for i &lt;- 0..length(update), {a, b} = Enum.split(update, i) do
    case {a, b} do
      {_, []} -&gt;
        true

      {a, [current | _]} -&gt;
        current_rules = Map.get(rules, current, [])
        MapSet.disjoint?(MapSet.new(current_rules), MapSet.new(a))
    end
  end
  |&gt; Enum.all?(&amp; &amp;1)
end

updates
|&gt; Enum.filter(&amp;valid_update?.(&amp;1, rules))
|&gt; Enum.map(fn list -&gt; Enum.at(list, div(length(list), 2)) end)
|&gt; Enum.sum()
</code></pre>
<p>Part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">[rules, updates] = puzzle_input |&gt; String.split("\n\n", trim: true)

rules =
  rules
  |&gt; String.split(["\n", "|"])
  |&gt; Enum.map(&amp;String.to_integer/1)
  |&gt; Enum.chunk_every(2)
  |&gt; Enum.reduce(%{}, fn [x, y], acc -&gt; Map.update(acc, x, [y], &amp;[y | &amp;1]) end)

updates =
  updates
  |&gt; String.split("\n", trim: true)
  |&gt; Enum.map(fn update -&gt;
    update |&gt; String.split(",") |&gt; Enum.map(&amp;String.to_integer/1)
  end)

valid_update? = fn update, rules -&gt;
  for i &lt;- 0..length(update), {a, b} = Enum.split(update, i) do
    case {a, b} do
      {_, []} -&gt;
        true

      {a, [current | _]} -&gt;
        current_rules = Map.get(rules, current, [])
        MapSet.disjoint?(MapSet.new(current_rules), MapSet.new(a))
    end
  end
  |&gt; Enum.all?(&amp; &amp;1)
end

updates
|&gt; Enum.reject(&amp;valid_update?.(&amp;1, rules))
|&gt; Enum.map(&amp;Enum.sort(&amp;1, fn a, b -&gt; b in Map.get(rules, a, []) end))
|&gt; Enum.map(fn list -&gt; Enum.at(list, div(length(list), 2)) end)
|&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="348598" 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-5/67893/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-348598" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348598"
                     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 #17"></div>
  </section>
</div>
    <div class="postbit" id="348599" data-post-id="348599">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t think it is solvable in general. Counterexample:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">1|2

2,1,3
</code></pre>
<p>Both <code>1,2,3</code> and <code>3,1,2</code> are correct re-orderings but with different middle numbers.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348599" 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-5/67893/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-348599" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348599"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="348600" data-post-id="348600">
  <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>Yes, exactly. We’re only lucky because something like that did not appear in the data.</p> 
	            </div>

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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sevenseacat" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sevenseacat/120/23153_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sevenseacat
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Author of Ash Framework</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think you’ll find the data was deliberately designed that way <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="348601" 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-5/67893/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-348601" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348601"
                     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 #20"></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/67893/load_more?page=3">Load more posts (38 remaining)</a>
</div></template></turbo-stream>