2015 - Day 14 (Reindeer Olympics)

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3

#Advent of Code
#2015 Day 14, 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

def solve(puzzle_input, duration=2503):
    distances = []
    for reindeer in puzzle_input:
        reindeer = reindeer.split()

        t = 0
        d = 0
        speed = int(reindeer[3])
        flytime = int(reindeer[6])
        resttime = int(reindeer[13])
        state = "fly"

        while t + flytime < duration:
            if state == "fly":
                d += speed * flytime
                t += flytime
                state = "rest"
            elif state == "rest":
                t += resttime
                state = "fly"

        if state == "fly" and t < duration:
            d += speed * (duration - t)
        distances.append(d)

    return max(distances)

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

    highest = solve(puzzle_input)

    print("After 2503 seconds, the distance the winning reindeer has travelled is " + str(highest) + "km.")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 1), 16)

    def test_ex2(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 10), 160)

    def test_ex3(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 11), 176)

    def test_ex4(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 12), 176)

    def test_ex5(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 1000), 1120)

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3

#Advent of Code
#2015 Day 14, 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

def solve(puzzle_input, duration=2503):
    stats = []
    for reindeer in puzzle_input:
            reindeer = reindeer.split()

            stats.append({"t": 0,
                          "d": 0,
                          "speed": int(reindeer[3]),
                          "flytime": int(reindeer[6]),
                          "resttime": int(reindeer[13]),
                          "state": "fly",
                          "points": 0})

    for second in range(duration):
        for i in range(len(stats)):
            if stats[i]["state"] == "fly":
                stats[i]["d"] += stats[i]["speed"]
                stats[i]["t"] += 1
                if stats[i]["t"] == stats[i]["flytime"]:
                    stats[i]["t"] = 0
                    stats[i]["state"] = "rest"

            elif stats[i]["state"] == "rest":
                stats[i]["t"] += 1
                if stats[i]["t"] == stats[i]["resttime"]:
                    stats[i]["t"] = 0
                    stats[i]["state"] = "fly"

        first = max(reindeer["d"] for reindeer in stats)
        for i in range(len(stats)):
            if stats[i]["d"] == first:
                stats[i]["points"] += 1

    return max(reindeer["points"] for reindeer in stats)

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

    highest = solve(puzzle_input)

    print("After 2503 seconds, the number of points the winning reindeer has is " + str(highest) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 1), 1)

    def test_ex2(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 140), 139)

    def test_ex3(self):
        return self.assertEqual(solve(["Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.",
                                       "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds."], 1000), 689)

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