Implement 2019 day 6

This commit is contained in:
2021-01-23 21:08:27 +01:00
parent 07098ab691
commit fe639f14b3
3 changed files with 67 additions and 0 deletions

27
2019/aoc2019/day06.py Normal file
View File

@@ -0,0 +1,27 @@
from typing import TextIO
import networkx
def read_graph(data: TextIO) -> networkx.DiGraph:
graph = networkx.DiGraph()
for line in data:
a, b = line.strip().split(')')
graph.add_edge(a, b)
return graph
def part1(data: TextIO) -> int:
graph = read_graph(data)
paths = networkx.single_source_shortest_path_length(graph, 'COM')
return sum(paths.values())
def part2(data: TextIO) -> int:
graph = read_graph(data).to_undirected()
return networkx.shortest_path_length(graph, 'YOU', 'SAN') - 2