def getRule(rule, rules, missing): if rule in rules.keys(): return rules[rule] match = re.fullmatch('(([ ]?[0-9]+)+)', missing[rule]) if match: subrules = convertToInt(match.groups()[0].split(' ')) result = getResult(subrules, rules, missing) rules[rule] = result else: match = re.fullmatch('(([ ]?[0-9]+)+) \| (([ ]?[0-9]+)+)', missing[rule]) if match: alternatives = [] alternatives.append(match.groups()[0]) alternatives.append(match.groups()[2]) completeResult = [] for alternative in alternatives: subrules = convertToInt(alternative.split(' ')) result = getResult(subrules, rules, missing) completeResult += result rules[rule] = completeResult del missing[rule] return rules[rule]
def do1(xRange, yRange): xMin,xMax = convertToInt(xRange.split(',')) yMin,yMax = convertToInt(yRange.split(',')) maxY = -100000 for xVel in range(1, xMax + 1): for yVel in range(0, 200): currentMaxY = fly((0,0), (xVel,yVel), (xMin,xMax), (yMin, yMax)) maxY = max(maxY, currentMaxY) return maxY
def getLine(line): match = re.findall('(on|off) x=(-?[0-9]+)\.\.(-?[0-9]+)',line) onOrOff,xmin,xmax = match[0] xmin,xmax = convertToInt((xmin,xmax)) match = re.findall('y=(-?[0-9]+)\.\.(-?[0-9]+)',line) ymin,ymax = convertToInt(match[0]) match = re.findall('z=(-?[0-9]+)\.\.(-?[0-9]+)',line) zmin,zmax = convertToInt(match[0]) return onOrOff,xmin,xmax,ymin,ymax,zmin,zmax
def do2(xRange, yRange): xMin,xMax = convertToInt(xRange.split(',')) yMin,yMax = convertToInt(yRange.split(',')) landingVels = [] for xVel in range(1, xMax + 1): for yVel in range(-100, 200): currentMaxY = fly((0,0), (xVel,yVel), (xMin,xMax), (yMin, yMax)) if currentMaxY != -1000000: landingVels.append((xVel,yVel)) return len(landingVels)
def do(): strInput = readInputFile(1) splitInput = strInput.split('\n') intInput = convertToInt(splitInput) print(do1(intInput)) print(do2(intInput))
def do(): with open('Input/day1.txt') as f: strInput = f.read() splitInput = inputsplit(strInput, '\n') intInput = convertToInt(splitInput) print(do1(intInput)) print(do2(intInput))
def do(): strInput = readInputFile(21) player1,player2 = convertToInt(strInput.split('\n')) print(do1(player1,player2)) print(do2(player1,player2)) print('done')
def do(): strInput = readInputFile(6) splitInput = strInput.split(',') intInput = convertToInt(splitInput) print(do1(intInput)) print(do2(intInput)) print('done')
def prepareInput(puzzleInput): intNumbers = convertToInt(puzzleInput) numbers = {} for i, number in enumerate(intNumbers): numbers[number] = [1, [i + 1]] lastNumber = number return [numbers, lastNumber]
def do(): with open('Input/day9.txt') as f: strInput = f.read() splitInput = inputsplit(strInput, '\n') intInput = convertToInt(splitInput) preambelLength = 25 result1 = do1(intInput, preambelLength) print(result1) print(do2(intInput, result1))
def checkNearbyTickets(tickets, validFields): invalidValues = [] for ticket in tickets: strfields = ticket.split(',') fields = convertToInt(strfields) for field in fields: valid = False for value in validFields.values(): if field in value: valid = True if not valid: invalidValues.append(field) return invalidValues
def generateBiggerMap(splitInput): newLines = [] for line in splitInput: line = convertToInt(line) newPart = deepcopy(line) for i in range(4): newPart = [x + 1 if x < 9 else 1 for x in newPart] line += newPart newLines.append(line) newMap = deepcopy(newLines) for i in range(1, 5): for line in newLines: newMap.append([x + i if x + i < 10 else (x + i) - 9 for x in line]) return newMap
def getPointsAndInstructions(dots, foldInstructions): points = set() instructions = [] for dot in dots: x,y = convertToInt(dot.split(',')) points.add((x,y)) #foldInstructions for line in foldInstructions: match = re.fullmatch('fold along ([x,y])=([0-9]+)', line) if match: coordinate = match.groups()[0] value = int(match.groups()[1]) instructions.append((coordinate,value)) return points,instructions
def getValidTickets(tickets, validFields): validIntTickets = [] for ticket in tickets: ticketValid = True strfields = ticket.split(',') fields = convertToInt(strfields) for field in fields: valid = False for value in validFields.values(): if field in value: valid = True if not valid: ticketValid = False break if ticketValid: validIntTickets.append(fields) return validIntTickets