def part2(): fileInput = getFileContent("Day 5 Input.txt", True, False, ",") comp = IntCodeComputer05(fileInput) comp.input = 5 while comp.executeCode(): None print("Part 2: {}".format(comp.output))
def part2(): # Read file dataInput = getFileContent("Day 8 Input.txt", True, False, " ") data = [dataInput, 0] n = Node() n.setValues(data) print("Part 2: {}".format(n.part2Sum()))
def part1(): fileInput = getFileContent("Day 7 Input.txt", True, False, ",") amp = [0, 1, 2, 3, 4] maxOutput = 0 for phase0 in range(5): for phase1 in range(5): if phase1 == phase0: continue for phase2 in range(5): if phase2 == phase1 or phase2 == phase0: continue for phase3 in range(5): if phase3 == phase2 or phase3 == phase1 or phase3 == phase0: continue for phase4 in range(5): if phase4 == phase3 or phase4 == phase2 or phase4 == phase1 or phase4 == phase0: continue phase = [phase0, phase1, phase2, phase3, phase4] ampInput = 0 for i in range(5): amp[i] = IntCodeComputer05(fileInput.copy()) amp[i].input = phase[i] while amp[i].executeCode(): amp[i].input = ampInput None ampInput = amp[i].output if ampInput > maxOutput: maxOutput = ampInput print("Part 1: {}".format(maxOutput))
def part2(): # Time with lists: 389,85 s start = time.time() points = getFileContent("Day 6 Input.txt", True, False, "\n", ",") bounds = [9999, 9999, 0, 0] directions = [[-1, 0], [0, -1], [1, 0], [0, 1]] myMDist = 10000 # Start at first point with total distance < myMDist for p in points: totalDist = 0 for compP in points: totalDist += mDist(compP, p) if totalDist < myMDist: homeP = p break zone = [list(homeP)] i = 0 dirNum = 0 # Expand area until to far away while i < len(zone): # Find new point newP = [ zone[i][0] + directions[dirNum][0], zone[i][1] + directions[dirNum][1] ] isMyP = True # Check if point is already in zone for compP in zone: if newP[0] == compP[0] and newP[1] == compP[1]: isMyP = False break # Check distance to all other points worst = [True, True, True, True] totalDist = 0 if isMyP: for compP in points: totalDist += mDist(compP, newP) # Add new point to zone if isMyP and totalDist < myMDist: #if newP[0] < bounds[0]: # bounds[0] = newP[0] # print("Point x< ({}, {}) {}".format(newP[0], newP[1], totalDist)) #if newP[0] > bounds[1]: # bounds[1] = newP[0] # print("Point x> ({}, {}) {}".format(newP[0], newP[1], totalDist)) #if newP[1] < bounds[2]: # bounds[2] = newP[1] # print("Point y< ({}, {}) {}".format(newP[0], newP[1], totalDist)) #if newP[1] > bounds[3]: # bounds[3] = newP[1] # print("Point y> ({}, {}) {}".format(newP[0], newP[1], totalDist)) zone.append(list(newP)) # Go to next new point dirNum += 1 if dirNum > 3: i += 1 dirNum = 0 print("Zone size = {}".format(len(zone))) end = time.time() print(end - start)
def part2(): # Read file fileInput = getFileContent("Day 17 Input.txt", False, False, "\n", ["=", ",", "..", " "]) board = Board() board.setGrid(fileInput) while len(board.newWater) > 0: board.flow() print("Part 2: Water count is {}".format(board.waterCount(True)))
def day1(): fileInput = getFileContent("Day 8 Input.txt", True, True) p = Picture(25, 6, fileInput) lowestZeroCount = 999 ans = 0 for layer in p.layers: dCount = layer.getDigitCount() if dCount[0] < lowestZeroCount: lowestZeroCount = dCount[0] ans = dCount[1] * dCount[2] print("Part 1: {}".format(ans))
def findGeneralArea(): # Read file posNVel = getFileContent("Day 10 Input.txt", True, False, "\n", [" ", "<", ">", ","]) minDX = 99999999 minX = 0 maxX = 0 minDY = 99999999 minY = 0 maxY = 0 minArea = 99999999 minDXI = 0 minDYI = 0 minAreaI = 0 for i in range(20000): minXT = 9999 maxXT = -9999 minYT = 9999 maxYT = -9999 for pv in posNVel: pv[0] += pv[2] pv[1] += pv[3] if pv[0] < minXT: minXT = pv[0] if pv[0] > maxXT: maxXT = pv[0] if pv[1] < minYT: minYT = pv[1] if pv[1] > maxYT: maxYT = pv[1] areaT = (maxXT - minXT) * (maxYT - minYT) if areaT < minArea: minArea = areaT minAreaI = i print("minDX {}: {} ({}-{})".format(minDXI, minDX, minX, maxX)) print("minDY {}: {} ({}-{})".format(minDYI, minDY, minY, maxY)) print("minArea {}: {}".format(minAreaI, minArea)) if (maxXT - minXT) < minDX: minDX = (maxXT - minXT) minX = minXT maxX = maxXT minDXI = i print("minDX {}: {} ({}-{})".format(minDXI, minDX, minX, maxX)) print("minDY {}: {} ({}-{})".format(minDYI, minDY, minY, maxY)) print("minArea {}: {}".format(minAreaI, minArea)) if (maxYT - minYT) < minDY: minDY = (maxYT - minYT) minY = minYT maxY = maxYT minDYI = i print("minDX {}: {} ({}-{})".format(minDXI, minDX, minX, maxX)) print("minDY {}: {} ({}-{})".format(minDYI, minDY, minY, maxY)) print("minArea {}: {}".format(minAreaI, minArea))
def part1(): mem = getFileContent("Day 2 Input.txt", True, False, ",") i = 0 mem[1] = 12 mem[2] = 2 while mem[i] != 99: if mem[i] == 1: mem[mem[i + 3]] = mem[mem[i + 1]] + mem[mem[i + 2]] i += 4 elif mem[i] == 2: mem[mem[i + 3]] = mem[mem[i + 1]] * mem[mem[i + 2]] i += 4 print("Part 1: {}".format(mem[0]))
def part2(): fileInput = getFileContent("Day 3 Input.txt", False, False, "\n", ",") directions = [] line = [BentLine(), BentLine()] for d in range(2): directions.append([]) for point in range(len(fileInput[d])): line[d].addLine(fileInput[d][point]) col = line[0].collision(line[1]) minDist = 99999999999 for c in col: minDist = min(minDist, c[2]) print("Part 2: Closest distance is {}".format(minDist))
def part1(): # Read file posNVel = getFileContent("Day 10 Input.txt", True, False, "\n", [" ", "<", ">", ","]) for i in range(10121): for pv in posNVel: pv[0] += pv[2] pv[1] += pv[3] for i in range(5): for pv in posNVel: pv[0] += pv[2] pv[1] += pv[3] printHMI(posNVel, 100, 250, 100, 170)
def part2(): # Get file input fileInput = getFileContent("Day 15 Input.txt", False, True, "\n") mapInst = Map() damage = 3 maxDamage = 0 minDamage = damage while minDamage+1 != maxDamage: # Restart map print("Starting map damage min, now, max {} {} {}".format(minDamage, damage, maxDamage)) mapInst.elfDamage = damage mapInst.loadMap(fileInput) eCountStart = 0 for u in mapInst.units: if u.icon == "E": eCountStart += 1 eCount = eCountStart while not mapInst.finished and eCountStart == eCount: # Run map mapInst.action(False) eCount = 0 eSum = 0 gSum = 0 for u in mapInst.units: if u.icon == "E": eSum += u.hp eCount += 1 if u.icon == "G": gSum += u.hp print("Turn: {}, Elves HP: {}({}), Goblins HP: {}".format(mapInst.rounds, eSum, eCount, gSum)) if eCountStart != eCount: # If elf died, damage was to low minDamage = damage if maxDamage == 0: damage *= 2 else: damage = int((maxDamage + minDamage)/2) else: # If no elf died, damage might be to high maxDamage = damage damage = int((maxDamage + minDamage) / 2) print("Minimum required damage for no elf to die is {}".format(maxDamage)) hpSum = 0 for u in mapInst.units: hpSum += u.hp print("Part 2: {} turns * {} hp = {}".format(mapInst.rounds, hpSum, mapInst.rounds*hpSum))
def part2(): fileInput = getFileContent("Day 7 Input.txt", True, False, ",") amp = [5, 6, 7, 8, 9] maxOutput = 0 for phase0 in range(5): for phase1 in range(5): if phase1 == phase0: continue for phase2 in range(5): if phase2 == phase1 or phase2 == phase0: continue for phase3 in range(5): if phase3 == phase2 or phase3 == phase1 or phase3 == phase0: continue for phase4 in range(5): if phase4 == phase3 or phase4 == phase2 or phase4 == phase1 or phase4 == phase0: continue # Startup phase = [phase0+5, phase1+5, phase2+5, phase3+5, phase4+5] for i in range(5): amp[i] = IntCodeComputer05(fileInput.copy()) amp[i].input = phase[i] amp[i].executeCode() amp[0].input = 0 # Run until the first amp halted done = False ampID = 0 while not done: while amp[ampID].executeCode() and amp[ampID].newOutput is False: None if amp[ampID].newOutput: ampInput = amp[ampID].output ampID += 1 if ampID > 4: ampID = 0 amp[ampID].input = ampInput else: done = True if amp[4].output > maxOutput: maxOutput = ampInput print("Part 2: {}".format(maxOutput))
def part1(): # Get file input fileInput = getFileContent("Day 15 Input.txt", False, True, "\n") mapInst = Map() mapInst.loadMap(fileInput) mapInst.print() while not mapInst.finished: eSum = 0 gSum = 0 for u in mapInst.units: if u.icon == "E": eSum += u.hp if u.icon == "G": gSum += u.hp print("Turn: {}, Elves HP: {}, Goblins HP: {}".format(mapInst.rounds, eSum, gSum)) mapInst.action(False) hpSum = 0 for u in mapInst.units: hpSum += u.hp mapInst.print() print("Part 1: {} turns * {} hp = {}".format(mapInst.rounds, hpSum, mapInst.rounds*hpSum))
def part2(): fileInput = getFileContent("Day 2 Input.txt", True, False, ",") goal = 19690720 done = False for noun in range(100): for verb in range(100): mem = list(fileInput) i = 0 mem[1] = noun mem[2] = verb while mem[i] != 99: if mem[i] == 1: mem[mem[i + 3]] = mem[mem[i + 1]] + mem[mem[i + 2]] i += 4 elif mem[i] == 2: mem[mem[i + 3]] = mem[mem[i + 1]] * mem[mem[i + 2]] i += 4 if mem[0] == goal: print("Part 2: {}".format(100 * noun + verb)) done = True break if done: break
def part1(): # Get file input fileInput = getFileContent("Day 13 Input.txt", False, True, "\n") rails = fileInput # Create carts carts = [] for y in range(len(rails)): for x in range(len(rails[y])): if rails[y][x] == '<': carts.append(Cart(x, y, LEFT)) rails[y][x] = '-' if rails[y][x] == '>': carts.append(Cart(x, y, RIGHT)) rails[y][x] = '-' if rails[y][x] == '^': carts.append(Cart(x, y, UP)) rails[y][x] = '|' if rails[y][x] == 'v': carts.append(Cart(x, y, DOWN)) rails[y][x] = '|' # Move carts crash = False crashX = 0 crashY = 0 cycles = 0 while not crash: sortCarts(carts) for c in carts: if rails[c.y][c.x] == '/': if c.dir == LEFT: c.dir = DOWN elif c.dir == RIGHT: c.dir = UP elif c.dir == UP: c.dir = RIGHT elif c.dir == DOWN: c.dir = LEFT if rails[c.y][c.x] == '\\': if c.dir == LEFT: c.dir = UP elif c.dir == RIGHT: c.dir = DOWN elif c.dir == UP: c.dir = LEFT elif c.dir == DOWN: c.dir = RIGHT if rails[c.y][c.x] == '+': if c.lastTurn == LEFT: c.lastTurn = FORWARD elif c.lastTurn == FORWARD: c.lastTurn = RIGHT c.dir += 1 if c.dir == 5: c.dir = 1 elif c.lastTurn == RIGHT: c.lastTurn = LEFT c.dir -= 1 if c.dir == 0: c.dir = 4 if c.dir == LEFT: c.x -= 1 if c.dir == RIGHT: c.x += 1 if c.dir == UP: c.y -= 1 if c.dir == DOWN: c.y += 1 # Test for collision cIndex = 0 for c1 in range(len(carts) - 1): cIndex2 = c1 + 1 for c2 in range(c1 + 1, len(carts)): if carts[c1].x == carts[c2].x and carts[c1].y == carts[ c2].y: crash = True crashX = carts[c1].x crashY = carts[c1].y cIndex2 += 1 cIndex += 1 cycles += 1 print("Part 1: Crash at ({}, {}) after {} cycles".format( crashX, crashY, cycles))
def part1(): # Time with lists: 44,96 s start = time.time() points = getFileContent("Day 6 Input.txt", True, False, "\n", ",") directions = [[-1, 0], [0, -1], [1, 0], [0, 1]] zoneSizes = [] # Go through all points for homeP in points: # print("Testing point ({}, {})".format(homeP[0], homeP[1])) zone = [list(homeP)] i = 0 dirNum = 0 infiniteZone = False # Expand area until all paths has ended while i < len(zone): # Find new point newP = [ zone[i][0] + directions[dirNum][0], zone[i][1] + directions[dirNum][1] ] myMDist = mDist(newP, homeP) isMyP = True # Check if point is already in zone for compP in zone: if newP[0] == compP[0] and newP[1] == compP[1]: isMyP = False break # Compare coordinate with all other points to se who is closest, and check if outside field worst = [True, True, True, True] if isMyP: for compP in points: if not (homeP[0] == compP[0] and homeP[1] == compP[1]): if compP[0] > newP[0]: worst[0] = False if compP[0] < newP[0]: worst[1] = False if compP[1] > newP[1]: worst[2] = False if compP[1] < newP[1]: worst[3] = False if mDist(compP, newP) <= myMDist: isMyP = False break # Add new point to zone if isMyP: zone.append(list(newP)) # Go to next new point dirNum += 1 if dirNum > 3: i += 1 dirNum = 0 if isMyP and (worst[0] or worst[1] or worst[2] or worst[3]): infiniteZone = True break if infiniteZone: zoneSizes.append(0) else: zoneSizes.append(len(zone)) print(zoneSizes) biggest = 0 for z in zoneSizes: if z > biggest: biggest = z print("Biggest = {}".format(biggest)) end = time.time() print(end - start)
def day2(): fileInput = getFileContent("Day 8 Input.txt", True, True) p = Picture(25, 6, fileInput) p.combineLayers() print("Part 2:") p.finalPicture.print()
def part2(): # Read file fileNumbers = getFileContent("Day 9 Input.txt", True, False, " ") playGame(2, fileNumbers[0], fileNumbers[1] * 100)
def part2(): # Get file input fileInput = getFileContent("Day 13 Input.txt", False, True, "\n") rails = fileInput # Create carts carts = [] for y in range(len(rails)): for x in range(len(rails[y])): if rails[y][x] == '<': carts.append(Cart(x, y, LEFT)) rails[y][x] = '-' if rails[y][x] == '>': carts.append(Cart(x, y, RIGHT)) rails[y][x] = '-' if rails[y][x] == '^': carts.append(Cart(x, y, UP)) rails[y][x] = '|' if rails[y][x] == 'v': carts.append(Cart(x, y, DOWN)) rails[y][x] = '|' # Move carts cycles = 0 while len(carts) > 1: sortCarts(carts) for c in carts: if rails[c.y][c.x] == '/': if c.dir == LEFT: c.dir = DOWN elif c.dir == RIGHT: c.dir = UP elif c.dir == UP: c.dir = RIGHT elif c.dir == DOWN: c.dir = LEFT if rails[c.y][c.x] == '\\': if c.dir == LEFT: c.dir = UP elif c.dir == RIGHT: c.dir = DOWN elif c.dir == UP: c.dir = LEFT elif c.dir == DOWN: c.dir = RIGHT if rails[c.y][c.x] == '+': if c.lastTurn == LEFT: c.lastTurn = FORWARD elif c.lastTurn == FORWARD: c.lastTurn = RIGHT c.dir += 1 if c.dir == 5: c.dir = 1 elif c.lastTurn == RIGHT: c.lastTurn = LEFT c.dir -= 1 if c.dir == 0: c.dir = 4 if not c.dead and c.dir == LEFT: c.x -= 1 if not c.dead and c.dir == RIGHT: c.x += 1 if not c.dead and c.dir == UP: c.y -= 1 if not c.dead and c.dir == DOWN: c.y += 1 # Test for collision cIndex = 0 for c1 in range(len(carts) - 1): cIndex2 = c1 + 1 for c2 in range(c1 + 1, len(carts)): if carts[c1].x == carts[c2].x and carts[c1].y == carts[ c2].y: carts[c1].dead = True carts[c2].dead = True cIndex2 += 1 cIndex += 1 for c in range(len(carts) - 1, -1, -1): if carts[c].dead: print("Cart died at step {} ".format(cycles)) del (carts[c]) cycles += 1 print("Part 2: Last cart at ({}, {}) after {} cycles".format( carts[0].x, carts[0].y, cycles))