self.next = next class Player: def __init__(self, elfID): self.elfID = elfID self.score = 0 def addScore(self, scoreToAdd): self.score += scoreToAdd # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(9, True) words = contents.split(' ') totalPlayers = int(words[0]) totalMarbles = int(words[6]) # ------Parts 1 & 2------ # # Place the first marble currentMarble = Marble(0, 0, 0) currentMarble.next = currentMarble currentMarble.previous = currentMarble part1Stop = totalMarbles part2Stop = totalMarbles * 100 currentMarbleValue = 1
self.predecessors += 1 def removePredecessor(self): self.predecessors -= 1 class TimeStamp: def __init__(self, timeFinished, stepName): self.timeFinished = timeFinished self.stepName = stepName # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(7, True) conditions = list(map(Condition, contents.split('\n'))) # ------Part 1------ # # Make a conditions dictionary and save how many references a condition has: stepsDictPt1 = {} for condition in conditions: if condition.parent not in stepsDictPt1: stepsDictPt1[condition.parent] = Step(condition.parent) stepsDictPt1[condition.parent].addChild(condition.child) else: stepsDictPt1[condition.parent].addChild(condition.child) if condition.child not in stepsDictPt1: stepsDictPt1[condition.child] = Step(condition.child) stepsDictPt1[condition.child].addPredecessor()
from GetFile import getContents # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(1, True) # Parse the frequencies and put them in a list frequencies = list(map(int, contents.split("\n"))) # ------Part 1------ # # Sum the entire list of frequencies answer1 = sum(frequencies) # ------Part 2------ # # Keep track of frequencies we've seen seenFrequencies = [] currentFrequency = 0 repeatFound = False # Update the frequency list every step of the way while not repeatFound: for f in frequencies: currentFrequency += f # If we find a frequency we've already encoutered, stop and note the answer if currentFrequency in seenFrequencies: answer2 = currentFrequency repeatFound = True break
from GetFile import getContents from blist import blist # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(5, True) polymer = contents # ------Part 1------ # def collapsePolymer(polymerToCollapse, charToRemove): # Remove any leading instances of the char to remove while polymerToCollapse[-1].lower() == charToRemove.lower(): del polymerToCollapse[-1] # Traverse the string backwards and delete any double entries. # Only one pass is required due to how unit deletion works here. for i in range(len(polymerToCollapse)-1, 0, -1): # In case the units we collapsed were at the end of the string if i > len(polymerToCollapse) - 1: continue currentUnit = polymerToCollapse[i] nextUnit = polymerToCollapse[i-1] # If we come across a forbidden char, we remove it. # We continue the loop and have it decrease the index 1 by # to continue where we left off. if nextUnit.lower() == charToRemove.lower(): del polymerToCollapse[i-1] continue if currentUnit == nextUnit.swapcase(): # Deleting the units here moves all previous units down by 2 units.
from GetFile import getContents # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(2, True) boxIDs = contents.split("\n") # ------Part 1------ # letter2 = 0 letter3 = 0 # Go through the entire list of boxes for box in boxIDs: # For a box ID, note how often each char appears letterFreqs = {} for char in box: letterFreqs[char] = letterFreqs.get(char, 0) + 1 # Count whether or not this word has chars occurring twice or thrice hasLetter2 = False hasLetter3 = False for letter, freq in letterFreqs.items(): if hasLetter2 and hasLetter3: break if freq == 2 and not hasLetter2: hasLetter2 = True elif freq == 3 and not hasLetter3: hasLetter3 = True
lowerEdges = set() else: claims = self.sweepLineList[indexBefore].claims - self.sweepLineList[indexBefore].getLenLowerEdges() lowerEdges = self.sweepLineList[indexBefore].rectIDs.difference(self.sweepLineList[indexBefore].lowerEdges) newSLP = sweepLinePoint(point, claims, 1, lowerEdges) if isLowerEdge: newSLP.addLowerEdge(rectID) self.sweepLineList.insert(insertAt, newSLP) return insertAt # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(3, True) rectangleStrings = contents.split("\n") rectangles = list(map(Rectangle, rectangleStrings)) # ------Part 1 & 2------ # rectangleDictionary = {rectObj.rectID: rectObj for rectObj in rectangles} overlapsDictionary = {rectObj.rectID: 0 for rectObj in rectangles} xLines = list(map(lambda rectObj: xLine(rectObj.x, rectObj.rectID, True), rectangles)) \ + list(map(lambda rectObj: xLine(rectObj.x + rectObj.width, rectObj.rectID, False), rectangles)) xLines = sorted(xLines, key=lambda xl: (xl.x, xl.isLeftEdge, rectangleDictionary[xl.rectID].y)) sl = sweepLine() previousX = 0 intersectionArea = 0
# Determine in which minute the guard slept the most # Return tuple of form (Days slept, Minute slept on) def getMinuteMostSleptIn(self): currentDays = 0 currentBestMinute = (0, 0) # Days slept, Minute in question for minit in range(0,60): if minit in self.log: currentDays += self.log[minit] if currentDays > currentBestMinute[0]: currentBestMinute = (currentDays, minit) return currentBestMinute # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(4, True) timestamps = list(map(TimeStamp, contents.split('\n'))) timestamps = sorted(timestamps, key=lambda ts: ts.date) # ------Part 1------ # # Loop through timestamps and update guards accordingly guardList = {} currentGuard = 0 minuteFellAsleep = 0 guardAsleep = False sleepiestGuard = 0 # GuardID, minutes asleep for ts in timestamps: # Guard starts their shift
result.append((coordX, coordY)) return result # Add a point captured by this coordinate def addPointCaptured(self): self.pointsCaptured += 1 # Remove a point captured by this coordinate def removePointCaptured(self): self.pointsCaptured -= 1 # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(6, True) # Adjust coordinate list to also add names to them sepCoordinateStrList = contents.split('\n') coordinateStrList = [ coordStr + ", " + str(chr(i + ord('a'))) for i, coordStr in enumerate(sepCoordinateStrList) ] coordinateList = list(map(Coordinate, coordinateStrList)) coordinateDictionary = {coord.name: coord for coord in coordinateList} # ------Part 1------ # smallestX = int(min(crd.x for crd in coordinateList)) smallestY = int(min(crd.y for crd in coordinateList)) largestX = int(max(crd.x for crd in coordinateList))
words = split('<|>|, ', string) self.position = (int(words[1]), int(words[2])) self.velocity = (int(words[4]), int(words[5])) def updatePosition(self): self.position = (self.position[0] + self.velocity[0], self.position[1] + self.velocity[1]) def addOffset(self, offset): return self.position[0] + offset[0], self.position[1] + offset[1] # ------Input------ # answer1 = 0 answer2 = 0 contents = getContents(10, True) starList = list(map(Star, contents.split('\n'))) # ------Part 1------ # # Function that checks the score for a given list of stars # It basically awards points for straight lines in one of the compass directions def calculateScore(starDict): score = 0 for starsAtThisPoint in starDict.values(): for star in starsAtThisPoint: # We only need to look at two compass directions here: East and North for k in range(0, 2): scoreThisRound = 0 baseOffset = (int(sin(radians(90 * k))),