You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
#!/usr/bin/env python3
|
|
|
|
import sys, re
|
|
|
|
|
|
def show(mp):
|
|
for line in mp:
|
|
print(''.join(line))
|
|
|
|
|
|
def easter_egg(robots, w, h):
|
|
for steps in range(7000, 10000):
|
|
print('Steps: ', steps)
|
|
m = [['.'] * w for _ in range(h)]
|
|
for r in robots:
|
|
px, py, vx, vy = map(int, r)
|
|
nx = (px + steps*vx)%w
|
|
ny = (py + steps*vy)%h
|
|
m[ny][nx] = 'X'
|
|
show(m)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
robot = re.compile(r'p=(?P<px>-?\d+),(?P<py>-?\d+) v=(?P<vx>-?\d+),(?P<vy>-?\d+)')
|
|
robots = [robot.search(line.strip('\n')).groups() for line in open(sys.argv[1])]
|
|
|
|
# space: 11 x 7 for example and 101 x 103 for real input
|
|
w, h = 101, 103
|
|
steps = 100
|
|
q1, q2, q3, q4 = 0,0,0,0
|
|
for r in robots:
|
|
px, py, vx, vy = map(int,r)
|
|
nx = (px + 100*vx)%w
|
|
ny = (py+100*vy)%h
|
|
if nx in range(0, w//2):
|
|
if ny in range(0,h//2): q1 += 1
|
|
if ny in range(h//2+1,h): q2 += 1
|
|
if nx in range(w//2+1,w):
|
|
if ny in range(0,h//2): q3 += 1
|
|
if ny in range(h//2+1,h): q4 += 1
|
|
|
|
# challenge 1
|
|
res1 = q1 * q2 * q3 * q4
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
easter_egg( robots, w, h)
|
|
res2 = "8006" # empirical solution
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|