2022-12-22 05:35:46 +00:00
|
|
|
map = []
|
|
|
|
moves = ""
|
|
|
|
with open("input") as f:
|
|
|
|
for line in f:
|
|
|
|
if line == "\n":
|
|
|
|
break
|
|
|
|
map.append(line.rstrip("\n"))
|
|
|
|
moves = f.readline().strip()
|
|
|
|
|
2022-12-23 04:56:46 +00:00
|
|
|
# 0 - E
|
|
|
|
# 1 - S
|
|
|
|
# 2 - W
|
|
|
|
# 3 - N
|
2022-12-22 05:35:46 +00:00
|
|
|
F = {
|
|
|
|
0: (+1, 0),
|
|
|
|
1: (0, +1),
|
|
|
|
2: (-1, 0),
|
|
|
|
3: (0, -1),
|
|
|
|
}
|
|
|
|
|
|
|
|
#assert len(set(len(l) for l in map)) == 1
|
|
|
|
|
|
|
|
class Map:
|
2022-12-23 04:56:46 +00:00
|
|
|
def __init__(self, map, region_size=50):
|
2022-12-22 05:35:46 +00:00
|
|
|
self.map = map
|
|
|
|
self.row = 0
|
2022-12-23 04:56:46 +00:00
|
|
|
self.col = 0
|
2022-12-22 05:35:46 +00:00
|
|
|
self.face = 0
|
2022-12-23 04:56:46 +00:00
|
|
|
self.region_size = region_size
|
|
|
|
|
|
|
|
while self.map[self.row][self.col] == ' ':
|
|
|
|
self.col += 1
|
2022-12-22 05:35:46 +00:00
|
|
|
|
2022-12-22 06:58:32 +00:00
|
|
|
# 9'
|
|
|
|
# 1 2
|
|
|
|
# \ 4 \
|
|
|
|
# 6 7
|
|
|
|
# 9 \
|
|
|
|
self.region_map = {
|
|
|
|
# R D L U
|
|
|
|
1: [(2,0), (4, 0), (6,2), (9, 1)],
|
|
|
|
4: [(2,-1), (7,0), (6,-1), (1, 0)],
|
|
|
|
7: [(2,2), (9,+1), (6,0), (4,0)],
|
|
|
|
6: [(7,0), (9,0), (1,2), (4,+1)],
|
|
|
|
9: [(7,-1), (2,0), (1,-1), (6,0)],
|
|
|
|
2: [(7,2), (4,+1), (1, 0), (9,0)],
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = True
|
|
|
|
for f in range(4):
|
|
|
|
for r in self.region_map:
|
|
|
|
orig = (r,f)
|
|
|
|
s = repr(orig)
|
|
|
|
for _ in range(4):
|
|
|
|
r, h = self.region_map[r][f]
|
|
|
|
f = (f+h)%4
|
|
|
|
s += "-> {} {}".format(r, f)
|
|
|
|
if (r,f) != orig:
|
|
|
|
print("!!", s)
|
|
|
|
ok = False
|
2022-12-23 04:56:46 +00:00
|
|
|
assert ok, "topology failure"
|
|
|
|
|
|
|
|
def set_simple_region_map(self):
|
|
|
|
R = self.region_size
|
|
|
|
def exists(x,y):
|
|
|
|
return 0 <= y < len(self.map) and 0 <= x < len(self.map[y]) and self.map[y][x] != ' '
|
|
|
|
for y in range(0,len(self.map), R):
|
|
|
|
for x in range(0, len(self.map[y]), R):
|
|
|
|
if exists(x,y):
|
|
|
|
warps = []
|
|
|
|
for f in range(4):
|
|
|
|
dx, dy = F[f]
|
|
|
|
dx, dy = dx*R, dy*R
|
|
|
|
x1, y1 = x+dx, y+dy
|
|
|
|
if exists(x1, y1):
|
|
|
|
dest = self.region(x1,y1)
|
|
|
|
else:
|
|
|
|
if dy:
|
|
|
|
y1 %= len(self.map)
|
|
|
|
while not exists(x1, y1):
|
|
|
|
y1 = (y1+dy)%len(self.map)
|
|
|
|
if dx:
|
|
|
|
x1 %= len(self.map[y1])
|
|
|
|
while not exists(x1, y1):
|
|
|
|
x1 = (x1+dx)%len(self.map[y1])
|
|
|
|
dest = self.region(x1, y1)
|
|
|
|
warps.append((dest, 0))
|
|
|
|
r = self.region(x,y)
|
|
|
|
self.region_map[r] = warps
|
|
|
|
print(self.region_map)
|
2022-12-22 06:58:32 +00:00
|
|
|
|
|
|
|
def nextTile(self, x, y, face):
|
2022-12-23 04:56:46 +00:00
|
|
|
R = self.region_size
|
2022-12-22 06:58:32 +00:00
|
|
|
dx, dy = F[face]
|
2022-12-23 04:56:46 +00:00
|
|
|
if 0 <= (x%R + dx) < R and 0 <= (y%R + dy) < R:
|
2022-12-22 06:58:32 +00:00
|
|
|
x, y = x+dx, y+dy
|
|
|
|
else:
|
2022-12-23 04:56:46 +00:00
|
|
|
r = self.region(x,y)
|
|
|
|
u = (x+dx) % R
|
|
|
|
v = (y+dy) % R
|
|
|
|
|
2022-12-22 06:58:32 +00:00
|
|
|
new_r, rotate = self.region_map[r][face]
|
|
|
|
|
|
|
|
if rotate == 0:
|
|
|
|
pass
|
|
|
|
elif rotate == -1:
|
2022-12-23 04:56:46 +00:00
|
|
|
u, v = v, R-u-1
|
2022-12-22 06:58:32 +00:00
|
|
|
elif rotate == 1:
|
2022-12-23 04:56:46 +00:00
|
|
|
u, v = R-v-1, u
|
2022-12-22 06:58:32 +00:00
|
|
|
elif rotate == 2:
|
2022-12-23 04:56:46 +00:00
|
|
|
u, v = R-u-1, R-v-1
|
|
|
|
x, y = self.to_abs(u, v, new_r)
|
2022-12-22 06:58:32 +00:00
|
|
|
face = (face + rotate) % 4
|
|
|
|
c = self.map[y][x]
|
|
|
|
assert c != ' '
|
|
|
|
return x, y, face, c
|
|
|
|
|
2022-12-23 04:56:46 +00:00
|
|
|
def to_abs(self, u, v, r):
|
|
|
|
"""returns the absolute coords of the local point (u,v) in region r"""
|
|
|
|
R = self.region_size
|
|
|
|
u %= R
|
|
|
|
v %= R
|
2022-12-22 06:58:32 +00:00
|
|
|
rx = r % 3
|
|
|
|
ry = r // 3
|
2022-12-23 04:56:46 +00:00
|
|
|
return (rx*R + u), (ry*R + v)
|
2022-12-22 05:35:46 +00:00
|
|
|
|
2022-12-23 04:56:46 +00:00
|
|
|
def region(self, x, y):
|
|
|
|
R = self.region_size
|
|
|
|
return y//R * 3 + x//R
|
2022-12-22 05:35:46 +00:00
|
|
|
|
|
|
|
def turnLeft(self):
|
|
|
|
self.face = (self.face - 1) % 4
|
|
|
|
def turnRight(self):
|
|
|
|
self.face = (self.face + 1) % 4
|
2022-12-23 04:56:46 +00:00
|
|
|
def walk(self, n, noclip=True):
|
|
|
|
x, y, face = self.col, self.row, self.face
|
2022-12-22 05:35:46 +00:00
|
|
|
while n > 0:
|
2022-12-23 04:56:46 +00:00
|
|
|
x,y,face,c = self.nextTile(x, y, face)
|
|
|
|
if c == '#' and noclip:
|
2022-12-22 05:35:46 +00:00
|
|
|
break
|
2022-12-23 04:56:46 +00:00
|
|
|
n -= 1
|
2022-12-22 05:35:46 +00:00
|
|
|
self.row = y
|
|
|
|
self.col = x
|
2022-12-23 04:56:46 +00:00
|
|
|
self.face = face
|
|
|
|
#print(self.pos())
|
2022-12-22 05:35:46 +00:00
|
|
|
|
|
|
|
def pos(self):
|
|
|
|
return self.row+1, self.col+1
|
|
|
|
|
|
|
|
def score(self):
|
|
|
|
r, c = self.pos()
|
2022-12-22 06:58:32 +00:00
|
|
|
return r*1000 + c*4 + self.face
|
2022-12-22 05:35:46 +00:00
|
|
|
|
2022-12-23 04:56:46 +00:00
|
|
|
#print(moves)
|
2022-12-22 05:35:46 +00:00
|
|
|
|
|
|
|
m = Map(map)
|
2022-12-23 04:56:46 +00:00
|
|
|
#m.set_simple_region_map()
|
2022-12-22 05:35:46 +00:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
while i < len(moves):
|
|
|
|
if moves[i].isdigit():
|
|
|
|
j = i+1
|
|
|
|
while j < len(moves) and moves[j].isdigit():
|
|
|
|
j += 1
|
|
|
|
n = int(moves[i:j])
|
|
|
|
m.walk(n)
|
|
|
|
i = j
|
|
|
|
elif moves[i] == 'L':
|
|
|
|
m.turnLeft()
|
|
|
|
i += 1
|
|
|
|
elif moves[i] == 'R':
|
|
|
|
m.turnRight()
|
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
print("invalid move", moves[i], "at", i)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
print(m.row, m.col)
|
|
|
|
print(m.score())
|