Added 2025 day11 part 1
This commit is contained in:
63
2025/11/solution.py
Normal file
63
2025/11/solution.py
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/bin/python3
|
||||
import sys,time,re,os,copy
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import *
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
if sys.argv[1] == 'test':
|
||||
input_f = 'test'
|
||||
elif sys.argv[1] == 'input':
|
||||
input_f = 'input'
|
||||
elif sys.argv[1]:
|
||||
input_f = sys.argv[1]
|
||||
else:
|
||||
print('No argv provided')
|
||||
exit()
|
||||
|
||||
|
||||
def file2graph(f):
|
||||
graph = {}
|
||||
for line in open(f):
|
||||
l = line.split(':')
|
||||
key = l[0].strip()
|
||||
nodes = l[1].rstrip().split()
|
||||
graph[key] = nodes
|
||||
return graph
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
def part1():
|
||||
graph = file2graph(input_f)
|
||||
result = dfs_all_paths(graph,'you', 'out',[])
|
||||
|
||||
return len(result)
|
||||
|
||||
start_time = time.time()
|
||||
p1 = part1()
|
||||
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
def part2():
|
||||
if sys.argv[1] == 'test':
|
||||
graph = file2graph('test2')
|
||||
else:
|
||||
graph = file2graph('input')
|
||||
|
||||
score = 0
|
||||
|
||||
return score
|
||||
|
||||
start_time = time.time()
|
||||
p2 = part2()
|
||||
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
|
||||
30
fred.py
30
fred.py
@@ -1,6 +1,7 @@
|
||||
import sys,re,heapq
|
||||
from itertools import permutations
|
||||
from termcolor import colored
|
||||
from functools import cache
|
||||
|
||||
def findInGrid(grid:list,x:str) -> set:
|
||||
"""Find the location of a character in a grid.
|
||||
@@ -680,6 +681,35 @@ def dfs_graph(graph: dict, start) -> list:
|
||||
return list(visited)
|
||||
|
||||
|
||||
|
||||
def dfs_all_paths(graph, start:str, end:str, path:list) -> list:
|
||||
"""
|
||||
Perform a DFS starting from the given position in the graph ending at a given position.
|
||||
Return all possible paths.
|
||||
|
||||
Args:
|
||||
graph: Dictionary representing the graph.
|
||||
start: Starting node in the graph.
|
||||
end: Ending node in the graph.
|
||||
path: Initially an empty list of paths
|
||||
|
||||
Returns:
|
||||
list: List of all possible paths.
|
||||
"""
|
||||
path = path + [start]
|
||||
|
||||
if start == end: return [path]
|
||||
if start not in graph: return []
|
||||
|
||||
all_paths = []
|
||||
|
||||
for neighbor in graph[start]:
|
||||
if neighbor not in path:
|
||||
paths_from_neighbor = dfs_all_paths(graph, neighbor, end, path)
|
||||
all_paths.extend(paths_from_neighbor)
|
||||
return all_paths
|
||||
|
||||
|
||||
def dfs(grid:list, pos:set) -> list:
|
||||
"""
|
||||
Perform a flood fill/dfs starting from the given position in the grid.
|
||||
|
||||
Reference in New Issue
Block a user