-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.py
109 lines (92 loc) · 2.52 KB
/
17.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from lib import *
input = read_input(2022, 17).strip()
SHAPES = [
[[1, 1, 1, 1]],
[[0, 1, 0], [1, 1, 1], [0, 1, 0]],
[[0, 0, 1], [0, 0, 1], [1, 1, 1]],
[[1], [1], [1], [1]],
[[1, 1], [1, 1]],
]
WIDTH = 7
START_X = 2
START_Y = 3
def part1():
grid = set()
height = 0
sh = 0
st = 0
def test(s, x, y):
return (
y - len(s) >= -1
and x >= 0
and x + len(s[0]) <= WIDTH
and all((y - i, x + j) not in grid for i, r in enumerate(s) for j, k in enumerate(r) if k)
)
def add(s, x, y):
grid.update((y - i, x + j) for i, r in enumerate(s) for j, k in enumerate(r) if k)
for _ in range(2022):
s = SHAPES[sh]
sh = (sh + 1) % len(SHAPES)
x = START_X
y = height + START_Y + len(s) - 1
while True:
d = {"<": -1, ">": 1}[input[st]]
st = (st + 1) % len(input)
if test(s, x + d, y):
x += d
if test(s, x, y - 1):
y -= 1
else:
add(s, x, y)
height = max(y + 1, height)
break
return height
def part2():
grid = set()
height = 0
st = 0
sh = 0
def test(s, x, y):
return (
y - len(s) >= -1
and x >= 0
and x + len(s[0]) <= WIDTH
and all((y - i, x + j) not in grid for i, r in enumerate(s) for j, k in enumerate(r) if k)
)
def add(s, x, y):
grid.update((y - i, x + j) for i, r in enumerate(s) for j, k in enumerate(r) if k)
seen = []
idx = {}
heights = []
round = 0
while True:
k = sh, st
seen.append(k)
heights.append(height)
if k in idx:
l = len(seen) - idx[k] - 1
if seen[-l:] == seen[-2 * l : -l]:
break
idx[k] = round
s = SHAPES[sh]
sh = (sh + 1) % len(SHAPES)
x = START_X
y = height + START_Y + len(s) - 1
while True:
d = {"<": -1, ">": 1}[input[st]]
st = (st + 1) % len(input)
if test(s, x + d, y):
x += d
if test(s, x, y - 1):
y -= 1
else:
add(s, x, y)
height = max(y + 1, height)
break
round += 1
left = 1000000000000 - round
height += left // l * (heights[-1] - heights[-l - 1])
height += heights[-l - 1 + left % l] - heights[-l - 1]
return height
print(part1())
print(part2())