-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
72 lines (62 loc) · 1.73 KB
/
day14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from helpers.importHelpers import *
WIDTH = 101
HEIGHT = 103
class Robot:
def __init__(self, x, y, dx, dy):
self.x = int(x)
self.y = int(y)
self.dx = int(dx)
self.dy = int(dy)
def move(self, seconds=1):
self.x += self.dx * seconds
self.x %= WIDTH
self.y += self.dy * seconds
self.y %= HEIGHT
def quadrant(x, y):
if y < HEIGHT//2:
if x < WIDTH//2:
return 0
if x > WIDTH//2:
return 1
if y > HEIGHT//2:
if x < WIDTH//2:
return 2
if x > WIDTH//2:
return 3
return None
robots = []
for line in getInput().splitlines():
# pass format p=<int>,<int> v=<int>,<int>
pos, vel = line.split()
pos = pos.split("=")[1].split(",")
vel = vel.split("=")[1].split(",")
robots.append(Robot(*pos, *vel))
robotsInQuadrants = [0]*4
for robot in robots:
robot.move(100)
if (q := quadrant(robot.x, robot.y)) is not None:
robotsInQuadrants[q] += 1
part1 = 1
for i in range(4):
part1 *= robotsInQuadrants[i]
# Displaying only states where all robots are in different positions, quickly shows the christmas tree.
# Looking at it, the additional condition that at least 31 robots are in a row has been determined.
part2 = 100
while True:
for robot in robots:
robot.move()
part2 += 1
if len(set([(r.x, r.y) for r in robots])) == len(robots): # All robots are in different positions
if any([sum([1 for r in robots if r.y == y]) > 30 for y in range(HEIGHT)]): # At least 31 robots are in a row
break
# Print christmas tree:
robotPositions = set((r.x, r.y) for r in robots)
for y in range(HEIGHT):
for x in range(WIDTH):
if (x, y) in robotPositions:
print("#", end="")
else:
print(".", end="")
print()
print("Part 1: ", part1)
print("Part 2: ", part2)