mirror of
https://github.com/thib8956/advent-of-code.git
synced 2026-06-15 21:48:16 +00:00
2015 day 9
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import fileinput
|
||||||
|
from collections import defaultdict
|
||||||
|
from itertools import permutations
|
||||||
|
|
||||||
|
|
||||||
|
def parse_locations(inp: list[str]) -> dict[str, dict[str, int]]:
|
||||||
|
"parse list of location pairs into adjacency map"
|
||||||
|
locations = defaultdict(dict)
|
||||||
|
for line in inp:
|
||||||
|
places, dist = line.split(" = ")
|
||||||
|
place_from, place_to = places.split(" to ")
|
||||||
|
locations[place_from][place_to] = int(dist)
|
||||||
|
locations[place_to][place_from] = int(dist) # store both directions
|
||||||
|
return locations
|
||||||
|
|
||||||
|
|
||||||
|
def main(inp):
|
||||||
|
"brute force over permutations of cities, keeping track of min and max cost"
|
||||||
|
locations = parse_locations(inp)
|
||||||
|
min_cost = 99999
|
||||||
|
max_cost = 0
|
||||||
|
for perm in permutations(locations):
|
||||||
|
cost = 0
|
||||||
|
# iterate over pairs of cities and get cost from adjacency map
|
||||||
|
for city_a, city_b in zip(perm, perm[1:]):
|
||||||
|
cost += locations[city_a][city_b]
|
||||||
|
min_cost = min(cost, min_cost)
|
||||||
|
max_cost = max(cost, max_cost)
|
||||||
|
print("Part 1: ", min_cost)
|
||||||
|
print("Part 2: ", max_cost)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
lines = [x.rstrip() for x in fileinput.input()]
|
||||||
|
main(lines)
|
||||||
@@ -28,7 +28,10 @@ def can_reach(pos, obstacles, grid_size):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def search(obstacles, start, goal, grid_size): # could just use bfs?
|
def search(obstacles, start, goal, grid_size):
|
||||||
|
"""
|
||||||
|
Dijkstra's algorithm. Could as well use BFS since step costs are uniform. (n = Node(new_pos, node.cost + 1))
|
||||||
|
"""
|
||||||
queue = [Node(start)]
|
queue = [Node(start)]
|
||||||
visited = set()
|
visited = set()
|
||||||
i = 0
|
i = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user