Skip to content

Instantly share code, notes, and snippets.

@object
Created December 13, 2024 07:11
Show Gist options
  • Save object/8f0954ab58017108e7dea209cd3172d9 to your computer and use it in GitHub Desktop.
Save object/8f0954ab58017108e7dea209cd3172d9 to your computer and use it in GitHub Desktop.
Advent of Code 2024, day 13
from itertools import groupby
import re
with open("./data/input13.txt") as inputFile:
input = inputFile.read().splitlines()
groups = [list(sub) for is_empty, sub in groupby(input, key = lambda x: x == '') if not is_empty]
machines = []
for group in groups:
machine = {}
m = re.findall(r'Button (\w+): X\+(\d+), Y\+(\d+)', group[0])
machine['A'] = {'X': int(m[0][1]), 'Y': int(m[0][2])}
m = re.findall(r'Button (\w+): X\+(\d+), Y\+(\d+)', group[1])
machine['B'] = {'X': int(m[0][1]), 'Y': int(m[0][2])}
m = re.findall(r'Prize: X=(\d+), Y=(\d+)', group[2])
machine['C'] = {'X': int(m[0][0]), 'Y': int(m[0][1])}
machines.append(machine)
def compute_tokens(machines):
sum = 0
for m in machines:
xnom = m['B']['Y'] * m['C']['X'] - m['B']['X'] * m['C']['Y']
xdem = m['B']['Y'] * m['A']['X'] - m['B']['X'] * m['A']['Y']
ynom = m['A']['Y'] * m['C']['X'] - m['A']['X'] * m['C']['Y']
ydem = m['B']['X'] * m['A']['Y'] - m['B']['Y'] * m['A']['X']
if xnom % xdem == 0 and ynom % ydem == 0:
x = xnom // xdem
y = ynom // ydem
sum += 3*x + y
return sum
# Part 1
print(compute_tokens(machines))
# Part 2
for m in machines:
m['C']['X'] = m['C']['X'] + 10000000000000
m['C']['Y'] = m['C']['Y'] + 10000000000000
print(compute_tokens(machines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment