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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes, it is the same, but because how the task works, there will be only <strong>one</strong> way to win each game, so the minimum will be at the same time the only way to win.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349814" 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/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-349814" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349814"
                     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 #21"></div>
  </section>
</div>
    <div class="postbit" id="349817" data-post-id="349817">
  <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>Used the same linear algebra approach as other solutions here, though I chose to always keep things as integers until they were confirmed to be exactly divisible.</p>
<p>Also uses <code>Regex.run</code> to parse a whole problem at once.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule TheClaw do
  @problem_format ~r/Button A: X\+(\d+), Y\+(\d+)\nButton B: X\+(\d+), Y\+(\d+)\nPrize: X=(\d+), Y=(\d+)/

  defstruct [:xa, :xb, :ya, :yb, :x0, :y0]

  def read(filename) do
    File.read!(filename)
    |&gt; String.split("\n\n")
    |&gt; Enum.map(&amp;Regex.run(@problem_format, &amp;1, capture: :all_but_first))
    |&gt; Enum.map(fn vs -&gt; Enum.map(vs, &amp;String.to_integer/1) end)
    |&gt; Enum.map(fn [xa, ya, xb, yb, x0, y0] -&gt;
      %__MODULE__{xa: xa, xb: xb, ya: ya, yb: yb, x0: x0, y0: y0}
    end)
  end

  def offset(c, off) do
    %{c | x0: c.x0 + off, y0: c.y0 + off}
  end

  def solution(c) do
    discriminant = c.xb*c.ya - c.xa*c.yb
    {
      discriminant,
      c.y0*c.xb - c.x0*c.yb,
      c.x0*c.ya - c.y0*c.xa
    }
  end

  def possible?({d, na, nb}) do
    rem(na, d) == 0 and rem(nb, d) == 0
  end

  @cost_a 3
  @cost_b 1

  def cost({d, na, nb}) do
    div(na * @cost_a + nb * @cost_b, d)
  end
end

TheClaw.read("input.txt")
|&gt; Enum.map(&amp;TheClaw.offset(&amp;1, 10000000000000))
|&gt; Enum.map(&amp;TheClaw.solution/1)
|&gt; Enum.filter(&amp;TheClaw.possible?/1)
|&gt; Enum.map(&amp;TheClaw.cost/1)
|&gt; Enum.sum()
|&gt; IO.inspect()
</code></pre> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="349818" data-post-id="349818">
  <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>dam! THANKS! It was just “off by one” issue .. removing greater than and using ===</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
  defp reached_target?(
         {presses_a, presses_b, _},
         {x_inc_a, y_inc_a},
         {x_inc_b, y_inc_b},
         {target_x, target_y}
       ) do
    current_x = presses_a * x_inc_a + presses_b * x_inc_b
    current_y = presses_a * y_inc_a + presses_b * y_inc_b
    current_x === target_x and current_y === target_y
  end
</code></pre>
<p>now i probably will need to go off into the linear algebra territory also …I just hate it when i cant use normal dynamic programming for part1</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="349821" data-post-id="349821">
  <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>Linear algebra tells us there is at most one solution to this system of equations (that it’s integer division doesn’t play into it). We can solve with a simple application of <a href="https://en.wikipedia.org/wiki/Cramer%27s_rule" rel="noopener nofollow ugc">Cramer’s rule</a> or any equivalent method.</p>
<p>LOC: 13</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Aoc2024.Day13 do
  import Enum
  def part1(file), do: main(file, 0)
  def part2(file), do: main(file, 10_000_000_000_000)
  def main(f, z), do: f |&gt; File.read!() |&gt; String.split("\n\n") |&gt; map(&amp;p/1) |&gt; sum_by(&amp;s(&amp;1, z))
  @r ~r/Button A: X\+(\d+), Y\+(\d+)\nButton B: X\+(\d+), Y\+(\d+)\nPrize: X=(\d+), Y=(\d+)/
  def p(s), do: Regex.scan(@r, s, capture: :all_but_first) |&gt; hd |&gt; map(&amp;String.to_integer/1)

  def s([ax, ay, bx, by, px, py], z) do
    {d, x, y} = {ax * by - ay * bx, (px + z) * by - bx * (py + z), ax * (py + z) - (px + z) * ay}
    if d == 0 or rem(x, d) != 0 or rem(y, d) != 0, do: 0, else: 3 * div(x, d) + div(y, d)
  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="349821" 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/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-349821" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349821"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #24"></div>
  </section>
</div>
    <div class="postbit" id="349822" data-post-id="349822">
  <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>Ah I see <a class="mention" href="/u/hauleth" rel="nofollow">@hauleth</a> beat me to this observation! An incident at work kept me from this puzzle until today.</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #25"></div>
  </section>
</div>
    <div class="postbit" id="349824" data-post-id="349824">
  <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>I solved part2 today with the use of linear algebra. But it was a blatant copy paste. I think this and maybe the previous day (12) marks the end of where i can use dynamic programming alone to solve the puzzles. Its a bit sad i had to resort to math, but hey advent of code cant just be Enum.reduce <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"></p>
<p>i solved myself some cognitive overhead by actually naming the variables to match what the function actually takes in .. still doesnt “make sense”, but its easier to following the x’s and y’s i guess ..</p>
<p>haha just found the spoiler functionality <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"> would be REALLY nice actually to avoid scrolling past posts to not get the answer thrown into your face <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"> haha ..</p>
<div class="spoiler">
<p>def solve_machine({{aX, aY}, {bX, bY}, {tX, tY}}) do<br>
x = (bY * tX - bX * tY) / (aX * bY - bX * aY)<br>
y = (aX * tY - aY * tX) / (aX * bY - bX * aY)</p>
<pre><code>if floor(x) == x and floor(y) == y do
  [trunc(x), trunc(y)]
end
</code></pre>
<p>end</p>
</div> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349824" 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/27">Post #26</a>
	                </div>
	            </div>
              <div id="likers-container-349824" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349824"
                     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 #26"></div>
  </section>
</div>
    <div class="postbit" id="349827" data-post-id="349827">
  <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 feel like I just took a college class. It’s been years since I visited linear algebra, but I’m gonna get a book on it cuz this was fun.</p>
<p>3-blue,1-brown’s series on linear algebra explains things in a visual way that makes a lot of sense. Not sure I understood it back in college as well as I had to for solving this problem. Didn’t help that we were forced to do pen/paper solutions. Kept me from seeing “the joy of linear algebra,” so to speak.</p>
<p>I noted down my thoughts as I wrestled with the concepts. If anyone would like to correct or add to my understanding, I’m all ears.</p>
<div class="spoiler">
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day13.Part2 do
  @moduledoc """
  This puzzle will be solved easier if I understand linear algebra.

  After speed-running 3-blue-1-brown's series on the essence of linear algebra
  (up to Cramer's rule) and wrestling with the concepts, here's what I've come up with
  """

  @a_cost 3
  @b_cost 1
  @adjustment 10_000_000_000_000

  defp parse_machine_args(machine_args) when is_list(machine_args) do
    machine_args
    |&gt; Enum.reduce(
      %{},
      fn arg, machine -&gt;
        {key, value} = parse_arg(arg)
        machine |&gt; Map.put(key, value)
      end
    )
  end

  defp parse_arg("Button A: " &lt;&gt; a_vector), do: {:a, parse_point(a_vector)}
  defp parse_arg("Button B: " &lt;&gt; b_vector), do: {:b, parse_point(b_vector)}

  defp parse_arg("Prize: " &lt;&gt; point) do
    {p_x, p_y} = parse_point(point)
    {:prize, {p_x + @adjustment, p_y + @adjustment}}
  end

  defp parse_point("X" &lt;&gt; rest) do
    ~r/\d+/
    |&gt; Regex.scan(rest)
    |&gt; List.flatten()
    |&gt; Enum.map(&amp;String.to_integer/1)
    |&gt; List.to_tuple()
  end

  defp parse(str) do
    str
    |&gt; String.split("\n")
    |&gt; Stream.chunk_by(&amp;(&amp;1 == ""))
    |&gt; Stream.filter(&amp;(&amp;1 != [""]))
    |&gt; Enum.map(&amp;parse_machine_args/1)
  end

  defp determinant_2d([[a, b], [c, d]]) do
    a * d - b * c
  end

  defp min_cost(%{a: {a_x, a_y}, b: {b_x, b_y}, prize: {c_1, c_2}} = _machine) do
    # a_xx + b_xy = c_1 &lt;- how many xs from button A + how many xs from button b to reach c_1?
    # a_yx + b_yy = c_2 &lt;- how many ys from button A + how many ys from button b to reach c_2?

    # 2x2 Square Matrix
    #  _        _  _ _     _   _
    # | a_x  b_x || x |   | c_1 |
    # | a_y  b_y || y | = | c_2 |
    #  -        -  - -     -   -

    # Target vector
    #
    # [c_1 c_2]
    #
    # This 1x2 matrix of constants can be seen as a vector.

    # target_vector = [c_1, c_2]

    # Free-variable vector
    #
    # [x y]
    #
    # This 1x2 matrix of free-variables can be seen as a vector.

    # Variable Transform
    #
    # a_x b_x
    # a_y b_y
    #
    # The variable transform is the matrix of constants that is multiplied by the
    # free-variable vector in the matrix-multiplication representation of the linear system.

    variable_transform = [[a_x, b_x], [a_y, b_y]]

    # Columns
    #
    # A = [a_x a_y] = i-hat
    # B = [b_x b_y] = j-hat
    #
    # Each column corresponds to the vector described by pressing a given button.

    # Basis Vectors
    #
    # Typically i-hat and j-hat in 2-d vector space. These represent the initial state of
    # vector space to which you would like to apply transformations.

    # The Determinant
    #
    # The determinant is a function of a square matrix that tells us a scalar value. This
    # value is equivalent to the area of the unit square once transformed by the matrix,
    # in relation to some basis vectors. This scalar can be used to determine the scale of
    # one matrix/transform in proportion to another.

    # Carmine's Rule
    #
    # Used to evaluate the constituent variables of a linear system when said linear system
    # is representable by a square matrix. The square nature of the matrix means that each
    # linear equation represents a vector orthogonal to the other n-1 linear equations in
    # n-dimensional space.

    # When dealing with such a linear system, we can find the values of its constituent
    # variables one-at-a-time by finding a ratio of determinants.

    # For each n-th variable, the denominator is the determinant of the variable transform.

    # For each n-th variable, the numerator is the determinant of the target transform.
    # A target transform is the matrix formed by taking the variable transform and replacing
    # the n-th column's constants (the vector described by pressing that column's button)
    # with the constants from the target vector. I.e. we replace the vector described by
    # [n_x n_y] with [c_1 c_2].

    target_transform_x = [[c_1, b_x], [c_2, b_y]]

    target_transform_y = [[a_x, c_1], [a_y, c_2]]

    # x = determinant(target_transform_x)/determinant(variable_transform)
    # y = determinant(target_transform_y)/determinant(variable_transform)

    # x = by how much do I have to multiply the determinant of the variable transform to reach the determinant of the target x (i-hat) transform?
    # y = by how much do I have to multiply the determinant of the variable transform to reach the determinant of the target y (j-hat) transform?

    target_determinant_x = determinant_2d(target_transform_x)
    target_determinant_y = determinant_2d(target_transform_y)
    variable_determinant = determinant_2d(variable_transform)

    # For this puzzle, we want to find all integer solutions for x and y, since a button can't be partially pressed.

    # If all target determinants are evenly divisible by the variable determinant, we've found an integer solution to the linear system.

    # For that reason, we can filter by rem(target_determinant / variable_determinant) == 0 for all target_determinants

    # Cases where determinant(variable_transform) is 0 can't be divided, so skip those machines. Visually,
    # this would mean that the vector space described by the variable transform already squeezes down to a line or dot (starts out ortholinear?).

    if variable_determinant != 0 and
         rem(target_determinant_x, variable_determinant) == 0 and
         rem(target_determinant_y, variable_determinant) == 0 do
      # In the case we've found an integer solution to the linear equation, we've found how many button presses of A and B we need.
      # Multiply each by its cost and report their sum to be added to the overarching accumulator
      div(target_determinant_x, variable_determinant) * @a_cost +
        div(target_determinant_y, variable_determinant) * @b_cost
    else
      # Add nothing to the accumulator
      0
    end
  end

  defp score_min_costs(machines) do
    machines
    |&gt; Task.async_stream(&amp;min_cost/1)
    |&gt; Enum.reduce(0, fn {:ok, min_cost}, acc -&gt;
      acc + min_cost
    end)
  end

  def solve() do
    File.read!("lib/advent_of_code/year/2024/day/13/input.txt")
    |&gt; parse()
    |&gt; score_min_costs()
    |&gt; IO.puts()
  end
end
</code></pre>
</div> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349827" 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/28">Post #27</a>
	                </div>
	            </div>
              <div id="likers-container-349827" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349827"
                     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 #27"></div>
  </section>
</div>
    <div class="postbit" id="349887" data-post-id="349887">
  <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>Here is my solution for Parts 1 &amp; 2 using the equation-solving approach.<br>
I didn’t enjoy this challenge very much, I prefer more algorithmic ones (and I struggled a bit at simplifying my equation <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>
<p>My code (more parsing than solving)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Advent.Y2024.Day13 do
  defmodule Machine do
    defstruct [:x, :y, :a, :b, :x1, :x2, :y1, :y2, :prize]
  end

  def run(puzzle) do
    puzzle
    |&gt; parse()
    |&gt; Enum.map(&amp;solve/1)
    |&gt; Enum.map(&amp; &amp;1.prize)
    |&gt; Enum.sum()
  end

  def parse(puzzle, error \\ 0) do
    puzzle
    |&gt; String.split("\n\n")
    |&gt; Enum.map(fn machine -&gt;
      [a_btn, b_btn, prize] = String.split(machine, "\n")
      [[_, x1, y1]] = Regex.scan(~r/Button A: X\+(\d+), Y\+(\d+)/, a_btn)
      [[_, x2, y2]] = Regex.scan(~r/Button B: X\+(\d+), Y\+(\d+)/, b_btn)
      [[_, x, y]] = Regex.scan(~r/Prize: X=(\d+), Y=(\d+)/, prize)

      %Machine{
        x: String.to_integer(x) + error,
        y: String.to_integer(y) + error,
        x1: String.to_integer(x1),
        x2: String.to_integer(x2),
        y1: String.to_integer(y1),
        y2: String.to_integer(y2)
      }
    end)
  end

  def solve(m = %Machine{x: x, y: y, x1: x1, x2: x2, y1: y1, y2: y2}) do
    a = (y - y2 / x2 * x) / (y1 - y2 * x1 / x2)
    b = (x - a * x1) / x2

    if Float.round(a, 3) == round(a) and Float.round(b, 3) == round(b) do
      %Machine{m | a: round(a), b: round(b), prize: round(3 * a + b)}
    else
      %Machine{m | prize: 0}
    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="349887" 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/29">Post #28</a>
	                </div>
	            </div>
              <div id="likers-container-349887" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349887"
                     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 #28"></div>
  </section>
</div>
    <div class="postbit" id="349904" data-post-id="349904">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<pre data-code-wrap="elixir"><code class="lang-elixir">#!/usr/bin/env elixir

# AoC 2024. day 13.

Mix.install([
  {:nx, "~&gt; 0.9.2"}
])

to_i = &amp;String.to_integer/1

###########################################################
# Part 1

start = System.monotonic_time(:microsecond)
File.stream!("../day13.txt")
|&gt; Stream.map(fn line -&gt; Regex.run(~r/^(?:Button (A|B): X\+(\d+), Y\+(\d+))|(?:Prize: X=(\d+), Y=(\d+)$)/, line, capture: :all_but_first) end)
|&gt; Enum.reduce({[], %{}}, fn
  nil, {lst, curr} -&gt; {[curr | lst], %{}}
  ["", "", "", n1, n2], {lst, curr} -&gt; {lst, Map.put(curr, :prize, {to_i.(n1), to_i.(n2)})}
  [what, n1, n2], {lst, curr} -&gt; {lst, Map.put(curr, String.to_atom(String.downcase(what)), {to_i.(n1), to_i.(n2)})}
end)
|&gt; then(fn {lst, curr} -&gt; Enum.map([curr | lst], fn %{a: {ax,ay}, b: {bx,by}, prize: {x,y}} -&gt;
    Nx.LinAlg.solve(Nx.tensor([[ax, bx], [ay, by]], type: {:f, 64}), Nx.tensor([x, y], type: {:f, 64}))
    |&gt; Nx.round()
    |&gt; Nx.as_type(:s64)
    |&gt; Nx.to_list()
    |&gt; then(fn [a,b] -&gt;
      if a*ax+b*bx==x &amp;&amp; a*ay+b*by==y do
        [a,b]
      else
        [-1,-1]
      end
    end)
  end) end)
|&gt; Enum.filter(fn [a,b] -&gt; a &gt;= 0 &amp;&amp; b &gt;= 0 &amp;&amp; a &lt;= 100 &amp;&amp; b &lt;= 100 end)
|&gt; Enum.map(fn [a,b] -&gt; a*3+b end)
|&gt; Enum.sum()
|&gt; IO.inspect(label: "Part 1")
elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"

###########################################################
# Part 2

start = System.monotonic_time(:microsecond)
File.stream!("../day13.txt")
|&gt; Stream.map(fn line -&gt; Regex.run(~r/^(?:Button (A|B): X\+(\d+), Y\+(\d+))|(?:Prize: X=(\d+), Y=(\d+)$)/, line, capture: :all_but_first) end)
|&gt; Enum.reduce({[], %{}}, fn
  nil, {lst, curr} -&gt; {[curr | lst], %{}}
  ["", "", "", n1, n2], {lst, curr} -&gt; {lst, Map.put(curr, :prize, {to_i.(n1)+10000000000000, to_i.(n2)+10000000000000})}
  [what, n1, n2], {lst, curr} -&gt; {lst, Map.put(curr, String.to_atom(String.downcase(what)), {to_i.(n1), to_i.(n2)})}
end)
|&gt; then(fn {lst, curr} -&gt; Enum.map([curr | lst], fn %{a: {ax,ay}, b: {bx,by}, prize: {x,y}} -&gt;
    Nx.LinAlg.solve(Nx.tensor([[ax, bx], [ay, by]], type: {:f, 64}), Nx.tensor([x, y], type: {:f, 64}))
    |&gt; Nx.round()
    |&gt; Nx.as_type(:s64)
    |&gt; Nx.to_list()
    |&gt; then(fn [a,b] -&gt;
      if a*ax+b*bx==x &amp;&amp; a*ay+b*by==y do
        [a,b]
      else
        [-1,-1]
      end
    end)
  end) end)
|&gt; Enum.filter(fn [a,b] -&gt; a &gt;= 0 &amp;&amp; b &gt;= 0 end)
|&gt; Enum.map(fn [a,b] -&gt; a*3+b end)
|&gt; Enum.sum()
|&gt; IO.inspect(label: "Part 2")
elapsed = System.monotonic_time(:microsecond) - start
IO.puts "Job done in #{elapsed} µs"
</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="349904" 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/30">Post #29</a>
	                </div>
	            </div>
              <div id="likers-container-349904" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349904"
                     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 #29"></div>
  </section>
</div>
    <div class="postbit" id="349979" data-post-id="349979">
  <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>Man that’s elegant. I tried similar but with float operations and conditions and such… complications and of course it didn’t work out for part 2; although I got correct results for part 1 input data. <img src="https://forum.elixirforum.com/images/emoji/apple/confused.png?v=15" title=":confused:" class="emoji" alt=":confused:" loading="lazy" width="20" height="20"><br>
Anyway, I’ll try to sear this into my memory:</p>
<blockquote>
<p>Solve the following equation system for integers (<strong>with integers</strong>)</p>
</blockquote> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="349979" 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/31">Post #30</a>
	                </div>
	            </div>
              <div id="likers-container-349979" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349979"
                     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 #30"></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/68065/load_more?page=4">Load more posts (2 remaining)</a>
</div></template></turbo-stream>