Skip to content

Instantly share code, notes, and snippets.

@object
Last active December 14, 2024 14:22
Show Gist options
  • Save object/c69d0bf44b20cc9b1edb7a9bab5b57d2 to your computer and use it in GitHub Desktop.
Save object/c69d0bf44b20cc9b1edb7a9bab5b57d2 to your computer and use it in GitHub Desktop.
Advent of Code 2024, day 14
import re
with open("./data/input14.txt") as inputFile:
input = inputFile.read().splitlines()
init = []
for line in input:
m = re.findall(r'p=(-?\d+),(-?\d+) v=(-?\d+),(-?\d+)', line)
p = (int(m[0][0]), int(m[0][1]))
v = (int(m[0][2]), int(m[0][3]))
init.append((p, v))
size = (101,103)
robots = init.copy()
def move(robots):
for n in range(len(robots)):
(x,y),(vx,vy) = robots[n]
x = (x + vx) % size[0]
y = (y + vy) % size[1]
robots[n] = (x,y),(vx,vy)
return robots
# Part 1
for n in range(100):
robots = move(robots)
counts = [0,0,0,0]
for robot in robots:
x,y = robot[0]
if x < size[0] // 2:
if y < size[1] // 2:
counts[0] += 1
elif y > size[1] // 2:
counts[1] += 1
elif x > size[0] // 2:
if y < size[1] // 2:
counts[2] += 1
elif y > size[1] // 2:
counts[3] += 1
res = 1
for c in counts: res *= c
print(res)
# Part 2
def has_no_overlaps(robs):
s = set()
for robot in robs:
pos = robot[0]
if pos in s:
return False
else:
s.add(pos)
return True
def draw(robots):
grid = set()
for robot in robots:
x, y = robot[0]
grid.add((x,y))
for y in range(size[1]):
line = ''
for x in range(size[0]):
if (x,y) in grid:
line += '#'
else:
line += '.'
print(line)
res = 0
robots = init.copy()
while(True):
if has_no_overlaps(robots):
break
robots = move(robots)
res += 1
print(res)
draw(robots)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment