def part1(data): program = [1, 1, 1, 4, 99, 5, 6, 0, 99] computer.Run(program) assert program[0] == 30 program = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] computer.Run(program) assert program[0] == 3500 program = data[:] computer.Run(program) util.Answer(1, program[0])
def Amplify(program, phasesettings, inputvalue=0): stdout = (v for v in [0]) for phase in phasesettings: stdout = computer.Run(program[:], stdin(phase, stdout)) return list(stdout)[-1]
def __init__(self, program): program[0] = 2 # enable free play self.canvas = Canvas({0: " ", 1: "#", 2: "X", 3: "T", 4: "O"}, False) self.segment = 0 self.stdout = computer.Run(program, self.joystick()) self.ball = None self.paddle = None
def part1(data): stdout = computer.Run(data) video = [] line = [] for out in stdout: if out == 10: video.append(line) line = [] else: line.append(chr(out)) util.Answer(1, compute_alignment(video))
def part2(data): for noun in range(100): for verb in range(100): program = data[:] # make a copy program[1] = noun program[2] = verb computer.Run(program) if program[0] == 19690720: util.Answer( 2, f"{100 * noun + verb} (noun: {noun}, verb: {verb})") break
def __init__(self, program): self.canvas = Canvas({ 0: " ", 1: "^", 2: "V", 3: "<", 4: ">", 5: "#" }, True) self.position = Coord(0, 0) self.direction = 1 self.stdout = computer.Run(program, self.stdin()) self.distance = 0
def AmplifyWithFeedback(program, phasesettings, inputvalue=0): feedback = [ 0, ] stdout = (v for v in feedback) for phase in phasesettings: stdout = computer.Run(program[:], stdin(phase, stdout)) # pipe feedback to amplifier 1 for value in stdout: feedback.append(value) return value
def part1(program): stdout = computer.Run(program, None) canvas = Canvas({0: " ", 1: "#", 2: "X", 3: "T", 4: "O"}, False) while True: try: pos = Coord(next(stdout), next(stdout)) tile = next(stdout) except StopIteration: break canvas.paint(pos, tile) print(canvas) util.Answer(1, sum(1 for v in canvas.canvas.values() if v == 2))
def part1(data): coords = [] effects = [] canvas = Canvas({0: " ", 1: "#"}, inverted=False) for x in range(50): for y in range(50): coord = Coord(x, y) stdout = computer.Run(data[:], stdin(coord)) coords.append(coord) effects.append(next(stdout)) canvas.paint(coord, effects[-1]) print(x) zip(coords, effects) util.Answer(1, sum(effects)) print(canvas)
def part2(data): data[0] = 2 # wake up robot "LR8|L10", "R", "10", "L", "8" "R", "12", "L", "10", "R", "10", "L", "2", "R", "12", "L", "6" # A------ "R", "12", "L", "10", "R", "12", # A "L", "8", "R", "10", "R", "6", "R", "12", "L", "10", "R", "12", # A "R", "12", "L", "10", "R", "10", # A "L", "8", "L", "8", "R", "10", "R", "6", "R", "12", "L", "10", "R", "10", "L", "8", "L", "8", "R", "10", "R", "6", "R", "12", "L", "10", "R", "10", program = [ "A,B,C,A,B,A,B", # program sequence (A,B,C) "R,12,L", # program A "10,R,12", # program B "L,8,R,10,R,6", # program C "n\n", # video feed ] stdout = computer.Run(data, (ord(v) for v in "\n".join(program))) line = [] for out in stdout: if out == 10: if len(line) == 0: print("") # system('cls') else: print("".join(line)) line = [] else: line.append(chr(out)) util.Answer(2, None)
def __init__(self, program, verbose=False): self.position = Coord(0, 0) self.direction = 0 self.hull = Canvas() # pass the camera generator as stdin to the program self.stdout = computer.Run(program[:], self.Camera(), verbose)
def part2(data): stdout = computer.Run(data[:], 2, True) util.Answer(2, list(stdout)[-1])
def part1(data): stdout = computer.Run(data[:], 1, True) util.Answer(1, list(stdout)[-1])
def part1(data): program = data[:] stdout = computer.Run(program, 1) util.Answer(1, list(stdout)[-1])
def part2(data): program = data[:] stdout = computer.Run(program, 5) util.Answer(2, list(stdout)[-1])