day 22 python part 2

main
magical 2022-12-21 22:58:32 -08:00
parent 6960c73439
commit 77f02a8217
1 changed files with 79 additions and 8 deletions

View File

@ -16,7 +16,6 @@ F = {
#assert len(set(len(l) for l in map)) == 1 #assert len(set(len(l) for l in map)) == 1
class Map: class Map:
def __init__(self, map): def __init__(self, map):
self.map = map self.map = map
@ -30,14 +29,79 @@ class Map:
self.col = self.starts[0] self.col = self.starts[0]
self.face = 0 self.face = 0
def nextTile(self): # 9'
x,y = self.col, self.row # 1 2
f = self.face # \ 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
assert ok, "region topography failure"
def nextTile(self, x, y, face):
# 0 - E # 0 - E
# 1 - S # 1 - S
# 2 - W # 2 - W
# 3 - N # 3 - N
dx, dy = F[self.face] dx, dy = F[face]
r = self.region(x,y)
if 0 <= (x%50 + dx) < 50 and 0 <= (y%50 + dy) < 50:
x, y = x+dx, y+dy
else:
new_r, rotate = self.region_map[r][face]
x = (x+dx) % 50
y = (y+dy) % 50
if rotate == 0:
pass
elif rotate == -1:
x, y = y, 50-x-1
elif rotate == 1:
x, y = 50-y-1, x
elif rotate == 2:
x, y = 50-x-1, 50-y-1
x, y = self.in_region(x, y, new_r)
face = (face + rotate) % 4
c = self.map[y][x]
assert c != ' '
return x, y, face, c
@staticmethod
def in_region(x, y, r):
"""returns the absolute coords if the local (x,y) in region r"""
x %= 50
y %= 50
rx = r % 3
ry = r // 3
return (rx*50 + x), (ry*50 + y)
@staticmethod
def region(x, y):
return y//50 * 3 + x//50
def warp(x,y,face):
r = self.region(x,y)
s = 0
if dx: if dx:
x += dx x += dx
@ -66,14 +130,16 @@ class Map:
self.face = (self.face + 1) % 4 self.face = (self.face + 1) % 4
def walk(self, n): def walk(self, n):
while n > 0: while n > 0:
x,y,c = self.nextTile() x,y,f,c = self.nextTile(self.col, self.row, self.face)
if c == '#': if c == '#':
break break
pass#break
self.row = y self.row = y
self.col = x self.col = x
self.face = f
#self.map[y][x] = '>v<^'[self.face] #self.map[y][x] = '>v<^'[self.face]
n -= 1 n -= 1
print(self.row, self.col) print(self.row+1, self.col+1)
def pos(self): def pos(self):
return self.row+1, self.col+1 return self.row+1, self.col+1
@ -86,6 +152,11 @@ print(moves)
m = Map(map) m = Map(map)
for _ in range(4):
#m.walk(50)
print("---")
#blah
i = 0 i = 0
while i < len(moves): while i < len(moves):
if moves[i].isdigit(): if moves[i].isdigit():