another day of coding

This commit is contained in:
Peter Hudec
2015-12-20 00:41:02 +01:00
parent 6a4482d038
commit 570f54b720
27 changed files with 178234 additions and 0 deletions

9
14/input Normal file
View File

@ -0,0 +1,9 @@
Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds.
Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds.
Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds.
Donner can fly 25 km/s for 6 seconds, but then must rest for 145 seconds.
Dasher can fly 11 km/s for 12 seconds, but then must rest for 125 seconds.
Comet can fly 21 km/s for 6 seconds, but then must rest for 121 seconds.
Blitzen can fly 18 km/s for 3 seconds, but then must rest for 50 seconds.
Vixen can fly 20 km/s for 4 seconds, but then must rest for 75 seconds.
Dancer can fly 7 km/s for 20 seconds, but then must rest for 119 seconds.

2
14/input1 Normal file
View File

@ -0,0 +1,2 @@
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.

37
14/part1.py Normal file
View File

@ -0,0 +1,37 @@
#!/usr/bin/python
import re
RE_SPEED_INFO = re.compile(r'^(.*) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds.$')
def read_file(filename):
file = open(filename, 'r')
while True:
line = file.readline()
if not line:
break
yield line
def calculateDistance(time, speed, speedTime, restTime):
distance = 0
cycleTime = speedTime + restTime
cycles = int(time/cycleTime)
distance = distance + cycles*speed*speedTime
distance += min(speedTime, time % cycleTime)*speed
return distance
def main():
time = 2503
for line in read_file('input'):
match = re.search(RE_SPEED_INFO, line)
if not match:
print "wrong operation syntax"
distance = calculateDistance(time, int(match.group(2)), int(match.group(3)), int(match.group(4)))
print "%010d %s" % (distance, match.group(1))
if __name__ == "__main__":
main()

68
14/part2.py Normal file
View File

@ -0,0 +1,68 @@
#!/usr/bin/python
import re
RE_SPEED_INFO = re.compile(r'^(.*) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds.$')
class Reindeer:
def __init__(self, name):
self.name = name
self.speed = None
self.speedTime = None
self.restTime = None
self.points = 0
def addPoint(self):
self.points += 1
def calculateDistance(self, time):
distance = 0
cycleTime = self.speedTime + self.restTime
cycles = int(time/cycleTime)
distance = distance + cycles*self.speed*self.speedTime
distance += min(self.speedTime, time % cycleTime)*self.speed
return distance
def read_file(filename):
file = open(filename, 'r')
while True:
line = file.readline()
if not line:
break
yield line
def main():
time = 2503
reindeers = dict()
distance = dict()
for line in read_file('input'):
match = re.search(RE_SPEED_INFO, line)
if not match:
print "wrong operation syntax"
reindeers[match.group(1)] = Reindeer(match.group(1))
reindeers[match.group(1)].speed = int(match.group(2))
reindeers[match.group(1)].speedTime = int(match.group(3))
reindeers[match.group(1)].restTime = int(match.group(4))
distance[match.group(1)] = 0
for t in range(2503):
maxDistance = 0
for r,v in reindeers.iteritems():
distance[r] = v.calculateDistance(t+1)
maxDistance = max(maxDistance, distance[r])
for r,v in distance.iteritems():
if v == maxDistance:
reindeers[r].addPoint()
for r,v in reindeers.iteritems():
print "%010d %s" % (v.points, r)
if __name__ == "__main__":
main()