Day 14 - Advent of Code 2024
14 December 2024
Working solutions for the day 14 puzzles.
Part One
""" day_14_01.py """
# usage: python3 day_14_01.py 101 103 < input
import re
import sys
from math import prod
def move(across, down, bots):
""" move all bots """
state = {}
for i, j in bots.items():
state[i] = [(j[0] + j[2]) % across, (j[1] + j[3]) % down, j[2], j[3]]
return state
def quadrant(across, down, x, y):
""" which quadrant """
if across - 1 - x == x:
return None
if down - 1 - y == y:
return None
xmid, ymid = across / 2, down / 2
if x < xmid and y < ymid:
return 0
if x > xmid and y < ymid:
return 1
if x < xmid:
return 2
return 3
width, height = int(sys.argv[1]), int(sys.argv[2])
robots = {}
with sys.stdin as infile:
for num, line in enumerate(infile):
robots[num] = [int(i) for i in re.findall(r'-?\d+', line)]
for _ in range(100):
robots = move(width, height, robots)
quadrants = [0, 0, 0, 0]
for xtile, ytile, _, _ in robots.values():
if (index := quadrant(width, height, xtile, ytile)) is not None:
quadrants[index] += 1
print(prod(quadrants))
Part Two
""" day_14_02.py """
# usage: python3 day_14_02.py 101 103 < input
import re
import sys
def move(across, down, bots):
""" move all bots """
state = {}
for i, j in bots.items():
state[i] = [(j[0] + j[2]) % across, (j[1] + j[3]) % down, j[2], j[3]]
return state
def text(across, down, bots):
""" visualise robots """
points = [(x, y) for x, y, _, _ in bots.values()]
out = ''
for y in range(down):
for x in range(across):
out += 'X' if (x, y) in points else '.'
out += '\n'
return out + '\n'
width, height = int(sys.argv[1]), int(sys.argv[2])
robots = {}
with sys.stdin as infile:
for num, line in enumerate(infile):
robots[num] = [int(i) for i in re.findall(r'-?\d+', line)]
seconds = 0
while True:
if len(set((x, y) for x, y, _, _ in robots.values())) == 500:
# print(text(width, height, robots))
break
robots = move(width, height, robots)
seconds += 1
print(seconds)