Browse Source

Day 14.

master
Chris Smith 8 years ago
parent
commit
a0ac85adeb
2 changed files with 38 additions and 0 deletions
  1. 29
    0
      14.py
  2. 9
    0
      14.txt

+ 29
- 0
14.py View File

@@ -0,0 +1,29 @@
1
+#!/usr/bin/python
2
+
3
+import re
4
+from collections import defaultdict
5
+
6
+at = 2503
7
+pattern = re.compile('^(.*) can fly ([0-9]+) .*? for ([0-9]+) seconds, .* for ([0-9]+) seconds.')
8
+reindeer = {}
9
+
10
+with open('14.txt', 'r') as file:
11
+    for line in file.readlines():
12
+        name, speed, speed_period, rest_period = pattern.match(line).groups()
13
+        reindeer[name] = (int(speed), int(speed_period), int(rest_period))
14
+
15
+
16
+def distance_at(reindeer_spec, time):
17
+    speed, speed_period, rest_period = reindeer_spec
18
+    cycle_period = speed_period + rest_period
19
+    return speed * (speed_period * (time // cycle_period) + min(speed_period, time % cycle_period))
20
+
21
+print(max(distance_at(v, at) for v in reindeer.values()))
22
+
23
+points = defaultdict(int)
24
+for i in range(at):
25
+    distances = {r[0]: distance_at(r[1], i + 1) for r in reindeer.iteritems()}
26
+    for n in [n for n, d in distances.iteritems() if d == max(distances.values())]:
27
+        points[n] += 1
28
+
29
+print(max(points.values()))

+ 9
- 0
14.txt View File

@@ -0,0 +1,9 @@
1
+Vixen can fly 19 km/s for 7 seconds, but then must rest for 124 seconds.
2
+Rudolph can fly 3 km/s for 15 seconds, but then must rest for 28 seconds.
3
+Donner can fly 19 km/s for 9 seconds, but then must rest for 164 seconds.
4
+Blitzen can fly 19 km/s for 9 seconds, but then must rest for 158 seconds.
5
+Comet can fly 13 km/s for 7 seconds, but then must rest for 82 seconds.
6
+Cupid can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
7
+Dasher can fly 14 km/s for 3 seconds, but then must rest for 38 seconds.
8
+Dancer can fly 3 km/s for 16 seconds, but then must rest for 37 seconds.
9
+Prancer can fly 25 km/s for 6 seconds, but then must rest for 143 seconds.

Loading…
Cancel
Save