Day 22 part 1.
This commit is contained in:
parent
b34b8fce66
commit
82357f660e
69
d22.py
Normal file
69
d22.py
Normal file
@ -0,0 +1,69 @@
|
||||
from lib import *
|
||||
|
||||
EXAMPLE = """ ...#
|
||||
.#..
|
||||
#...
|
||||
....
|
||||
...#.......#
|
||||
........#...
|
||||
..#....#....
|
||||
..........#.
|
||||
...#....
|
||||
.....#..
|
||||
.#......
|
||||
......#.
|
||||
|
||||
10R5L5R10L4R5L5"""
|
||||
|
||||
|
||||
DIRS = [
|
||||
(0, 1),
|
||||
(1, 0),
|
||||
(0, -1),
|
||||
(-1, 0),
|
||||
]
|
||||
|
||||
|
||||
def solve(input: Input, second=False):
|
||||
grid, steps = input.text.split("\n\n")
|
||||
grid = list(map(list, grid.splitlines()))
|
||||
rows = len(grid)
|
||||
|
||||
startcol = 0
|
||||
while grid[0][startcol] == ' ':
|
||||
startcol += 1
|
||||
pos = (0, startcol)
|
||||
dir = (0, 1)
|
||||
insts = re.compile(r"(\d+)([LR])").findall(steps)
|
||||
lastn = re.compile(r"\d+").findall(steps)[-1]
|
||||
insts.append((lastn, None))
|
||||
for n, turn in insts:
|
||||
nrow, ncol = pos
|
||||
for _ in range(int(n)):
|
||||
nrow = (nrow + dir[0]) % rows
|
||||
ncol = (ncol + dir[1]) % len(grid[nrow])
|
||||
while grid[nrow][ncol] == ' ':
|
||||
nrow = (nrow + dir[0]) % rows
|
||||
ncol = (ncol + dir[1]) % len(grid[nrow])
|
||||
if grid[nrow][ncol] == '#':
|
||||
break
|
||||
pos = (nrow, ncol)
|
||||
if turn is not None:
|
||||
dir = DIRS[(DIRS.index(dir) + 1 if turn == 'R' else DIRS.index(dir) - 1) % 4]
|
||||
|
||||
return 1000 * (pos[0] + 1) + 4 * (pos[1] + 1) + DIRS.index(dir)
|
||||
|
||||
def main():
|
||||
DAY_INPUT = "i22.txt"
|
||||
|
||||
print("Example 1:", solve(Input(EXAMPLE)))
|
||||
print("Solution 1:", solve(Input(DAY_INPUT)))
|
||||
|
||||
# print("Example 2:", solve(Input(EXAMPLE), True))
|
||||
return
|
||||
|
||||
print("Solution 2:", solve(Input(DAY_INPUT), True))
|
||||
return
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user