def PartOne(program_state): program_state = aoc.getInput(filename, 'csv') EOF = len(program_state) # Set Pre-alarm state program_state[1] = '12' #noun program_state[2] = '2' #verb result = runProgram(program_state) print("Part 1: {}".format(result[0]))
def calculateFuel(filename): result = 0 masses = aoc.getInput(filename) for mass in masses: # Part 1 --- #equiv = math.floor(int(mass) / 3.0) - 2.0 # -------- equiv = calculateTotalFuelRequirement(mass) result = result + int(equiv) return result
def PartTwo(): for noun in range(99, 0, -1): for verb in range(100): intcode = aoc.getInput(filename, 'csv') intcode[1] = noun intcode[2] = verb result = runProgram(intcode) if result[0] == 19690720: print("Part 2: noun: {}, verb: {}".format(noun, verb)) print(" 100 * noun + verb = {}".format(100 * noun + verb)) return print("Part 2: Result not found.")
import aoc lines = aoc.getInput() def bracket(c): return c in ["(", "[", "{", "<", ")", "]", "}", ">"] def opener(c): return c in ["(", "[", "{", "<"] def closer(c): match c: case "(": return ")" case "[": return "]" case "{": return "}" case "<": return ">" print("Unexpected closer", c) return "" def checkLine(line, illegal): stack = [] for c in line: if not bracket(c): continue if opener(c): stack.append(c) else: o = stack.pop() if closer(o) != c: print(line, "is corrupt, got", c, "expected", closer(o)) illegal[c] += 1
import aoc import math wire_paths = aoc.getInput("input3.txt") wire_1 = aoc.csvToList(wire_paths[0]) wire_2 = aoc.csvToList(wire_paths[1]) def findLineSegments(wire): seg = [[0, 0, 0, 0]] index = 0 for direction in wire: heading = direction[0] dist = int(direction[1:]) prev_point = (seg[index][0], seg[index][1]) #x0,y0 next_point = () if heading == 'L' or heading == 'D': dist *= -1 if heading == 'L' or heading == 'R': next_point = (prev_point[0] + dist, prev_point[1]) elif heading == 'U' or heading == 'D': next_point = (prev_point[0], prev_point[1] + dist) seg[index][2] = next_point[0] #x1 seg[index][3] = next_point[1] #y1 seg[index] = tuple(seg[index]) seg.append([next_point[0], next_point[1], 0, 0]) index += 1 seg.remove(seg[-1]) # remove point return seg def findIntersections(wire_1_segs, wire_2_segs):
n = 0 while sum < x: n += 1 sum += n return n def sumTo(x): sum = 0 for n in range(0, x + 1): sum += n return sum # e.g. target area: x=20..30, y=-10..-5 input = aoc.getInput() t = Target(input) # minx = sum(0..start x) >= range min x minx = getStepsTo(t.xrmin) aoc.log(2, f"Min x speed: {minx}") # find max y start speed such that we hit the target # speed could still be > target size if we get "lucky" # let's just try a "sensible" range speedSet = aoc.SetOfCoords() maxy = 0 for y in range(0, 20): if t.hit(minx, y): speedSet.add(aoc.Coord(minx, y)) aoc.log(1, f"target hit when yspeed = {y}")
import aoc input = list(aoc.getInput()[0]) def toDecimal(bin): n = 1 sum = 0 while len(bin) > 0: sum += n * bin.pop() n *= 2 return sum class Bitstream: def __init__(self, input, bits = []): self.input = input self.binary = bits def __len__(self): return len(self.binary) + 4 * len(self.input) def read(self, bitCount): while bitCount > len(self.binary) and len(self.input) > 0: n = int(self.input.pop(0), 16) for bit in range(3, -1, -1): self.binary.append((n >> bit) & 1) s = slice(bitCount) ret = self.binary[s] self.binary = self.binary[slice(bitCount, 999)] return ret class Header:
def PartOne(program_state): program_state = aoc.getInput(filename, 'csv') EOF = len(program_state) # Set Pre-alarm state program_state[1] = '12' #noun program_state[2] = '2' #verb result = runProgram(program_state) print("Part 1: {}".format(result[0])) def PartTwo(): for noun in range(99, 0, -1): for verb in range(100): intcode = aoc.getInput(filename, 'csv') intcode[1] = noun intcode[2] = verb result = runProgram(intcode) if result[0] == 19690720: print("Part 2: noun: {}, verb: {}".format(noun, verb)) print(" 100 * noun + verb = {}".format(100 * noun + verb)) return print("Part 2: Result not found.") program_state = aoc.getInput(filename, 'csv') EOF = len(program_state) PartOne(program_state) PartTwo()