<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="351776" data-post-id="351776">
  <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>Thought about rewriting part 1 to use Cramer’s rule like part 2 does but I quite like my comprehension use, actually, even if it’s not terribly efficient (part 2 finishes faster than part 1 lol). And no, I didn’t know Cramer’s rule. Reddit to the rescue again. I like it when I learn something new. Or something I may have known 30 years ago and forgot all about since linear algebra is not exactly prominent in my day to day life.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day13 do
  @re ~r/(?&lt;=[A|B|Prize][:|=] X[\+|\=])(\d*).*(?&lt;=^|Y[\+|\=])(\d*)/

  @real File.read!(__DIR__ &lt;&gt; "/input.txt") |&gt; String.trim()

  def run(input \\ @real) do
    data =
      input
      |&gt; parse()

    :timer.tc(fn -&gt; solve_1(data) end) |&gt; IO.inspect(label: :part_1)

    :timer.tc(fn -&gt; solve_2(data) end) |&gt; IO.inspect(label: :part_2)
  end

  defp parse(input) do
    input
    |&gt; String.split("\n\n", trim: true)
    |&gt; Enum.map(fn machine -&gt; Regex.scan(@re, machine) end)
    |&gt; Enum.map(fn machine -&gt;
      machine |&gt; Enum.map(fn line -&gt; line |&gt; tl() |&gt; Enum.map(&amp;String.to_integer/1) end)
    end)
  end

  defp solve_1(machines, limit \\ 100) do
    machines
    |&gt; Task.async_stream(fn machine -&gt; calc_min_cost(machine, limit) end, timeout: 10_000_000)
    |&gt; Enum.filter(fn {:ok, cost} -&gt; is_integer(cost) end)
    |&gt; Enum.sum_by(&amp;elem(&amp;1, 1))
  end

  @doc """
    [[ax, ay],[bx,by],[px,py]] = machine
    A * ax + B * bx == px AND A * ay + B * by == py
    solve for all values of A and B
    get_b = fn a -&gt; (px + py - a * (ax + ay)) / (bx + by) end
  """
  def calc_min_cost([[ax, ay], [bx, by], [px, py]], limit) do
    max_moves_a = min(div(px, ax), div(py, ay))
    max_moves_b = min(div(px, bx), div(py, by))

    for a &lt;- 0..min(limit, max_moves_a),
        b &lt;- 0..min(limit, max_moves_b),
        a * ax + b * bx == px,
        a * ay + b * by == py,
        reduce: :infinity do
      acc -&gt; min(3 * a + b, acc)
    end
  end

  defp solve_2(machines) do
    machines
    |&gt; Enum.map(fn [axay, bxby, [px, py]] -&gt;
      [axay, bxby, [px + 10_000_000_000_000, py + 10_000_000_000_000]]
    end)
    |&gt; Enum.map(&amp;cramers_rule/1)
    |&gt; Enum.filter(&amp;is_integer/1)
    |&gt; Enum.sum()
  end

  defp cramers_rule([[ax, ay], [bx, by], [px, py]]) do
    a = div(px * by - py * bx, ax * by - ay * bx)
    b = div(py * ax - px * ay, ax * by - ay * bx)

    if a * ax + b * bx == px and a * ay + b * by == py do
      3 * a + b
    else
      :infinity
    end
  end
end
</code></pre>
<p>I also finally decided to structure this day with the test data as an actual test that is run with <code>mix test</code>. Rather than running it as a script I’m compiling it and running from <code>iex</code> with <code>iex -S mix</code> and then calling the <code>Day13.run</code> function from there.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="351776" 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-13/68065/32">Post #31</a>
	                </div>
	            </div>
              <div id="likers-container-351776" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="351776"
                     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="351777" data-post-id="351777">
  <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>What is this pattern matching wizardry:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">&lt;&lt;ax::2-binary&gt;&gt; &lt;&gt; ", Y+" &lt;&gt; &lt;&lt;ay::2-binary&gt;&gt;
</code></pre>
<p>Also your explanation of the linear algebra concepts is fantastic. Thanks for sharing!</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>