Skip to content

Commit e0eb86a

Browse files
committed
Day 9 of AOC 2015
1 parent 7aa2ec9 commit e0eb86a

File tree

4 files changed

+82
-2
lines changed

4 files changed

+82
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ This repository contains my solutions in Python3 for the [Advent of Code](https:
272272
| 24 ||| [🔗](./aoc_2016/day24) |
273273
| 25 ||| [🔗](./aoc_2016/day25) |
274274

275-
## \[2015] 16
275+
## \[2015] 18
276276

277277
| Day | Part 1 | Part 2 | Source link |
278278
| :-: | :----: | :----: | :-------------------------: |
@@ -284,7 +284,7 @@ This repository contains my solutions in Python3 for the [Advent of Code](https:
284284
| 6 ||| [🔗](./aoc_2015/day6) |
285285
| 7 ||| [🔗](./aoc_2015/day7) |
286286
| 8 ||| [🔗](./aoc_2015/day8) |
287-
| 9 | | | [🔗](./aoc_2015/day9) |
287+
| 9 | | | [🔗](./aoc_2015/day9) |
288288
| 10 ||| [🔗](./aoc_2015/day10) |
289289
| 11 ||| [🔗](./aoc_2015/day11) |
290290
| 12 ||| [🔗](./aoc_2015/day12) |

aoc_2015/day9/__init__.py

Whitespace-only changes.

aoc_2015/day9/part1.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pathlib import Path
2+
3+
with Path(Path(__file__).parent, "input").open() as f:
4+
paths = [line.rstrip("\n") for line in f]
5+
6+
NEIGHBORS: dict[str, dict[str, int]] = {}
7+
8+
for path in paths:
9+
mapping, distance_raw = path.split(" = ")
10+
city_from, city_to = mapping.split(" to ")
11+
distance = int(distance_raw)
12+
13+
d = NEIGHBORS.setdefault(city_from, {})
14+
d[city_to] = distance
15+
16+
d = NEIGHBORS.setdefault(city_to, {})
17+
d[city_from] = distance
18+
19+
20+
def shortest_distance(city: str, cities: frozenset[str]) -> int:
21+
neighbors = frozenset(NEIGHBORS[city].keys())
22+
neighbors = neighbors.intersection(cities)
23+
24+
distances: list[int] = []
25+
26+
for neighbor in neighbors:
27+
available_cities = cities.difference([city, neighbor])
28+
29+
distances.append(
30+
NEIGHBORS[city][neighbor] + shortest_distance(neighbor, available_cities)
31+
)
32+
33+
return min(distances) if len(distances) > 0 else 0
34+
35+
36+
cities = frozenset(NEIGHBORS.keys())
37+
38+
result = min(shortest_distance(city, cities) for city in cities)
39+
40+
print(f"Result: {result}")

aoc_2015/day9/part2.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from pathlib import Path
2+
3+
with Path(Path(__file__).parent, "input").open() as f:
4+
paths = [line.rstrip("\n") for line in f]
5+
6+
NEIGHBORS: dict[str, dict[str, int]] = {}
7+
8+
for path in paths:
9+
mapping, distance_raw = path.split(" = ")
10+
city_from, city_to = mapping.split(" to ")
11+
distance = int(distance_raw)
12+
13+
d = NEIGHBORS.setdefault(city_from, {})
14+
d[city_to] = distance
15+
16+
d = NEIGHBORS.setdefault(city_to, {})
17+
d[city_from] = distance
18+
19+
20+
def shortest_distance(city: str, cities: frozenset[str]) -> int:
21+
neighbors = frozenset(NEIGHBORS[city].keys())
22+
neighbors = neighbors.intersection(cities)
23+
24+
distances: list[int] = []
25+
26+
for neighbor in neighbors:
27+
available_cities = cities.difference([city, neighbor])
28+
29+
distances.append(
30+
NEIGHBORS[city][neighbor] + shortest_distance(neighbor, available_cities)
31+
)
32+
33+
return max(distances) if len(distances) > 0 else 0
34+
35+
36+
cities = frozenset(NEIGHBORS.keys())
37+
38+
result = max(shortest_distance(city, cities) for city in cities)
39+
40+
print(f"Result: {result}")

0 commit comments

Comments
 (0)