2015 - Day 9 šŸŽ‚ (All in a Single Night)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3

#Advent of Code
#2015 Day 9, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import permutations

def solve(puzzle_input):
    places = []
    distances = {}

    for line in puzzle_input:
        line = line.strip().split()
        for place in [line[0], line[2]]:
            if place not in places:
                places.append(place)
        distances[str(sorted([line[0], line[2]]))] = int(line[4])

    lowest = sum(distances.values())
    for route in permutations(places):
        running = 0
        for a, b in zip(route[:-1], route[1:]):
            running += distances[str(sorted([a, b]))]
        if running < lowest:
            lowest = running

    return lowest

def main():
    puzzle_input = util.read.as_lines()

    lowest = solve(puzzle_input)

    print("The distance of the shortest route is " + str(lowest) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["London to Dublin = 464",
                                       "London to Belfast = 518",
                                       "Dublin to Belfast = 141"]), 605)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3

#Advent of Code
#2015 Day 9, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import permutations

def solve(puzzle_input):
    places = []
    distances = {}

    for line in puzzle_input:
        line = line.strip().split()
        for place in [line[0], line[2]]:
            if place not in places:
                places.append(place)
        distances[str(sorted([line[0], line[2]]))] = int(line[4])

    highest = 0
    for route in permutations(places):
        running = 0
        for a, b in zip(route[:-1], route[1:]):
            running += distances[str(sorted([a, b]))]
        if running > highest:
            highest = running

    return highest

def main():
    puzzle_input = util.read.as_lines()

    highest = solve(puzzle_input)

    print("The distance of the longest route is " + str(highest) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["London to Dublin = 464",
                                       "London to Belfast = 518",
                                       "Dublin to Belfast = 141"]), 982)

if __name__ == "__main__":
    run(main)
Return to solution index