-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14p2.py
More file actions
executable file
·51 lines (38 loc) · 1.31 KB
/
day14p2.py
File metadata and controls
executable file
·51 lines (38 loc) · 1.31 KB
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
#!/usr/bin/env python
import re
class Reindeer(object):
def __init__(self, rate, fly_time, rest_time):
self.rate = rate
self.fly_time = fly_time
self.rest_time = rest_time
self.remaining_time = fly_time + rest_time
self.distance = 0
with open('input/day14.txt') as fh:
data = fh.read().rstrip('\n').split('\n')
pat = re.compile(
'(\w+) can fly (\d+) km/s for (\d+) seconds, '
'but then must rest for (\d+) seconds.'
)
total_time = 2503
reindeer = dict()
points = dict()
for line in data:
m = re.search(pat, line)
if not m:
raise RuntimeError('Bad line!')
name, rate, fly_time, rest_time = \
m.group(1), int(m.group(2)), int(m.group(3)), int(m.group(4))
reindeer[name] = Reindeer(rate, fly_time, rest_time)
points[name] = 0
for second in xrange(1, total_time+1):
for name, buck in reindeer.iteritems():
if buck.remaining_time > buck.rest_time:
buck.distance += buck.rate
buck.remaining_time -= 1
if buck.remaining_time == 0:
buck.remaining_time = buck.fly_time + buck.rest_time
max_dist = max([buck.distance for buck in reindeer.values()])
for name, buck in reindeer.iteritems():
if buck.distance == max_dist:
points[name] += 1
print max(points.values())