Friday, November 11, 2016

Advent of Code 2015 Day 14 Solution

I'm getting in the Christmas spirit, so here's a Python 3 solution to Advent of Code Day 14's puzzle. I love this site, by the way. Looking forward to the 2016 edition!

import re
f = open("input.txt", "r")
fl = f.readlines()
f.close()
names = {}
rawdistances = {}
winner = {}
isresting = {}
##Setting up dicts to store information about each reindeer
for item in fl:
    matches = re.search("([A-Za-z]+).*?(\d+).*?(\d+).*?(\d+)", item)
    ##matches returns a set of groups: the name of the reindeer, its speed,
    ##the amount of time it can fly before resting, and the time it must rest
    name = matches.group(1)
    speed = int(matches.group(2))
    flytime = int(matches.group(3))
    resttime = int(matches.group(4))
    names[name] = [speed, flytime, resttime]
    ##Store the information of the reindeer in a list in a dict
    isresting[name] = [False, flytime]
    ##isresting stores a list with:
    ##A boolean (False if resting, True if flying)
    ##The amount of time that will elapse before starting the next segment
    rawdistances[name] = 0
    winner[name] = 0
for i in range(2503):
##Replace 2503 with 2504 to solve Part Two
##The reason is that second no. 2503 also needs to be counted for Part Two
    print(str(i))
    for reindeer in names:
        if isresting[reindeer][1] == 0 and isresting[reindeer][0] == False:
            ##Switch to resting at the beginning of the second, it rests for 1 second
            isresting[reindeer][0] = True
            isresting[reindeer][1] = names[reindeer][2]-1
            print(reindeer + ' started resting')
        elif isresting[reindeer][0] == False:
            ##It continues flying
            isresting[reindeer][1] -= 1
            rawdistances[reindeer] += names[reindeer][0]
            print(reindeer + ' flew ' + str(names[reindeer][0]))
            print(reindeer + " is at " + str(rawdistances[reindeer]))
        elif isresting[reindeer][1] == 0 and isresting[reindeer][0] == True:
            ##Switch to flying at the beginning of the second, it flies for 1 second
            isresting[reindeer][0] = False
            isresting[reindeer][1] = names[reindeer][1]-1
            rawdistances[reindeer] += names[reindeer][0]
            print(reindeer + ' stopped resting')
        else:
            ##It continues resting
            isresting[reindeer][1] -= 1
            print(reindeer + ' is resting for ' + str(isresting[reindeer][1]) + " more seconds")

    ##Uncomment the below to find solution to Part Two
    ##high = 0
    ##for item in rawdistances.values():
    ##    if high < item:
    ##        high = item
    ##currname = [k for k, v in rawdistances.items() if v == high]
    ##for item in currname:
    ##    winner[item] += 1
    ##Deindent here!
##high = 0
##for item in rawdistances.values():
##    if high < winner[list(rawdistances.keys()[[list(rawdistances.values()).index(item)]]:
##        high = winner[list(rawdistances.keys())[list(rawdistances.values()).index(item)]]
high = 0
for item in rawdistances.values():
    if high < item:
        high = item
##Print the reindeer in the lead for Part One, the reindeer with the most points for Part Two
print(high)