Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Advent of Code - Day 13

livebooks/day13.livemd

Advent of Code - Day 13

Part 1

raw_sample = """
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400

Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176

Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450

Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
"""
defmodule Part1 do
  def parse_input(input) do
    input
    |> String.split("\n\n")
    |> Enum.map(fn x -> String.split(x, "\n", trim: true) end)
  end

  # find minimal combination matching the prize, multiply by cost per button mash
  
end

It costs 3 tokens to push the A button and 1 token to push the B button.

…move a specific amount to the right (along the X axis) and a specific amount forward (along the Y axis) each time that button is pressed.

The input: What is the smallest number of tokens you would have to spend to win as many prizes as possible? You assemble a list of every machine’s button behavior and prize location

For now, consider just the first claw machine in the list:

  1. Pushing the machine’s A button would move the claw 94 units along the X axis and 34 units along the Y axis.
  2. Pushing the B button would move the claw 22 units along the X axis and 67 units along the Y axis.
  3. The prize is located at X=8400, Y=5400; this means that from the claw’s initial position, it would need to move exactly 8400 units along the X axis and exactly 5400 units along the Y axis to be perfectly aligned with the prize in this machine.

The cheapest way to win the prize is by pushing the A button 80 times and the B button 40 times. This would line up the claw along the X axis (because 8094 + 4022 = 8400) and along the Y axis (because 8034 + 4067 = 5400). Doing this would cost 803 tokens for the A presses and 401 for the B presses, a total of 280 tokens.

For the second and fourth claw machines, there is no combination of A and B presses that will ever win a prize.

For the third claw machine, the cheapest way to win the prize is by pushing the A button 38 times and the B button 86 times. Doing this would cost a total of 200 tokens.

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.

You estimate that each button would need to be pressed no more than 100 times to win a prize. How else would someone be expected to play?

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?

Part1.parse_input(raw_sample)