<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="348657" data-post-id="348657">
  <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>The first task of today was much easier than yesterdays first task.  Took a while, but i made it:</p>
<p><a href="https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_5/Part1.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_5/Part1.ex</a></p>
<p>for part 2 im not really sure how to debug why it doesnt pass on the real input. It passes on the test input. Not sure what i can do to debug. Any help?  Or tips to fix my code <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"> maybe it has bugs ..</p>
<p><a href="https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_5/Part2.ex" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_5/Part2.ex</a></p>
<p>i mean good thing santa doesnt get angry even if i complete the parts next day right <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"> <img src="https://forum.elixirforum.com/images/emoji/apple/rofl.png?v=15" title=":rofl:" class="emoji" alt=":rofl:" 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="348657" 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/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-348657" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348657"
                     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 #31"></div>
  </section>
</div>
    <div class="postbit" id="348661" data-post-id="348661">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I don’t have much advice to offer on debugging, but I can offer a hint: after you run <code>fix_invalid_update()</code> on an update, check if it’s valid again.  I think you’ll find that some still aren’t <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> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Fun problem!</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AOC.Y2024.Day5 do
  @moduledoc false

  use AOC.Solution

  @impl true
  def load_data() do
    Data.load_day(2024, 5, "\n\n")
    |&gt; then(fn [rules, updates] -&gt;
      {
        rules
        |&gt; String.split("\n")
        |&gt; Enum.map(&amp;(&amp;1 |&gt; String.split("|") |&gt; List.to_tuple()))
        |&gt; MapSet.new(),
        updates
        |&gt; String.split("\n")
        |&gt; Enum.map(&amp;String.split(&amp;1, ","))
      }
    end)
  end

  @impl true
  def part_one({rules, updates}) do
    updates
    |&gt; Enum.filter(fn update -&gt; right_order?(rules, update) end)
    |&gt; General.map_sum(fn update -&gt;
      update |&gt; Enum.at(div(length(update), 2)) |&gt; String.to_integer()
    end)
  end

  @impl true
  def part_two({rules, updates}) do
    updates
    |&gt; Enum.reject(fn update -&gt; right_order?(rules, update) end)
    |&gt; Enum.map(fn update -&gt;
      Enum.sort(update, fn a, b -&gt; not MapSet.member?(rules, {b, a}) end)
    end)
    |&gt; General.map_sum(fn update -&gt;
      update |&gt; Enum.at(div(length(update), 2)) |&gt; String.to_integer()
    end)
  end

  defp right_order?(rules, update) do
    update
    |&gt; Enum.with_index(1)
    |&gt; Enum.map(fn {v, i} -&gt; update |&gt; Enum.slice(i..-1//1) |&gt; Enum.map(fn k -&gt; {v, k} end) end)
    |&gt; Enum.all?(fn order -&gt;
      Enum.all?(order, fn {a, b} -&gt; !MapSet.member?(rules, {b, a}) end)
    end)
  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="348662" 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/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-348662" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348662"
                     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 #33"></div>
  </section>
</div>
    <div class="postbit" id="348665" data-post-id="348665">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You’re right .. Thank you.<br>
I think I missed this as I was refactoring the code after part2 was complete. Here’s the revised code:</p>
<p>def check_page_order(page_list, rules) do<br>
page_list</p>
<blockquote>
<blockquote>
<p>Enum.sort(fn n1, n2 → <br>
rule = Map.get(rules, n1, [n1])<br>
not Enum.member?(rule, n2)<br>
end)<br>
Enum.reverse()<br>
end</p>
</blockquote>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348665" 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/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-348665" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348665"
                     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 #34"></div>
  </section>
</div>
    <div class="postbit" id="348666" data-post-id="348666">
  <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>I’ve used digraph and digraph_utils.<br>
Doing so may be overkill but digraph_utils really helped quickly solving part 2.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule D5 do
  def p1(file) do
    {digraph, updates} = parse(file)

    updates
    |&gt; Enum.filter(fn update -&gt; right_order?(update, digraph) end)
    |&gt; Enum.map(fn update -&gt; Enum.at(update, div(Enum.count(update), 2)) end)
    |&gt; Enum.sum()
  end

  defp parse(file) do
    [section1, section2] =
      file
      |&gt; File.read!()
      |&gt; String.split("\n\n")

    digraph = :digraph.new()

    section1
    |&gt; String.split("\n")
    |&gt; Enum.map(fn rule -&gt; rule |&gt; String.split("|") |&gt; Enum.map(&amp;String.to_integer/1) end)
    |&gt; Enum.each(fn [n1, n2] -&gt;
      :digraph.add_vertex(digraph, n1)
      :digraph.add_vertex(digraph, n2)
      :digraph.add_edge(digraph, n1, n2)
    end)

    updates =
      section2
      |&gt; String.split(["\n"])
      |&gt; Enum.map(fn u -&gt; String.split(u, ",") |&gt; Enum.map(&amp;String.to_integer/1) end)

    {digraph, updates}
  end

  defp right_order?([n1, n2 | rest], digraph) do
    case :digraph.get_short_path(digraph, n1, n2) do
      [^n1, ^n2] -&gt;
        right_order?([n2 | rest], digraph)

      _ -&gt;
        false
    end
  end

  defp right_order?(_, _) do
    true
  end

  def p2(file) do
    {digraph, updates} = parse(file)

    updates
    |&gt; Enum.filter(fn update -&gt; not right_order?(update, digraph) end)
    |&gt; Enum.map(fn update -&gt;
      digraph
      |&gt; :digraph_utils.subgraph(update)
      |&gt; :digraph_utils.topsort()
    end)
    |&gt; Enum.map(fn update -&gt; Enum.at(update, div(Enum.count(update), 2)) end)
    |&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="348666" 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/36">Post #35</a>
	                </div>
	            </div>
              <div id="likers-container-348666" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348666"
                     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 #35"></div>
  </section>
</div>
    <div class="postbit" id="348669" data-post-id="348669">
  <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>Here’s how I did this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule AdventOfCode.Y2024.Day05 do
  alias AdventOfCode.Helpers.{InputReader, Transformers}

  def input, do: InputReader.read_from_file(2024, 5)

  def run(input \\ input()) do
    input = parse(input)

    {run_1(input), run_2(input)}
  end

  defp run_1(input) do
    input
    |&gt; Enum.filter(fn {a, b} -&gt; a == b end)
    |&gt; Enum.map(fn {a, _} -&gt; a |&gt; Enum.at(div(length(a), 2)) end)
    |&gt; Enum.sum()
  end

  defp run_2(input) do
    input
    |&gt; Enum.filter(fn {a, b} -&gt; a != b end)
    |&gt; Enum.map(fn {_, b} -&gt; b |&gt; Enum.at(div(length(b), 2)) end)
    |&gt; Enum.sum()
  end

  def parse(data \\ input()) do
    [deps, updates] = Transformers.sections(data)
    given_sorted_pair({parse_deps(deps), parse_updates(updates)})
  end

  defp parse_deps(deps) do
    for line &lt;- Transformers.lines(deps),
        into: MapSet.new(),
        do: String.split(line, "|") |&gt; Enum.map(&amp;String.to_integer/1) |&gt; List.to_tuple()
  end

  defp parse_updates(updates) do
    for line &lt;- Transformers.lines(updates) do
      for update &lt;- String.split(line, ","), do: String.to_integer(update)
    end
  end

  defp given_sorted_pair({deps, updates}) do
    for update &lt;- updates do
      {update, Enum.sort(update, &amp;({&amp;1, &amp;2} in deps))}
    end
  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="348669" 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/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-348669" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348669"
                     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 #36"></div>
  </section>
</div>
    <div class="postbit" id="348672" data-post-id="348672">
  <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>Nice. I was thinking of giving digraph a try, but confused myself into thinking it would sort on the values, not the edges. Nice to see it in action.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348672" 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/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-348672" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348672"
                     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 #37"></div>
  </section>
</div>
    <div class="postbit" id="348685" data-post-id="348685">
  <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>I need to not code at 12am on workdays. <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20">  This took me longer than I wanted, but I was out today. I’m just glad to have it done before the next one.</p>
<p>Part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

defmodule Day5.Part1 do
  defp parse(str) do
    [sorting_rules, [""], updates] =
      str
      |&gt; String.split("\n")
      |&gt; Enum.chunk_by(fn x -&gt; x == "" end)

    sorting_rules =
      sorting_rules
      |&gt; Enum.reduce(
        %{},
        fn str, state -&gt;
          [l, r] =
            str
            |&gt; String.split("|")

          Map.update(state, :"#{l}", [r], fn tail -&gt; [r|tail] end)
        end
      )

    updates =
      updates
      |&gt; Enum.map(fn str -&gt; String.split(str, ",") end)

    {sorting_rules, updates}
  end

  def sorted?([_], _sorting_rules), do: true

  def sorted?([a | tail], must_suffix) do
    tail
    |&gt; Enum.all?(fn t -&gt;
      must_suffix_t = must_suffix[:"#{t}"]
      is_nil(must_suffix_t) or a not in must_suffix_t
    end) and sorted?(tail, must_suffix)
  end

  def solve() do
    {sorting_rules, updates} =
      File.read!("05/input.txt")
      |&gt; parse()

    updates
    |&gt; Enum.filter(
      fn update -&gt;
        sorted?(update, sorting_rules)
      end
    )
    |&gt; Enum.reduce(
      0,
      fn update, acc -&gt;
        middle_num =
          update
          |&gt; Enum.at(
            ((update
              |&gt; Enum.count()) / 2)
            |&gt; Float.floor()
            |&gt; trunc()
          )
          |&gt; String.to_integer()

        acc + middle_num
      end
    )
    |&gt; IO.puts()
  end
end

Day5.Part1.solve()
</code></pre>
<p>Part 2:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

defmodule Day5.Part2 do
  defp parse(str) do
    [sorting_rules, [""], updates] =
      str
      |&gt; String.split("\n")
      |&gt; Enum.chunk_by(fn x -&gt; x == "" end)

    sorting_rules =
      sorting_rules
      |&gt; Enum.reduce(
        %{},
        fn str, state -&gt;
          [l, r] =
            str
            |&gt; String.split("|")

          Map.update(state, :"#{l}", [r], fn tail -&gt; [r|tail] end)
        end
      )

    updates =
      updates
      |&gt; Enum.map(fn str -&gt; String.split(str, ",") end)

    {sorting_rules, updates}
  end

  def sorted?([_], _sorting_rules), do: true

  def sorted?([a | tail], must_suffix) do
    tail
    |&gt; Enum.all?(fn t -&gt;
      must_suffix_t = must_suffix[:"#{t}"]
      is_nil(must_suffix_t) or a not in must_suffix_t
    end) and sorted?(tail, must_suffix)
  end

  defp place(x, acc, rules) do
    index = acc
    |&gt; Enum.map(
      fn item -&gt;
        if not is_nil(rules[:"#{item}"]) do
          x in rules[:"#{item}"]
        else
          false
        end
      end
    )
    |&gt; Enum.find_index(fn b -&gt; b == false end) || acc |&gt; Enum.count()


    List.insert_at(acc, index, x)
  end

  defp sort(list, rules) do
    list
    |&gt; Enum.reduce(
      [],
      fn x, acc -&gt;
        place(x, acc, rules)
      end
    )
  end

  def solve() do
    {sorting_rules, updates} =
      File.read!("05/input.txt")
      |&gt; parse()

    updates
    |&gt; Enum.filter(
      fn update -&gt;
        not sorted?(update, sorting_rules)
      end
    )
    |&gt; Enum.map(
      fn update -&gt;
        update
        |&gt; sort(sorting_rules)
      end
    )
    |&gt; Enum.reduce(
      0,
      fn update, acc -&gt;
        middle_num =
          update
          |&gt; Enum.at(
            ((update
              |&gt; Enum.count()) / 2)
            |&gt; Float.floor()
            |&gt; trunc()
          )
          |&gt; String.to_integer()

        acc + middle_num
      end
    )
    |&gt; IO.puts()
  end
end

Day5.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="348685" 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/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-348685" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348685"
                     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 #38"></div>
  </section>
</div>
    <div class="postbit" id="348686" data-post-id="348686">
  <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>is this correct algorithm for part2?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">invalid: [[47, 89, 77, 74, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]]
current: [47, 89, 77, 74, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 89 at index 1 with 77 at index 2"
current: [47, 77, 89, 74, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 47 at index 0 with 74 at index 3"
current: [74, 77, 89, 47, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 89 at index 2 with 61 at index 4"
current: [74, 77, 61, 47, 89, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 77 at index 1 with 61 at index 2"
current: [74, 61, 77, 47, 89, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 89 at index 4 with 53 at index 5"
current: [74, 61, 77, 47, 53, 89, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 47 at index 3 with 53 at index 4"
current: [74, 61, 77, 53, 47, 89, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 77 at index 2 with 53 at index 3"
current: [74, 61, 53, 77, 47, 89, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 61 at index 1 with 53 at index 2"
current: [74, 53, 61, 77, 47, 89, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 74 at index 0 with 53 at index 1"
current: [53, 74, 61, 77, 47, 89, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 89 at index 5 with 82 at index 6"
current: [53, 74, 61, 77, 47, 82, 89, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 61 at index 2 with 82 at index 5"
current: [53, 74, 82, 77, 47, 61, 89, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 74 at index 1 with 82 at index 2"
current: [53, 82, 74, 77, 47, 61, 89, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 61 at index 5 with 38 at index 7"
current: [53, 82, 74, 77, 47, 38, 89, 61, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 47 at index 4 with 38 at index 5"
current: [53, 82, 74, 77, 38, 47, 89, 61, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 77 at index 3 with 38 at index 4"
current: [53, 82, 74, 38, 77, 47, 89, 61, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 82 at index 1 with 38 at index 3"
current: [53, 38, 74, 82, 77, 47, 89, 61, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 74 at index 2 with 73 at index 9"
current: [53, 38, 73, 82, 77, 47, 89, 61, 45, 74, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 45 at index 8 with 15 at index 10"
current: [53, 38, 73, 82, 77, 47, 89, 61, 15, 74, 45, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 89 at index 6 with 15 at index 8"
current: [53, 38, 73, 82, 77, 47, 15, 61, 89, 74, 45, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 77 at index 4 with 15 at index 6"
current: [53, 38, 73, 82, 15, 47, 77, 61, 89, 74, 45, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 45 at index 10 with 76 at index 11"
current: [53, 38, 73, 82, 15, 47, 77, 61, 89, 74, 76, 45, 41, 24, 56, 55, 67, 68, 95]
"replace 82 at index 3 with 76 at index 10"
current: [53, 38, 73, 76, 15, 47, 77, 61, 89, 74, 82, 45, 41, 24, 56, 55, 67, 68, 95]
"replace 38 at index 1 with 76 at index 3"
current: [53, 76, 73, 38, 15, 47, 77, 61, 89, 74, 82, 45, 41, 24, 56, 55, 67, 68, 95]
"replace 47 at index 5 with 41 at index 12"
current: [53, 76, 73, 38, 15, 41, 77, 61, 89, 74, 82, 45, 47, 24, 56, 55, 67, 68, 95]
"replace 15 at index 4 with 41 at index 5"
current: [53, 76, 73, 38, 41, 15, 77, 61, 89, 74, 82, 45, 47, 24, 56, 55, 67, 68, 95]
"replace 15 at index 5 with 24 at index 13"
current: [53, 76, 73, 38, 41, 24, 77, 61, 89, 74, 82, 45, 47, 15, 56, 55, 67, 68, 95]
"replace 15 at index 13 with 55 at index 15"
current: [53, 76, 73, 38, 41, 24, 77, 61, 89, 74, 82, 45, 47, 55, 56, 15, 67, 68, 95]
"replace 89 at index 8 with 55 at index 13"
current: [53, 76, 73, 38, 41, 24, 77, 61, 55, 74, 82, 45, 47, 89, 56, 15, 67, 68, 95]
"replace 77 at index 6 with 55 at index 8"
current: [53, 76, 73, 38, 41, 24, 55, 61, 77, 74, 82, 45, 47, 89, 56, 15, 67, 68, 95]
"replace 24 at index 5 with 55 at index 6"
current: [53, 76, 73, 38, 41, 55, 24, 61, 77, 74, 82, 45, 47, 89, 56, 15, 67, 68, 95]
"replace 73 at index 2 with 67 at index 16"
current: [53, 76, 67, 38, 41, 55, 24, 61, 77, 74, 82, 45, 47, 89, 56, 15, 73, 68, 95]
"replace 45 at index 11 with 68 at index 17"
current: [53, 76, 67, 38, 41, 55, 24, 61, 77, 74, 82, 68, 47, 89, 56, 15, 73, 45, 95]
"replace 24 at index 6 with 68 at index 11"
current: [53, 76, 67, 38, 41, 55, 68, 61, 77, 74, 82, 24, 47, 89, 56, 15, 73, 45, 95]
"replace 55 at index 5 with 68 at index 6"
current: [53, 76, 67, 38, 41, 68, 55, 61, 77, 74, 82, 24, 47, 89, 56, 15, 73, 45, 95]
"replace 15 at index 15 with 95 at index 18"
current: [53, 76, 67, 38, 41, 68, 55, 61, 77, 74, 82, 24, 47, 89, 56, 95, 73, 45, 15]
"replace 56 at index 14 with 95 at index 15"
current: [53, 76, 67, 38, 41, 68, 55, 61, 77, 74, 82, 24, 47, 89, 95, 56, 73, 45, 15]
"replace 77 at index 8 with 95 at index 14"
fixed: [[53, 76, 67, 38, 41, 68, 55, 61, 95, 74, 82, 24, 47, 89, 77, 56, 73, 45, 15]]
</code></pre>
<p>If i a find a rule for a number in an update, where the update number should be before the the second number in the rule, i swap them with two simple replace_at</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">IO.inspect(acc_, label: "current", charlists: :as_lists, limit: :infinity)
IO.inspect("replace #{b} at index #{b_index} with #{next} at index #{new_next_id}")
acc_ |&gt; List.replace_at(new_next_id, b) |&gt; List.replace_at(b_index, next)
</code></pre>
<p>this part isnt actually rocket science … but i wonder if its correct .. well hard to read so its more like is the assumptions correct</p>
<p>i have looked at some of the rules manually for this example, and it seems to checkout</p>
<p>for ex the first one is a good example for all the others, and it represents what all the others do</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">invalid: [[47, 89, 77, 74, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]]
current: [47, 89, 77, 74, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
"replace 89 at index 1 with 77 at index 2"
current: [47, 77, 89, 74, 61, 53, 82, 38, 45, 73, 15, 76, 41, 24, 56, 55, 67, 68, 95]
</code></pre>
<p>here 89 swaps place with 77.</p>
<p>Is it correct assumption that the rules is always left to right oriented maybe?</p>
<p>The full “documented” algorithm in its full glory:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp fix_invalid_update(update, rules) do
    update
    # reduce with update itself as accumulator
    |&gt; Enum.reduce(update, fn next, acc -&gt;
      rules
      # get alll the rules for the number "next",
      # assuming i should only get rules where number "next" is the first number in the rule
      |&gt; Enum.filter(&amp;(&amp;1 |&gt; Tuple.to_list() |&gt; hd() == next))
      # reduce the rules with the current acumulator as accumulator (the update)
      |&gt; Enum.reduce(acc, fn {_, b}, acc_ -&gt;
        b_index = acc_ |&gt; Enum.find_index(&amp;(&amp;1 == b))
        # find new position for next, since it can have changed since last iteration
        new_next_id = acc_ |&gt; Enum.find_index(&amp;(&amp;1 == next))

        if b_index == nil do
          acc_
        else
          if b_index &lt; new_next_id do
            IO.inspect(acc_, label: "current", charlists: :as_lists, limit: :infinity)
            IO.inspect("replace #{b} at index #{b_index} with #{next} at index #{new_next_id}")
            acc_ |&gt; List.replace_at(new_next_id, b) |&gt; List.replace_at(b_index, next)
          else
            acc_
          end
        end
      end)
    end)
  end
</code></pre>
<p>havent read the others solutions yet. If i cant make it work myself its not worth the cheat <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> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348686" 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/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-348686" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348686"
                     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 #39"></div>
  </section>
</div>
    <div class="postbit" id="348687" data-post-id="348687">
  <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>aha . lol .. its still not valid<br>
confirmed with a<br>
|&gt; Kernel.tap(fn u → is_valid_update(u, rules) |&gt; IO.inspect() end)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348687" 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/41">Post #40</a>
	                </div>
	            </div>
              <div id="likers-container-348687" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348687"
                     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 #40"></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=5">Load more posts (18 remaining)</a>
</div></template></turbo-stream>