AdventOfCode/2024/10/solution_P2.py

45 lines
1.1 KiB
Python

#!/bin/python3
import sys,time,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile,toGrid, get_value_in_direction,addTuples,grid_valid
start_time = time.time()
input_f = 'input'
result = 0
dir = {
'up': (-1, 0),
'down': (1, 0),
'left': (0, -1),
'right': (0, 1)
}
grid = toGrid(input_f,list2int)
def find_path(grid:list,pos:set,cur:set):
path = 0
if cur == 9:
return 1
cur+=1
for direction in ['up', 'down', 'left', 'right']:
next_pos = addTuples(pos, dir[direction])
if get_value_in_direction(grid,pos,direction)== cur:
path += find_path(grid, next_pos, cur)
return path
for r, row in enumerate(grid):
for c, col in enumerate(row):
if grid[r][c] == 0:
for direction in ['up', 'down', 'left', 'right']:
next_pos = addTuples((r,c), dir[direction])
if get_value_in_direction(grid,(r,c),direction) == 1:
result += find_path(grid,next_pos,1)
print(result)
print("--- %s seconds ---" % (time.time() - start_time))