#!/usr/bin/env python3
#Advent of Code
#2015 Day 14, Part 1
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrundefsolve(puzzle_input,duration=2503):distances=[]forreindeerinpuzzle_input:reindeer=reindeer.split()t=0d=0speed=int(reindeer[3])flytime=int(reindeer[6])resttime=int(reindeer[13])state="fly"whilet+flytime<duration:ifstate=="fly":d+=speed*flytimet+=flytimestate="rest"elifstate=="rest":t+=resttimestate="fly"ifstate=="fly"andt<duration:d+=speed*(duration-t)distances.append(d)returnmax(distances)defmain():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.")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.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)deftest_ex2(self):returnself.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)deftest_ex3(self):returnself.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)deftest_ex4(self):returnself.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)deftest_ex5(self):returnself.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)
#!/usr/bin/env python3
#Advent of Code
#2015 Day 14, Part 2
#Solution by James C. (https://github.com/JamesMCo)
importos,syssys.path.append(os.path.abspath("../.."))importunittest,util.readfromutil.testsimportrundefsolve(puzzle_input,duration=2503):stats=[]forreindeerinpuzzle_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})forsecondinrange(duration):foriinrange(len(stats)):ifstats[i]["state"]=="fly":stats[i]["d"]+=stats[i]["speed"]stats[i]["t"]+=1ifstats[i]["t"]==stats[i]["flytime"]:stats[i]["t"]=0stats[i]["state"]="rest"elifstats[i]["state"]=="rest":stats[i]["t"]+=1ifstats[i]["t"]==stats[i]["resttime"]:stats[i]["t"]=0stats[i]["state"]="fly"first=max(reindeer["d"]forreindeerinstats)foriinrange(len(stats)):ifstats[i]["d"]==first:stats[i]["points"]+=1returnmax(reindeer["points"]forreindeerinstats)defmain():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)+".")classAOC_Tests(unittest.TestCase):deftest_ex1(self):returnself.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)deftest_ex2(self):returnself.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)deftest_ex3(self):returnself.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)