AdventOfCode/2017/19/solution.py

64 lines
1.5 KiB
Python
Raw Normal View History

2024-11-27 23:20:51 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
2024-11-28 13:58:15 +01:00
from fred import list2int,get_value_in_direction
2024-11-27 23:20:51 +01:00
2024-11-28 13:58:15 +01:00
input_f = 'input'
2024-11-27 23:20:51 +01:00
#########################################
# #
2024-11-28 13:58:15 +01:00
# Part 1+2 #
2024-11-27 23:20:51 +01:00
# #
2024-11-28 13:58:15 +01:00
#########################################
2024-11-27 23:20:51 +01:00
grid = []
2024-11-28 13:58:15 +01:00
with open(input_f) as file:
for line in file:
grid.append(line.rstrip())
2024-11-27 23:20:51 +01:00
2024-11-28 13:58:15 +01:00
start = (0,grid[0].index('|'))
dir = (1,0)
visited = []
cur = start
count = 1
while get_value_in_direction(grid,cur) != 'S': # last letter in my input
new = (cur[0]+dir[0],cur[1]+dir[1])
if get_value_in_direction(grid,new) != ' ':
cur = new
2024-11-27 23:20:51 +01:00
count += 1
2024-11-28 13:58:15 +01:00
if get_value_in_direction(grid,cur).isalpha():
visited.append(get_value_in_direction(grid,cur))
2024-11-27 23:20:51 +01:00
2024-11-28 13:58:15 +01:00
if get_value_in_direction(grid,cur) == '+':
if get_value_in_direction(grid,cur,'right') == '-' and dir[1] != -1:
dir = (0,1)
continue
if get_value_in_direction(grid,cur,'left') == '-' and dir[1] != 1:
dir = (0,-1)
continue
2024-11-27 23:20:51 +01:00
2024-11-28 13:58:15 +01:00
if get_value_in_direction(grid,cur,'up') == '|' and dir[0] != 1:
dir = (-1,0)
continue
2024-11-27 23:20:51 +01:00
2024-11-28 13:58:15 +01:00
if get_value_in_direction(grid,cur,'down') == '|' and dir[0] != -1:
dir = (1,0)
continue
2024-11-27 23:20:51 +01:00
2024-11-28 13:58:15 +01:00
for i in visited:
print(i,end='')
print()
print(count)
2024-11-27 23:20:51 +01:00