def getDepthAndTargetLocationFromFile(fileName): for line in getFileAsListOfString(fileName): depthMatch = re.match("^depth: (\d+)$", line) targetMatch = re.match("^target: (\d+),(\d+)", line) if depthMatch: depth = int(depthMatch.group(1)) elif targetMatch: target = (int(targetMatch.group(1)), int(targetMatch.group(2))) return (depth, target)
def getTrackSystemFromFile(self, fileName): trackSystem = TrackSystem() lines = getFileAsListOfString(fileName) for y in range(len(lines)): line = lines[y].rstrip() for x in range(len(line)): symbol = line[x] if symbol != " ": self._addTrackAndOrCart(trackSystem, x, y, symbol) return trackSystem
def getInstructionsAndExamplesFromFile(fileName): """ Returns (instructions, instructionExamples), where: - instructions is a list. Each element in the list (an instruction) is a list of integers. - instructionExamples is a list of InstructionExample. """ instructions = [] instructionExamples = [] lines = getFileAsListOfString(fileName) while len(lines) > 0: line = lines.pop(0) matchBefore = re.match("^Before: *(\[.*\])", line) if matchBefore: registersBefore = stringToIntegerList(matchBefore.group(1)) instruction = stringToIntegerList(lines.pop(0)) matchAfter = re.match("^After: *(\[.*\])", lines.pop(0)) registersAfter = stringToIntegerList(matchAfter.group(1)) instructionExample = InstructionExample(registersBefore, instruction, registersAfter) instructionExamples.append(instructionExample) elif len(line.strip()) > 0: instructions.append(stringToIntegerList(line)) return (instructions, instructionExamples)
print("Part 1: {} nanobots are in range".format(part1)) def part2(nanobots): (pos, size) = getBoundingBox([x.pos for x in nanobots]) initialNode = SearchNode(pos, size, nanobots) (solutionNodes, stats) = AStar([initialNode], ascending=False).findBestSolutions() print( "Nodes created: {c} Evaluated: {e} Skipped: {s} Remaining in list: {l}" .format(c=stats.created, e=stats.evaluated, s=stats.skipped, l=stats.created - (stats.evaluated + stats.skipped))) print("Number of solutions: {}".format(len(solutionNodes))) for node in solutionNodes: print("Part 2: Distance is {}".format( getManhattanDistance3((0, 0, 0), node.pos))) ######## # Main # ######## if __name__ == "__main__": nanobots = [ Nanobot.createFromString(x) for x in getFileAsListOfString("input23.txt") ] part1(nanobots) part2(nanobots)
def getWorldFromFile(fileName): world = World() for line in getFileAsListOfString(fileName): (x, y, width, height) = stringToRectangle(line) world.addClay(x, y, width, height) return world
def getPointsFromFile(fileName): return set( [getPointFromString(x) for x in getFileAsListOfString(fileName)])
def getEventsFromFile(fileName): return [ Event.createFromString(s) for s in sorted(getFileAsListOfString(fileName)) ]
return positionToNrClaims def findNonOverlappingClaim(positionToNrClaims, claims): for claim in claims: xRange = range(claim.x, claim.x + claim.width) yRange = range(claim.y, claim.y + claim.height) if not any(1 for x in xRange for y in yRange if positionToNrClaims[(x, y)] > 1): return claim ######## # Main # ######## claims = [ Claim.createFromString(line) for line in getFileAsListOfString("input03.txt") ] positionToNrClaims = getPositionToNrClaims(claims) # Part 1: How many squares are claimed by more than one elf? numberOfOverlappingSquares = len( [1 for x in positionToNrClaims.values() if x > 1]) print("Number of squares with more than one claim: {}".format( numberOfOverlappingSquares)) # Part 2: What is the ID of the only claim that is not overlapping with any other claim? nonOverlappingClaim = findNonOverlappingClaim(positionToNrClaims, claims) print("Found non-overlapping claim: {}".format(nonOverlappingClaim))