add day 13 part 1
This commit is contained in:
117
day13/code.py
Normal file
117
day13/code.py
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
import sys
|
||||||
|
sys.path.append('../aoclib')
|
||||||
|
|
||||||
|
from aoclib import Input
|
||||||
|
import re
|
||||||
|
|
||||||
|
class Button:
|
||||||
|
def __init__(self, cost, x, y):
|
||||||
|
self.cost = cost
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'x: {self.x}, y: {self.y}, cost: {self.cost}'
|
||||||
|
|
||||||
|
|
||||||
|
class Prize:
|
||||||
|
def __init__(self, x, y):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f'x: {self.x}, y: {self.y}'
|
||||||
|
|
||||||
|
|
||||||
|
class Machine:
|
||||||
|
def __init__(self, a, b: Button, p: Prize):
|
||||||
|
self.a = a
|
||||||
|
self.b = b
|
||||||
|
self.prize = p
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
s = ''
|
||||||
|
s += 'B_A: ' + self.a.__str__() + '\n'
|
||||||
|
s += 'B_B: ' + self.b.__str__() + '\n'
|
||||||
|
s += 'Prize: ' + self.prize.__str__() + '\n'
|
||||||
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
def parse_input(lines: list[str]) -> list[Machine]:
|
||||||
|
lst = list()
|
||||||
|
brx = re.compile(r'X\+(\d+), Y\+(\d+)')
|
||||||
|
prx = re.compile(r'X=(\d+), Y=(\d+)')
|
||||||
|
|
||||||
|
b_a = None
|
||||||
|
b_b = None
|
||||||
|
m = None
|
||||||
|
for l in lines:
|
||||||
|
if l.startswith('Button A'):
|
||||||
|
m = brx.search(l)
|
||||||
|
b_a = Button(3, int(m.group(1)), int(m.group(2)))
|
||||||
|
if l.startswith('Button B'):
|
||||||
|
m = brx.search(l)
|
||||||
|
b_b = Button(1, int(m.group(1)), int(m.group(2)))
|
||||||
|
if l.startswith('Prize'):
|
||||||
|
m = prx.search(l)
|
||||||
|
p = Prize(int(m.group(1)), int(m.group(2)))
|
||||||
|
machine = Machine(b_a, b_b, p)
|
||||||
|
lst.append(machine)
|
||||||
|
|
||||||
|
return lst
|
||||||
|
|
||||||
|
def _solve1(m: Machine) -> int:
|
||||||
|
max_a = min(m.prize.x // m.a.x, m.prize.y // m.a.y)+1
|
||||||
|
max_b = min(m.prize.x // m.b.x, m.prize.y // m.b.y)+1
|
||||||
|
|
||||||
|
costs = []
|
||||||
|
for _a in range(max_a):
|
||||||
|
for _b in range(max_b):
|
||||||
|
x, y, cost = m.a.x*_a + m.b.x*_b, m.a.y*_a + m.b.y*_b, m.a.cost*_a + m.b.cost*_b
|
||||||
|
if x == m.prize.x and y == m.prize.y:
|
||||||
|
costs.append(cost)
|
||||||
|
|
||||||
|
if not costs:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return min(costs)
|
||||||
|
|
||||||
|
|
||||||
|
def solve1(lines: list[str]):
|
||||||
|
machines = parse_input(lines)
|
||||||
|
|
||||||
|
cost = 0
|
||||||
|
for m in machines:
|
||||||
|
c = _solve1(m)
|
||||||
|
if not c:
|
||||||
|
continue
|
||||||
|
cost += c
|
||||||
|
|
||||||
|
return cost
|
||||||
|
|
||||||
|
|
||||||
|
def solve2(lines: list[str]):
|
||||||
|
machines = parse_input(lines)
|
||||||
|
|
||||||
|
cost = 0
|
||||||
|
for m in machines[:1]:
|
||||||
|
continue
|
||||||
|
m.prize.x += 10000000000000
|
||||||
|
m.prize.y += 10000000000000
|
||||||
|
c = _solve1(m)
|
||||||
|
if not c:
|
||||||
|
continue
|
||||||
|
cost += c
|
||||||
|
|
||||||
|
return cost
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
#lines = Input('input_test.txt').lines()
|
||||||
|
lines = Input('input.txt').lines()
|
||||||
|
|
||||||
|
#part 1
|
||||||
|
print('part 1:', solve1(lines)) # 27157
|
||||||
|
|
||||||
|
#part 2
|
||||||
|
print('part 2:', solve2(lines)) # 862486
|
1279
day13/input.txt
Normal file
1279
day13/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
15
day13/input_test.txt
Normal file
15
day13/input_test.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
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
|
76
day13/task.txt
Normal file
76
day13/task.txt
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
--- Day 13: Claw Contraption ---
|
||||||
|
Next up: the lobby of a resort on a tropical island. The Historians take a moment to admire the hexagonal floor tiles before spreading out.
|
||||||
|
|
||||||
|
Fortunately, it looks like the resort has a new arcade! Maybe you can win some prizes from the claw machines?
|
||||||
|
|
||||||
|
The claw machines here are a little unusual. Instead of a joystick or directional buttons to control the claw, these machines have two buttons labeled A and B. Worse, you can't just put in a token and play; it costs 3 tokens to push the A button and 1 token to push the B button.
|
||||||
|
|
||||||
|
With a little experimentation, you figure out that each machine's buttons are configured to move the claw 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.
|
||||||
|
|
||||||
|
Each machine contains one prize; to win the prize, the claw must be positioned exactly above the prize on both the X and Y axes.
|
||||||
|
|
||||||
|
You wonder: 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 (your puzzle input). For example:
|
||||||
|
|
||||||
|
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
|
||||||
|
This list describes the button configuration and prize location of four different claw machines.
|
||||||
|
|
||||||
|
For now, consider just the first claw machine in the list:
|
||||||
|
|
||||||
|
Pushing the machine's A button would move the claw 94 units along the X axis and 34 units along the Y axis.
|
||||||
|
Pushing the B button would move the claw 22 units along the X axis and 67 units along the Y axis.
|
||||||
|
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 80*94 + 40*22 = 8400) and along the Y axis (because 80*34 + 40*67 = 5400). Doing this would cost 80*3 tokens for the A presses and 40*1 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?
|
||||||
|
|
||||||
|
Your puzzle answer was 27157.
|
||||||
|
|
||||||
|
The first half of this puzzle is complete! It provides one gold star: *
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
As you go to win the first prize, you discover that the claw is nowhere near where you expected it would be. Due to a unit conversion error in your measurements, the position of every prize is actually 10000000000000 higher on both the X and Y axis!
|
||||||
|
|
||||||
|
Add 10000000000000 to the X and Y position of every prize. After making this change, the example above would now look like this:
|
||||||
|
|
||||||
|
Button A: X+94, Y+34
|
||||||
|
Button B: X+22, Y+67
|
||||||
|
Prize: X=10000000008400, Y=10000000005400
|
||||||
|
|
||||||
|
Button A: X+26, Y+66
|
||||||
|
Button B: X+67, Y+21
|
||||||
|
Prize: X=10000000012748, Y=10000000012176
|
||||||
|
|
||||||
|
Button A: X+17, Y+86
|
||||||
|
Button B: X+84, Y+37
|
||||||
|
Prize: X=10000000007870, Y=10000000006450
|
||||||
|
|
||||||
|
Button A: X+69, Y+23
|
||||||
|
Button B: X+27, Y+71
|
||||||
|
Prize: X=10000000018641, Y=10000000010279
|
||||||
|
Now, it is only possible to win a prize on the second and fourth claw machines. Unfortunately, it will take many more than 100 presses to do so.
|
||||||
|
|
||||||
|
Using the corrected prize coordinates, 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?
|
||||||
|
|
||||||
|
|
Reference in New Issue
Block a user