+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanDays 15-186e46de2

master
1.9 KiB66 linesraw
1from itertools import cycle
2
3shapes = (
4    ((0, 0), (1, 0), (2, 0), (3, 0)),
5    ((0, 1), (1, 0), (1, 1), (1, 2), (2, 1)),
6    ((0, 0), (1, 0), (2, 0), (2, 1), (2, 2)),
7    ((0, 0), (0, 1), (0, 2), (0, 3)),
8    ((0, 0), (0, 1), (1, 0), (1, 1)),
9)
10
11locked = set(shapes[0]) | {(4, 0), (5, 0)}
12translate = lambda p, d: tuple((x + d[0], y + d[1]) for x, y in p)
13valid = lambda piece: all(p not in locked and p[0] in range(7) for p in piece)
14highest = 0
15count = 0
16cur = translate(shapes[0], (2, 4))
17
18# def show():
19#     for y in range(1, highest + 5)[::-1]:
20#         for x in range(7):
21#             c = "."
22#             if (x, y) in locked:
23#                 c = "#"
24#             if (x, y) in cur:
25#                 c = "@"
26#             print(c, end="")
27#         print()
28#     print()
29
30heights = [0]
31for d in cycle(open("input").read()[:-1]):
32    trans = translate(cur, ((d == ">") * 2 - 1, 0))
33    if valid(trans):
34        cur = trans
35    drop = translate(cur, (0, -1))
36    if valid(drop):
37        cur = drop
38    else:
39        for p in cur:
40            locked.add(p)
41        count += 1
42        highest = max(highest, max(y for _, y in cur))
43        heights.append(highest)
44        if count == 2022:
45            print(highest)
46        if count == 10000:
47            break
48        cur = translate(shapes[count % 5], (2, highest + 4))
49diffs = [heights[i + 1] - heights[i] for i in range(len(heights) - 1)]
50
51left, right = 100, 101  # in case the start isn't periodic cuz of the floor?
52while right < 10000:
53    if diffs[left] == diffs[right]:
54        for i in range(5000):
55            if diffs[left + i] != diffs[right + i]:
56                break
57        else:
58            print("cycle detected with period", period := right - left)
59            break
60    right += 1
61
62goal = 1_000_000_000_000
63cyclediff = sum(diffs[left:right])
64repeats = (goal - left) // period
65togo = goal - (left + repeats * (right - left))
66print(heights[left] + cyclediff * repeats + sum(diffs[left : left + togo]))