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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
import sys
infile = sys.argv[1] if len(sys.argv) > 1 else "data/22_2022.txt"
data = open(infile).read()
lines = [x for x in data.split("\n")]
G, instr = data.split("\n\n")
G = G.split("\n")
instr = instr.strip()
D = [(-1, 0), (0, 1), (1, 0), (0, -1)]
R = len(G)
C = len(G[0])
for r in range(R):
while len(G[r]) < C:
G[r] += " "
assert len(G[r]) == C
CUBE = C // 3
assert CUBE == R // 4
REGION = [(0, 1), (0, 2), (1, 1), (2, 1), (2, 0), (3, 0)]
def regionToGlobal(r, c, region):
rr, cc = REGION[region - 1]
return (rr * CUBE + r, cc * CUBE + c)
def getRegion(r, c):
for i, (rr, cc) in enumerate(REGION):
if rr * CUBE <= r < (rr + 1) * CUBE and cc * CUBE <= c < (cc + 1) * CUBE:
return (i + 1, r - rr * CUBE, c - cc * CUBE)
assert False, (r, c)
def newCoords(r, c, d, nd):
if d == 0:
assert r == 0
x = c
if d == 1:
assert c == CUBE - 1
x = r
if d == 2:
assert r == CUBE - 1
x = CUBE - 1 - c
if d == 3:
assert c == 0
x = CUBE - 1 - r
if nd == 0:
return (CUBE - 1, x)
if nd == 1:
return (x, 0)
if nd == 2:
return (0, CUBE - 1 - x)
if nd == 3:
return (CUBE - 1 - x, CUBE - 1)
def getDest(r, c, d, part):
if part == 1:
r = (r + D[d][0]) % R
c = (c + D[d][1]) % C
while G[r][c] == " ":
r = (r + D[d][0]) % R
c = (c + D[d][1]) % C
return (r, c, d)
region, rr, rc = getRegion(r, c)
newRegion, nd = {
(4, 0): (3, 0),
(4, 1): (2, 3),
(4, 2): (6, 3),
(4, 3): (5, 3),
(1, 0): (6, 1),
(1, 1): (2, 1),
(1, 2): (3, 2),
(1, 3): (5, 1),
(3, 0): (1, 0),
(3, 1): (2, 0),
(3, 2): (4, 2),
(3, 3): (5, 2),
(6, 0): (5, 0),
(6, 1): (4, 0),
(6, 2): (2, 2),
(6, 3): (1, 2),
(2, 0): (6, 0),
(2, 1): (4, 3),
(2, 2): (3, 3),
(2, 3): (1, 3),
(5, 0): (3, 1),
(5, 1): (4, 1),
(5, 2): (6, 2),
(5, 3): (1, 1),
}[(region, d)]
nr, nc = newCoords(rr, rc, d, nd)
assert 0 <= nr < CUBE and 0 <= nc < CUBE
nr, nc = regionToGlobal(nr, nc, newRegion)
assert G[nr][nc] in [".", "#"], f"{G[nr][nc]}"
return (nr, nc, nd)
def solve(part):
r = 0
c = 0
d = 1
while G[r][c] != ".":
c += 1
i = 0
while i < len(instr):
n = 0
while i < len(instr) and instr[i].isdigit():
n = n * 10 + int(instr[i])
i += 1
for _ in range(n):
assert G[r][c] == ".", (r, c)
rr = (r + D[d][0]) % R
cc = (c + D[d][1]) % C
if G[rr][cc] == " ":
(nr, nc, nd) = getDest(r, c, d, part)
if G[nr][nc] == "#":
break
(r, c, d) = (nr, nc, nd)
continue
elif G[rr][cc] == "#":
break
else:
r = rr
c = cc
if i == len(instr):
break
turn = instr[i]
if turn == "L":
d = (d + 3) % 4
elif turn == "R":
d = (d + 1) % 4
else:
assert False, (i, instr[i:], instr[i])
i += 1
DV = {0: 3, 1: 0, 2: 1, 3: 2}
return (r + 1) * 1000 + (c + 1) * 4 + DV[d]
print(solve(1))
print(solve(2))
|