<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="348688" data-post-id="348688">
  <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><img src="https://forum.elixirforum.com/images/emoji/apple/man_facepalming.png?v=15" title=":man_facepalming:" class="emoji only-emoji" alt=":man_facepalming:" loading="lazy" width="20" height="20"></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">  defp fix_invalid_update(update, rules) do
    new_update =
      update
      |&gt; Enum.reduce(update, fn next, acc -&gt;
        rules
        |&gt; Enum.filter(&amp;(&amp;1 |&gt; Tuple.to_list() |&gt; hd() == next))
        |&gt; Enum.reduce(acc, fn {_, b}, acc_ -&gt;
          b_index = acc_ |&gt; Enum.find_index(&amp;(&amp;1 == b))

          if b_index == nil do
            acc_
          else
            new_next_id = acc_ |&gt; Enum.find_index(&amp;(&amp;1 == next))

            if b_index &lt; new_next_id do
              acc_ |&gt; List.replace_at(new_next_id, b) |&gt; List.replace_at(b_index, next)
            else
              acc_
            end
          end
        end)
      end)

    if is_valid_update(new_update, rules),
      do: new_update,
      else: fix_invalid_update(new_update, rules)
  end
</code></pre>
<p>need multiple recursive passes… DAY5 PASSED! &lt;3</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #41"></div>
  </section>
</div>
    <div class="postbit" id="348689" data-post-id="348689">
  <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>god you were right .. and i didnt understand your comment first ..  i came to the conclusion myself</p>
<p>the interesting questions is WHY and how performant IS IT? for each update it potentially needs multiple passes …</p>
<p>answer:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">"Used 4 passes to fix update"
"Used 5 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 4 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 5 passes to fix update"
"Used 3 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 3 passes to fix update"
"Used 2 passes to fix update"
"Used 3 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 6 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 4 passes to fix update"
"Used 3 passes to fix update"
"Used 4 passes to fix update"
"Used 6 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 1 passes to fix update"
"Used 5 passes to fix update"
"Used 3 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 3 passes to fix update"
"Used 4 passes to fix update"
"Used 4 passes to fix update"
"Used 3 passes to fix update"
"Used 6 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 4 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 3 passes to fix update"
"Used 4 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 5 passes to fix update"
"Used 2 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 6 passes to fix update"
"Used 5 passes to fix update"
"Used 2 passes to fix update"
"Used 6 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 5 passes to fix update"
"Used 3 passes to fix update"
"Used 3 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 3 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 4 passes to fix update"
"Used 4 passes to fix update"
"Used 2 passes to fix update"
"Used 5 passes to fix update"
"Used 4 passes to fix update"
"Used 4 passes to fix update"
"Used 5 passes to fix update"
"Used 6 passes to fix update"
"Used 4 passes to fix update"
</code></pre>
<p>not many … but if it was 1 mill incorrect it would mean 1mill *  worst case 6 * = 6mill passes of the fix function.</p>
<p>Its O(n) though …  says perplexity .. since it doesnt grow … some updates may need few passes, some many, but doesnt affect the next</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #42"></div>
  </section>
</div>
    <div class="postbit" id="348730" data-post-id="348730">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I also got mine down to 20ms using <code>async_stream</code> based parallelism but I think i7 won’t perform as good as M1 so anyhow fairly happy</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #43"></div>
  </section>
</div>
    <div class="postbit" id="348767" data-post-id="348767">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey, can someone help me? This works for test cases but not the big data set - it’s like there are circular references. (splitting at new line, file is f)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">rules = f[0].splitlines()
pages = f[1].splitlines()

ruleListA = []
ruleListB = []

for i in rules:
    ruleListA.append(i.split("|")[0])
    ruleListB.append(i.split("|")[1])

print(ruleListA)
print(ruleListB)
print(pages)

count = 0

incorrectLines = []

def test(line):
    pageList = []
    active = True
    for number in pages[line].split(","):
        pageList.append(number)
    valid = True
    for ruleNum in range(0, len(ruleListA)):
        if ruleListA[ruleNum] in pageList and ruleListB[ruleNum] in pageList:
            if pageList.index(ruleListA[ruleNum]) &gt; pageList.index(ruleListB[ruleNum]):
                valid = False
                incorrectLines.append(pages[line])
                return -1
    if valid == True:
        return pageList[len(pageList) // 2]

for i in range(0, len(pages)):
    if test(i) != -1:
        count += int(test(i))
print(count)
print(incorrectLines)

correctOrder = []

listLock = False

print("incorrect")
print(incorrectLines)

graph = {}

print(ruleListB)

for i in range(0, len(ruleListA)):
    graph[ruleListA[i]] = []
    graph[ruleListB[i]] = []
for i in range(0, len(ruleListB)):
    graph[ruleListA[i]].append(ruleListB[i])

print(graph)
orderList = []
while graph:
  for i in graph:
      if graph[i] == []:
          num = i
          orderList.append(i)
  for i in graph:
      for j in graph[i]:
          if graph[i][graph[i].index(j)] == num:
              graph[i].remove(num)
  del graph[num]

global count2
count2 = 0

def check(order, line):
   orderToPrint = [x for x in order if x in line]
   return orderToPrint[len(orderToPrint) // 2]

for i in incorrectLines:
    count2 += int(check(orderList, i))
    
print(count2)
</code></pre>
<p>I can’t figure this out</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #44"></div>
  </section>
</div>
    <div class="postbit" id="348768" data-post-id="348768">
  <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><a class="mention" href="/u/esorsomething" rel="nofollow">@ESOrSomething</a> FWIW, this forum is targeted at Elixir (and other BEAM languages) so you may want to find a Python-specific place to ask questions about Python.</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #45"></div>
  </section>
</div>
    <div class="postbit" id="348771" data-post-id="348771">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Oh, sorry! Thanks for letting me know. I will try to find another place to post this!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="348771" 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/47">Post #46</a>
	                </div>
	            </div>
              <div id="likers-container-348771" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348771"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #46"></div>
  </section>
</div>
    <div class="postbit" id="348841" data-post-id="348841">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>This was easier than it seemed at first. Part one I just did a naive thang.</p>
<p>For part 2 I realised I could just implement a sorting function and use <code>Enum.sort_by</code> to fix it <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"></p>
<h3><a name="p-348841-part-1-1" class="anchor" href="#p-348841-part-1-1" aria-label="Heading link" rel="nofollow"></a>Part 1</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def day_5_1() do
    input = File.read!("./day_5_input.txt")
    {rules, reports} = rules_reports(input, [])

    rules = numbers_in_rules(rules)

    Enum.reduce(reports, 0, fn report, mid_number_sum -&gt;
      report_good? =
        Enum.all?(report, fn number -&gt;
          {must_come_before, must_come_after} = Map.fetch!(rules,number)
          {comes_after, comes_before} = numbers_before(number, report)

          (comes_after == [] || not Enum.any?(comes_after, &amp;(&amp;1 in must_come_before))) &amp;&amp;
            (comes_before == [] || not Enum.all?(comes_before, &amp;(&amp;1 in must_come_after)))
        end)

      if report_good? do
        mid = Enum.at(report, div(length(report), 2))
        mid_number_sum + mid
      else
        mid_number_sum
      end
    end)
  end

  defp comes_before(number, rules), do: for({^number, rule} &lt;- rules, do: rule)
  defp comes_after(number, rules), do: for({rule, ^number} &lt;- rules, do: rule)

  defp numbers_in_rules(rules) do
    Enum.reduce(rules, %{}, fn {left, right}, acc -&gt;
      acc
      |&gt; Map.put_new_lazy(left, fn -&gt; {comes_before(left, rules), comes_after(left, rules)} end)
      |&gt; Map.put_new_lazy(right, fn -&gt; {comes_before(right, rules), comes_after(right, rules) } end)
    end)
  end

  # May want to raise if we see dupes here.
  defp numbers_before(number, numbers) do
    {_, before, afterr} =
      Enum.reduce(numbers, {false, [], []}, fn n, {spotted?, before, afterr} -&gt;
        if spotted? do
          {spotted?, before, [n | afterr]}
        else
          if n == number do
            {true, before, afterr}
          else
            {false, [n | before], afterr}
          end
        end
      end)

    {before, afterr}
  end

  @pipe "|"
  def rules_reports(&lt;&lt;@new_line, @new_line, rest::binary&gt;&gt;, rules) do
    {Enum.reverse(rules), parse_reports(rest, [], [])}
  end

  def rules_reports(&lt;&lt;@new_line, rest::binary&gt;&gt;, acc), do: rules_reports(rest, acc)

  def rules_reports(&lt;&lt;left::binary-size(2), @pipe, right::binary-size(2), rest::binary&gt;&gt;, rules) do
    rules_reports(rest, [{String.to_integer(left), String.to_integer(right)} | rules])
  end

  def parse_reports(&lt;&lt;&gt;&gt;, _report, reports), do: Enum.reverse(reports)

  def parse_reports(&lt;&lt;@new_line, rest::binary&gt;&gt;, report, reports) do
    parse_reports(rest, [], [Enum.reverse(report) | reports])
  end

  def parse_reports(&lt;&lt;@comma, rest::binary&gt;&gt;, report, reports) do
    parse_reports(rest, report, reports)
  end

  def parse_reports(&lt;&lt;numb::binary-size(2), rest::binary&gt;&gt;, report, reports) do
    parse_reports(rest, [String.to_integer(numb) | report], reports)
  end
</code></pre>
<h3><a name="p-348841-part-2-2" class="anchor" href="#p-348841-part-2-2" aria-label="Heading link" rel="nofollow"></a>Part 2</h3>
<pre data-code-wrap="elixir"><code class="lang-elixir">  def day_5_2() do
    input = File.read!("./day_5_input.txt")
    {rules, reports} = rules_reports(input, [])

    rules = numbers_in_rules(rules)

    Enum.reduce(reports, 0, fn report, mid_number_sum -&gt;
      report_good? =
        Enum.all?(report, fn number -&gt;
          {must_come_before, must_come_after} = Map.fetch!(rules,number)
          {comes_after, comes_before} = numbers_before(number, report)

          (comes_after == [] || not Enum.any?(comes_after, &amp;(&amp;1 in must_come_before))) &amp;&amp;
            (comes_before == [] || not Enum.all?(comes_before, &amp;(&amp;1 in must_come_after)))
        end)

      if report_good? do
        mid_number_sum
      else
        # Kinda nasty to do this after? As could do it in the moment. but yea
        mid = Enum.at(fix_report(report, rules), div(length(report), 2))
        mid_number_sum + mid
      end
    end)
  end

  def is_less_than?(number, another, rules) do
    {_must_come_before, must_come_after} = Map.fetch!(rules, number)
    another in must_come_after
  end

  # always an odd number of items in report as there is always a mid point.
  defp fix_report(report, rules) do
    Enum.sort_by(report, &amp; &amp;1, fn left, right -&gt; !is_less_than?(left, right, rules) end)
  end
</code></pre>
<p>Obligatory meaningless benchmarks:</p>
<pre data-code-wrap="sh"><code class="lang-sh">
Name              ips        average  deviation         median         99th %
Day 5 1        1.01 K        0.99 ms    ±11.12%        0.96 ms        1.18 ms
Day 5 2        0.75 K        1.33 ms    ±10.55%        1.31 ms        1.59 ms

Comparison:
Day 5 1        1.01 K
Day 5 2        0.75 K - 1.34x slower +0.34 ms

Memory usage statistics:

Name       Memory usage
Day 5 1         1.62 MB
Day 5 2         1.82 MB - 1.12x memory usage +0.199 MB

**All measurements for memory usage were the same**

Reduction count statistics:

Name    Reduction count
Day 5 1        545.29 K
Day 5 2        615.17 K - 1.13x reduction count +69.88 K

**All measurements for reduction count were the same**
</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="348841" 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/48">Post #47</a>
	                </div>
	            </div>
              <div id="likers-container-348841" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="348841"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #47"></div>
  </section>
</div>
    <div class="postbit" id="349050" data-post-id="349050">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Working on getting caught up so I didn’t even bother refactoring the duplicated logic between part 1 &amp; 2. Calling this copy/paste/modify junk good enough. Looking forward to reading through other solutions. AOC is turning out to be a fun, and sometimes frustrating, way to learn the Elixir standard library.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day5 do
  @moduledoc false

  defp get_input(file) do
    File.read!(file)
    |&gt; String.split("\n")
    |&gt; Enum.split_while(fn line -&gt; line != "" end)
    |&gt; Tuple.to_list()
    |&gt; Enum.map(&amp;Enum.filter(&amp;1, fn line -&gt; line != "" end))
  end

  defp parse_reqs(lines) do
    lines
    |&gt; Enum.map(&amp;String.split(&amp;1, "|"))
    |&gt; Enum.map(fn [a, b] -&gt; [{b, [a]}] end)
    |&gt; Enum.map(&amp;Map.new/1)
    |&gt; Enum.reduce(Map.new(), fn req, reqs -&gt; Map.merge(reqs, req, fn _key, value1, value2 -&gt; value1 ++ value2 end) end)
  end

  defp correctly_ordered(update, reqs) do
    ordered? = update
    |&gt; Enum.with_index()
    |&gt; Enum.all?(fn {page, index} -&gt;
      Map.get(reqs, page, [])
      |&gt; Enum.all?(fn dep -&gt;
        i = Enum.find_index(update, fn p -&gt; p == dep end)
        i &lt; index or i == nil
      end)
    end)
    if ordered?, do: update, else: false
  end

  defp incorrectly_ordered(update, reqs) do
    ordered? = update
    |&gt; Enum.with_index()
    |&gt; Enum.all?(fn {page, index} -&gt;
      Map.get(reqs, page, [])
      |&gt; Enum.all?(fn dep -&gt;
        i = Enum.find_index(update, fn p -&gt; p == dep end)
        i &lt; index or i == nil
      end)
    end)
    if ordered?, do: false, else: update
  end

  defp get_middle_page(update) do
    Enum.at(update, Integer.floor_div(length(update), 2))
  end

  def part1(file) do
    [reqs, updates] = get_input(file)
    reqs = parse_reqs(reqs)

    updates
    |&gt; Enum.map(&amp;String.split(&amp;1, ","))
    |&gt; Enum.filter(&amp;correctly_ordered(&amp;1, reqs))
    |&gt; Enum.map(&amp;get_middle_page/1)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; Enum.sum()
  end

  defp add(new_update, page, bad_update, reqs) do
    update = Map.get(reqs, page, [])
    |&gt; Enum.reduce(new_update, fn dep, acc -&gt;
      if not Kernel.in(dep, bad_update) or Kernel.in(dep, acc) do
        acc
      else
        add(acc, dep, bad_update, reqs)
      end
    end)
    [page | update]
  end

  defp order_correctly(update, reqs) do
    update
    |&gt; Enum.reduce([], fn page, acc -&gt;
      if Kernel.in(page, acc) do
        acc
      else
        add(acc, page, update, reqs)
      end
    end)
  end

  def part2(file) do
    [reqs, updates] = get_input(file)
    reqs = parse_reqs(reqs)

    updates
    |&gt; Enum.map(&amp;String.split(&amp;1, ","))
    |&gt; Enum.filter(&amp;incorrectly_ordered(&amp;1, reqs))
    |&gt; Enum.map(&amp;order_correctly(&amp;1, reqs))
    |&gt; Enum.map(&amp;get_middle_page/1)
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&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="349050" 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/49">Post #48</a>
	                </div>
	            </div>
              <div id="likers-container-349050" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349050"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #48"></div>
  </section>
</div>
    <div class="postbit" id="349379" data-post-id="349379">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Like every year I’ve been following along with AOC at my own pace. Here’s my Livebook solution for Day 5: I tend to model the problems as structured data and then manipulate that data when possible. <a href="https://github.com/sdball/livebooks/blob/main/advent-of-code/2024/day05.livemd" class="inline-onebox" rel="noopener nofollow ugc">livebooks/advent-of-code/2024/day05.livemd at main · sdball/livebooks · GitHub</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="349379" 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/50">Post #49</a>
	                </div>
	            </div>
              <div id="likers-container-349379" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349379"
                     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 #49"></div>
  </section>
</div>
    <div class="postbit" id="349903" data-post-id="349903">
  <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>Not really been making an effort to keep up this year. Stuck on this one b/c my intuition is apparently wrong. My thought was use the rules to make an acyclic directed graph with edges where every left hand value is incident upon right hand value. Then checking the updates just means there has to be a path between each consecutive pair of page numbers. Works great for the example data but apparently none of the updates from the real input data meet this condition. Any insight into why my logic is wrong?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def do_order(graph, [[a,b] | rest]) do 
  :digraph.add_vertex(graph, a)
  :digraph.add_vertex(graph, b)
  :digraph.add_edge(graph, a, b)
  do_order(graph, rest)
end

def solve(rules, updates) do 
  updates
  |&gt; Enum.map(&amp;Enum.chunk_every(&amp;1, 2, 1, :discard))
  |&gt; Enum.filter(&amp;Enum.all?(&amp;1, fn [a,b] -&gt; :digraph.get_path(rules, a, b) end)
  |&gt; Enum.map(fn update -&gt; Enum.at(update, div(length(update), 2)) |&gt; hd() end)
  |&gt; Enum.sum()
end
</code></pre>
<p>EDIT:<br>
I’m apparently doing something wrong with the process of adding edges to the graph. There are 1176 rules in my data set but I end up with a graph that contains only 632 edges. I can’t figure out why …</p> 
	            </div>

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