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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I think that the infinite solns problems would always have the nature that the two lines described by the rows of the matrices would be parallel. Meaning “they never cross”, hence the determinant is 0. The only way to get a solution, therefore, is if both the X and Y coordinate equations are right on top of each other. So, then you only need to hit as many A’s as you need to to get to an even multiple of B distance, and then use B from then on out. It wouldn’t be hard to solve, sort of a remainder type problem. But that would have added another twist. One twist too many perhaps, as you pointed 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="349688" 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/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-349688" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349688"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>My solution:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Parsing
puzzles =
  puzzle_input
  |&gt; String.split("\n", trim: true)
  |&gt; Enum.chunk_every(3)
  |&gt; Enum.map(fn machine -&gt;
    Enum.map(machine, fn row -&gt;
      [[_patternx, x], [_patterny, y]] = Regex.scan(~r/[XY][+=](\d+)/, row)

      {String.to_integer(x), String.to_integer(y)}
    end)
  end)
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ClawContraption do
  # ax * x + bx * y = px
  # ay * x + by * y = py
  def solve([{ax, ay}, {bx, by}, {px, py}]) do
    case ax * by - bx * ay do
      0 -&gt;
        0

      determinant -&gt;
        num_x = px * by - bx * py
        num_y = ax * py - px * ay

        if rem(num_x, determinant) == 0 and rem(num_y, determinant) == 0 do
          x = div(num_x, determinant)
          y = div(num_y, determinant)

          {x, y}
        else
          0
        end
    end
  end

  def calculate_cost({x, y}), do: x * 3 + y
  def calculate_cost(_result), do: 0
end
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir"># Puzzle 1
Enum.map(puzzles, fn puzzle -&gt;
  puzzle
  |&gt; ClawContraption.solve()
  |&gt; ClawContraption.calculate_cost()
end)
|&gt; Enum.sum()

# Puzzle 2
Enum.map(puzzles, fn puzzle -&gt;
  [{ax, ay}, {bx, by}, {px, py}] = puzzle
  
  [{ax, ay}, {bx, by}, {px + 10_000_000_000_000, py + 10_000_000_000_000}]
  |&gt; ClawContraption.solve()
  |&gt; ClawContraption.calculate_cost()
end)
|&gt; Enum.sum()
</code></pre>
<p>Full code:</p>
<p><a href="https://github.com/Flo0807/adventofcode/blob/main/2024/13.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/Flo0807/adventofcode/blob/main/2024/13.livemd</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="349691" 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/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-349691" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349691"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I liked that you didn’t introduce any floating point math.</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="349694" data-post-id="349694">
  <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>Simple Cramer’s rule for solving linear equations, and some small writeup about it.</p>
<p><a href="https://github.com/hauleth/advent-of-code/blob/master/2024/day13.livemd" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/hauleth/advent-of-code/blob/master/2024/day13.livemd</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="349694" data-batch-url="/posts/batch_likers">
                        4
                      </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/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-349694" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="349694"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Love that you have notes in your livemd!</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="349724" data-post-id="349724">
  <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>The async stream and the cartesian iteration limit for j were a bit extra, but it works for part 1:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Day13.Part1 do
  @max_presses 100
  @a_cost 3
  @b_cost 1

  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: {:prize, parse_point(point)}

  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 treasure_maps(%{a: {x_a, y_a}, b: {x_b, y_b}, prize: {target_x, target_y}} = _machine) do
    for i &lt;- 0..@max_presses do
      leftover_x = target_x - (x_a * i)
      js_that_can_fit_x = div(leftover_x, x_b)
      j_limit = min(target_x, js_that_can_fit_x)
      leftover_y = target_y - (y_a * i)
      js_that_can_fit_y = div(leftover_y, y_b)
      j_limit = min(j_limit, js_that_can_fit_y)
      j_limit = min(j_limit, @max_presses)
      for j &lt;- 0..j_limit do
        x_combined = i * x_a + j * x_b
        y_combined = i * y_a + j * y_b

        {x_combined, y_combined, i, j}
      end
      |&gt; Enum.filter(fn {x, y, _i, _j} -&gt; x == target_x and y == target_y end)
    end
    |&gt; Enum.flat_map(&amp; &amp;1)
  end

  defp min_cost(machine) do
    case treasure_maps(machine) do
      [] -&gt;
        0

      hits -&gt;
        hits
        |&gt; Enum.map(fn {_x, _y, a, b} -&gt; a * @a_cost + b * @b_cost end)
        |&gt; Enum.min()
    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>
<p>Still working on part 2 =]</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="349725" data-post-id="349725">
  <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>Oof- yeah, I need to review ~matrices~ linear algebra in general. <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> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="349774" data-post-id="349774">
  <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>Lets hope day 13 is not evil and destroys my brain <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"> like day12</p>
<p>I got the example input with four machines to work. My code returns 480 for that input</p>
<p>but it seems its returning a false cost for the actual input. Which tells me i need to go back to the drawing board</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="349805" data-post-id="349805">
  <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>Nope. Brain failing to understand. IF I understand the requirements correctly, the first example that cost 480 tokens should be a minimal example for the bigger answer. Is that a wrong assumption? I might not able to parse the English in the text, even though I’m pretty fluent.</p>
<p>da code:</p>
<p><a href="https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_13/Part1.ex#L33" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/jarlah/advent_of_code/blob/master/lib/2024/day_13/Part1.ex#L33</a></p>
<p>Hints?</p>
<p>for ex is this</p>
<p><code>So, the most prizes you could possibly win is two; the minimum tokens you would have to spend to win all (two) prizes is `480`.</code></p>
<p>the same as this</p>
<p><code>Figure out how to win as many prizes as possible. *What is the fewest tokens you would have to spend to win all possible prizes?</code></p>
<p><img src="https://forum.elixirforum.com/images/emoji/apple/thinking.png?v=15" title=":thinking:" class="emoji only-emoji" alt=":thinking:" loading="lazy" width="20" height="20"></p> 
	            </div>

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

    </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You need to check your <code>reached_target?</code> function, the logic there looks wrong.</p>
<p>At the moment your only “don’t win” case is if you reach 100 presses without winning.</p> 
	            </div>

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

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #20"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/68065/load_more?page=3">Load more posts (12 remaining)</a>
</div></template></turbo-stream>