131 lines
3.3 KiB
Python
131 lines
3.3 KiB
Python
|
#!/bin/python3
|
||
|
import sys,re
|
||
|
from pprint import pprint
|
||
|
sys.path.insert(0, '../../')
|
||
|
from fred import list2int
|
||
|
|
||
|
input_f = 'test'
|
||
|
|
||
|
part = 1
|
||
|
#########################################
|
||
|
# #
|
||
|
# Part 1 #
|
||
|
# #
|
||
|
#########################################
|
||
|
|
||
|
grid = []
|
||
|
|
||
|
start = ()
|
||
|
prev = ()
|
||
|
|
||
|
def valid_n(grid,cur):
|
||
|
r,c = cur
|
||
|
count = 0
|
||
|
if grid[r-1][c] != ' ':
|
||
|
count += 1
|
||
|
if grid[r+1][c] != ' ':
|
||
|
count += 1
|
||
|
if grid[r][c+1] != ' ':
|
||
|
count += 1
|
||
|
if grid[r][c-1] != ' ':
|
||
|
count += 1
|
||
|
return count
|
||
|
|
||
|
|
||
|
if part == 1:
|
||
|
with open(input_f) as file:
|
||
|
for line in file:
|
||
|
grid.append(list(line.rstrip()))
|
||
|
|
||
|
for r, row in enumerate(grid):
|
||
|
for c, col in enumerate(row):
|
||
|
print(grid[r][c],end='')
|
||
|
print()
|
||
|
|
||
|
for r, row in enumerate(grid):
|
||
|
for c, col in enumerate(row):
|
||
|
if r == 0:
|
||
|
if grid[r][c] == '|':
|
||
|
start = (r,c)
|
||
|
|
||
|
end = False
|
||
|
cur = start
|
||
|
prev = (-1,-1)
|
||
|
prev_dir = {
|
||
|
'up': False,
|
||
|
'down': False,
|
||
|
'left': False,
|
||
|
'right': False
|
||
|
}
|
||
|
|
||
|
while not end:
|
||
|
r,c = cur
|
||
|
try:
|
||
|
if grid[r-1][c] != ' ' and prev != (r-1,c):
|
||
|
print('up',grid[r-1][c])
|
||
|
|
||
|
prev = cur
|
||
|
prev_dir = {
|
||
|
'up': True,
|
||
|
'down': False,
|
||
|
'left': False,
|
||
|
'right': False
|
||
|
}
|
||
|
cur = (r-1,c)
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
|
||
|
try:
|
||
|
if grid[r+1][c] != ' ' and prev != (r+1,c):
|
||
|
print('down',grid[r+1][c])
|
||
|
if valid_n(grid,cur) >= 1 and prev_dir['down']:
|
||
|
prev = cur
|
||
|
prev_dir = {
|
||
|
'up': False,
|
||
|
'down': True,
|
||
|
'left': False,
|
||
|
'right': False
|
||
|
}
|
||
|
cur = (r+1,c)
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
|
||
|
try:
|
||
|
if grid[r][c-1] != ' ' and prev != (r,c-1) and not prev_dir['left']:
|
||
|
print('left',grid[r][c-1])
|
||
|
prev = cur
|
||
|
prev_dir = {
|
||
|
'up': False,
|
||
|
'down': False,
|
||
|
'left': True,
|
||
|
'right': False
|
||
|
}
|
||
|
cur = (r,c-1)
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
|
||
|
try:
|
||
|
if grid[r][c+1] != ' ' and prev != (r,c+1) and not prev_dir['right']:
|
||
|
print('right',grid[r][c+1])
|
||
|
prev = cur
|
||
|
prev_dir = {
|
||
|
'up': False,
|
||
|
'down': False,
|
||
|
'left': False,
|
||
|
'right': True
|
||
|
}
|
||
|
cur = (r,c+1)
|
||
|
except Exception as e:
|
||
|
print(e)
|
||
|
input()
|
||
|
|
||
|
|
||
|
|
||
|
#########################################
|
||
|
# #
|
||
|
# Part 2 #
|
||
|
# #
|
||
|
#########################################
|
||
|
if part == 2:
|
||
|
exit()
|