generated from alvesvaren/AoC-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.py
More file actions
88 lines (74 loc) · 2.27 KB
/
17.py
File metadata and controls
88 lines (74 loc) · 2.27 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from aoc import get_input
rocks = [
[0, 1, 2, 3],
[1, 1j, 1 + 1j, 2 + 1j, 1 + 2j],
[0, 1, 2, 2 + 1j, 2 + 2j],
[0, 1j, 2j, 3j],
[0, 1, 1j, 1 + 1j],
]
data = [1 if char == ">" else -1 for char in get_input(17)]
solid = {x - 1j for x in range(7)}
height = 0
seen = {}
def summarize():
o = [-20] * 7
for x in solid:
r = int(x.real)
i = int(x.imag)
o[r] = max(o[r], i)
top = max(o)
return tuple(x - top for x in o)
rockCount = 0
rockID = 0
rock = {x + 2 + (height + 3) * 1j for x in rocks[rockID]}
while rockCount < 2022:
for jet in data:
moved = {x + jet for x in rock}
if all(0 <= x.real < 7 for x in moved) and not (moved & solid):
rock = moved
moved = {x - 1j for x in rock}
if moved & solid:
solid |= rock
rockCount += 1
height = max(x.imag for x in solid) + 1
if rockCount >= 2022:
break
rockID = (rockID + 1) % 5
rock = {x + 2 + (height + 3) * 1j for x in rocks[rockID]}
else:
rock = moved
print(int(height))
solid = {x - 1j for x in range(7)}
height = 0
seen = {}
rockCount = 0
rockID = 0
rock = {x + 2 + (height + 3) * 1j for x in rocks[rockID]}
T = 1000000000000
while rockCount < T:
for ji, jet in enumerate(data):
moved = {x + jet for x in rock}
if all(0 <= x.real < 7 for x in moved) and not (moved & solid):
rock = moved
moved = {x - 1j for x in rock}
if moved & solid:
solid |= rock
rockCount += 1
o = height
height = max(x.imag for x in solid) + 1
if rockCount >= T:
break
rockID = (rockID + 1) % 5
rock = {x + 2 + (height + 3) * 1j for x in rocks[rockID]}
key = (ji, rockID, summarize())
if key in seen:
lastRockCount, lastHeight = seen[key]
rem = T - rockCount
rep = rem // (rockCount - lastRockCount)
offset = rep * (height - lastHeight)
rockCount += rep * (rockCount - lastRockCount)
seen = {}
seen[key] = (rockCount, height)
else:
rock = moved
print(int(height + offset))