Advent of Code 2015 day 9 part 1&2: solved

This commit is contained in:
Fabian Tessmer 2025-03-24 09:29:28 +01:00
parent 73b8f691b2
commit d485e4fcae
3 changed files with 133 additions and 0 deletions

28
2015/day9/input.txt Normal file
View File

@ -0,0 +1,28 @@
AlphaCentauri to Snowdin = 66
AlphaCentauri to Tambi = 28
AlphaCentauri to Faerun = 60
AlphaCentauri to Norrath = 34
AlphaCentauri to Straylight = 34
AlphaCentauri to Tristram = 3
AlphaCentauri to Arbre = 108
Snowdin to Tambi = 22
Snowdin to Faerun = 12
Snowdin to Norrath = 91
Snowdin to Straylight = 121
Snowdin to Tristram = 111
Snowdin to Arbre = 71
Tambi to Faerun = 39
Tambi to Norrath = 113
Tambi to Straylight = 130
Tambi to Tristram = 35
Tambi to Arbre = 40
Faerun to Norrath = 63
Faerun to Straylight = 21
Faerun to Tristram = 57
Faerun to Arbre = 83
Norrath to Straylight = 9
Norrath to Tristram = 50
Norrath to Arbre = 60
Straylight to Tristram = 27
Straylight to Arbre = 81
Tristram to Arbre = 90

36
2015/day9/problem.txt Normal file
View File

@ -0,0 +1,36 @@
--- Day 9: All in a Single Night ---
Every year, Santa manages to deliver all of his presents in a single night.
This year, however, he has some new locations to visit; his elves have provided him the distances between every pair of locations. He can start and end at any two (different) locations he wants, but he must visit each location exactly once. What is the shortest distance he can travel to achieve this?
For example, given the following distances:
London to Dublin = 464
London to Belfast = 518
Dublin to Belfast = 141
The possible routes are therefore:
Dublin -> London -> Belfast = 982
London -> Dublin -> Belfast = 605
London -> Belfast -> Dublin = 659
Dublin -> Belfast -> London = 659
Belfast -> Dublin -> London = 605
Belfast -> London -> Dublin = 982
The shortest of these is London -> Dublin -> Belfast = 605, and so the answer is 605 in this example.
What is the distance of the shortest route?
Your puzzle answer was 141.
--- Part Two ---
The next year, just to show off, Santa decides to take the route with the longest distance instead.
He can still start and end at any two (different) locations he wants, and he still must visit each location exactly once.
For example, given the distances above, the longest route would be 982 via (for example) Dublin -> London -> Belfast.
What is the distance of the longest route?
Your puzzle answer was 736.
Both parts of this puzzle are complete! They provide two gold stars: **

69
2015/day9/solution.py Normal file
View File

@ -0,0 +1,69 @@
def convert_strings(inputs: list) -> list:
connections = []
for input_str in inputs:
ab, distance = input_str.split(' = ')
a, b = ab.split(' to ')
connections.append((a, b, int(distance)))
connections.append((b, a, int(distance)))
return connections
def get_all_possible_connections(connections: list, connection: tuple) -> list:
pre_node, node, distance = connection
possible_connections = []
for _p in connections:
a, b, distance = _p
if a != pre_node and b != pre_node and b != node:
possible_connections.append(_p)
all_possible_paths = []
for p2 in possible_connections:
if p2[0] == node:
qs = get_all_possible_connections(possible_connections, p2)
for q in qs:
qn = [connection] + q
all_possible_paths.append(qn)
if len(all_possible_paths) == 0:
return list([[connection]])
# print(f"c: {connection} :\n ", all_possible_paths, "\n possible_connections: \n", possible_connections)
return all_possible_paths
def get_path_distances(puzzle_input: list) -> list[int]:
connections = convert_strings(puzzle_input)
possible_paths = get_all_possible_paths(connections)
distances = []
for path in possible_paths:
distance = 0
for c in path:
distance += int(c[2])
distances.append(distance)
return distances
def get_all_possible_paths(connections: list) -> list:
possible_paths = []
for connection in connections:
for pc in get_all_possible_connections(connections, connection):
possible_paths.append(pc)
return possible_paths
if __name__ == '__main__':
test_inputs = [
"London to Dublin = 464",
"London to Belfast = 518",
"Dublin to Belfast = 141",
]
assert min(get_path_distances(test_inputs)) == 605, "Error: Example Failed"
print("All test passed")
puzzle_input = open("input.txt", "r").read().splitlines()
print("solution: ", min(get_path_distances(puzzle_input)))
print("Part2: ")
print("solution: ", max(get_path_distances(puzzle_input)))