2015 day 14, part 2
This commit is contained in:
parent
d1418edbd9
commit
cfe00458a4
|
@ -20,4 +20,4 @@ for reindeer in reindeers:
|
|||
reindeer['distance'] = RACE_DURATION // cycle_duration * reindeer['speed'] * reindeer['duration']
|
||||
reindeer['distance'] += min(RACE_DURATION % cycle_duration, reindeer['duration']) * reindeer['speed']
|
||||
|
||||
print(sorted(map(itemgetter('distance'), reindeers))[-1])
|
||||
print(max(map(itemgetter('distance'), reindeers)))
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python3
|
||||
from collections import namedtuple
|
||||
from math import ceil
|
||||
from operator import itemgetter
|
||||
import fileinput
|
||||
import re
|
||||
|
||||
INPUT_RE = re.compile(r'^(?P<name>[A-Za-z]+) can fly (?P<speed>\d+) km/s for (?P<duration>\d+) seconds, but then must rest for (?P<cooldown>\d+) seconds\.$')
|
||||
RACE_DURATION = 2503
|
||||
|
||||
reindeers = []
|
||||
for line in fileinput.input():
|
||||
data = INPUT_RE.match(line).groupdict()
|
||||
for k in ('speed', 'duration', 'cooldown'):
|
||||
data[k] = int(data[k])
|
||||
data.update(
|
||||
distance=0,
|
||||
score=0,
|
||||
health=data['duration'],
|
||||
)
|
||||
reindeers.append(data)
|
||||
|
||||
for _ in range(RACE_DURATION):
|
||||
for reindeer in reindeers:
|
||||
if reindeer['health'] < 0: # Resting
|
||||
reindeer['health'] += 1
|
||||
if reindeer['health'] == 0: # Done resting
|
||||
reindeer['health'] = reindeer['duration']
|
||||
continue
|
||||
|
||||
assert reindeer['health'] != 0
|
||||
reindeer['health'] -= 1
|
||||
reindeer['distance'] += reindeer['speed']
|
||||
if reindeer['health'] == 0: # Start resting
|
||||
reindeer['health'] = -reindeer['cooldown']
|
||||
|
||||
|
||||
highest_distance = max(map(itemgetter('distance'), reindeers))
|
||||
for reindeer in reindeers:
|
||||
if reindeer['distance'] == highest_distance:
|
||||
reindeer['score'] += 1
|
||||
|
||||
# Compute one last time
|
||||
highest_distance = max(map(itemgetter('distance'), reindeers))
|
||||
for reindeer in reindeers:
|
||||
if reindeer['distance'] == highest_distance:
|
||||
reindeer['score'] += 1
|
||||
|
||||
print(max(map(itemgetter('score'), reindeers)))
|
Loading…
Reference in New Issue