day 14 part 2

main
magical 2024-12-14 06:00:19 +00:00
parent 7030b4529d
commit 35312d30d8
1 changed files with 42 additions and 1 deletions

View File

@ -32,6 +32,46 @@ def solve(file, W, H):
print(total)
def solve2(file, W, H):
robots = parse(file)
mq = float('inf')
for t in range(0, W*H):
rs = lambda: (iterate(r, t, W, H) for r in robots)
q = quads(rs(), W, H)
qs = q[0]*q[1]*q[2]*q[3]
if mq > qs:
mq = qs
if qs == mq:
picture = [["."]*W for _ in range(H)]
for (x,y),_ in rs():
if y < H:
picture[y][x] = "#"
for line in picture:
print("".join(line))
print("t = ", t, "q = ", qs)
#input(">")
# 442?
# 493
def quads(robots, W, H):
mx = W//2
my = H//2
q1, q2, q3, q4 = 0, 0, 0, 0
#seen = set()
for (x,y),_ in robots:
#if (x,y) in seen:
# continue
#seen.add((x,y))
if x < mx and y < my: q1 += 1
if x > mx and y < my: q2 += 1
if x < mx and y > my: q3 += 1
if x > mx and y > my: q4 += 1
return [q1, q2, q3, q4]
def iterate(robot, t, W, H):
(px,py), (vx,vy) = robot
px = (px+vx*t)%W
@ -39,4 +79,5 @@ def iterate(robot, t, W, H):
return (px,py), (vx,vy)
#solve(open("sample1.in"), 11, 7)
solve(open("input"), 101, 103)
#solve(open("input"), 101, 103)
solve2(open("input"), 101, 103)