def run_tests_create_tape(): tape = Tape() data = ["A", "B", "C", "B", "C", "A", "\n"] expected = "65,66,67,66,67,65,10" for ch in data: tape.append(ch, ord) res = str(tape) if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}") tape = Tape() data = ["R", ",", "8", ",", "L", ",", "6", "\n"] expected = "82,44,56,44,76,44,54,10" tape.append(data, ord) res = str(tape) if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def run_day_19_2(): """ TODO: runs really long: 80 min """ print("=== Day 16, Task 2 (takes really long) ===") tape = Tape.read_from_file('input.txt') expected = 6950903 scanner = Scanner2(tape) scanner.verbose = not True beam = Beam(scanner.target_v) scanner.execute(beam) # print("--- results ---") # print(beam) print(f"Coordinates of the closest point: {scanner.target}") x,y = scanner.target # (695,903) res = 10000*x+y # 6950903 if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def run_day_13_2(): """ Beat the game by breaking all the blocks. What is your score after the last block is broken? """ print("=== Day 13 part 2 ===") tape = Tape.read_from_file("input.13.txt") tape.cells[0] = 2 expected = 9803 game = Game((45, 20), tape) game.verbose = not True game.player = Player() game.execute() print("--- Final State of the Board ---") print(game) res = game.score print(f"Answer: The score after all blocks are destroyed is {res}") if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def run_day_13_1(): """ Start the game. How many block tiles are on the screen when the game exits? """ print("=== Day 13 part 1 ===") tape = Tape.read_from_file("input.13.txt") expected = 200 game = Game((45, 20), tape) game.verbose = True game.execute() print(game) blocks = np.where(game.board == game.BLOCK, 1, 0) res = np.count_nonzero(blocks) print(f"Answer: number of BLOCK tiles on the board: {res}") if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def execute(self, beam): self.beam = beam readings = [] for self.coord in self.positions(): if self.finished(): break y,x = self.coord self._vprint(f"Position: {(x,y)}") tape = Tape(self.tape) tape.rewind() # TODO: necessary? computer = Interpreter(tape, inputs=[x,y], outputs=readings) computer.execute() if readings.pop(0) == 1: self.beam.add((y,x)) self._vprint(str(self.beam))
def run_day_11_2(): """ """ print("=== Day 11 part 2 ===") tape = Tape.read_from_file("input.11.txt") expected = """\ ........................................... .#..#.###...##..#....####.#..#.#....####... .#..#.#..#.#..#.#.......#.#.#..#....#...... .####.###..#....#......#..##...#....###.... .#..#.#..#.#.##.#.....#...#.#..#....#...... .#..#.#..#.#..#.#....#....#.#..#....#...... .#..#.###...###.####.####.#..#.####.#...... ...........................................""" shape = (8, 43) startpos = (1, 0) hull = np.zeros(shape, dtype='int8') # all BLACK robot = PaintingRobot(tape, hull) robot.verbose = not True hull[startpos] = PaintingRobot.WHITE painted_panels = {} def count_painted_panels(coord, oldcolor, newcolor): painted_panels[coord] = True robot.on_paint = count_painted_panels robot.move_to(startpos) robot.execute() # print(f"Finished after {robot.num_iterations}") # print(robot._show_canvas()) lines = [] for row in hull: cols = ["." if c == 0 else "#" for c in row] lines.append("".join(cols)) res = "\n".join(lines) # print(res) #=> HBGLZKLF if res == expected: print(f"SUCCESS: Got as expected:\n{res}") else: print(f"FAILED: Expected\n{expected}\nbut got\n{res}\n")
def run_day_17_2(): """ After visiting every part of the scaffold at least once, how much dust does the vacuum robot report it has collected? """ print("=== Day 17, Task 2 ===") expected = 1063081 # first, we need to rerun the task 1 to get the board. #tape = Tape.read_from_file("input.txt") #bb = BoardBuilder(tape) #board = bb.execute() #path = board.path # but we are lazy and therefore cutting short path = Board().path # now part 2 tape = Tape.read_from_file("input.txt") tape.write_to(0, 2) # analyse path through the whole scaffold area and produce a program for # the cleaning robot that comprises movement instructions. commands = [] enc = CommandEncoder() enc.analyse_path(path) print(f"Number of possible programms: {len(enc.decompositions)}") for t in enc.to_tapes(enc.decompositions[0]): commands.extend(list(t.cells)) # provide continuous video feed? # continous video feed will produce a full board after each robot movement yes = [ord('y'), ord('\n')] # This is very slow! no = [ord('n'), ord('\n')] commands.extend(no) # finally, launch the robot bb = BoardBuilder(tape) bb.computer.inputs = commands bb.execute() res = bb.amount_of_dust print(f"Answer: amount of dust the robot collected: {res}") if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def to_tape(self, items): """ Build a Tape from list of strings given in <items>, inserting a comma between individual items and adding a newline after the last item. """ tape = Tape() separators = [','] * (len(items) - 1) + ['\n'] for item, sep in zip(items, separators): tape.append(item, ord) tape.append(sep, ord) return tape
def run_day_15_1(): """ What is the fewest number of movement commands required to move the repair droid from its starting position to the location of the oxygen system? """ # SOLUTION #Start -> Goal: (23, 23) -> (35, 39) print("=== Day 15, Task 1 ===") expected = 220 verbose = not True shape = (41,41) # optimal start_pos = (1+shape[0]//2, 1+shape[1]//2) remote = RemoteControl() remote.board = Board(shape, start_pos) remote.program = Tape.read_from_file("input.txt") remote.verbose = verbose remote.show_progress = True # will show the board # remote.computer.verbose = verbose remote.execute() res = remote.board.distance_to(remote.board.goal) print("------- Board final state -------") remote.board.unmark_dead_end_paths() print(remote.board.visualize()) print(f"Start -> Goal: {remote.board.origin} -> {remote.board.goal}, distance: {res}") print(f"Answer: distance between START and GOAL in movements: {res}") if verbose: print("--- Matrix of distances from ORIGIN ---") print(remote.board.distances) if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}") return remote.board
def run_day_19_1(): print("=== Day 19, Task 1 ===") tape = Tape.read_from_file('input.txt') expected = 229 scanner = Scanner1(tape) scanner.verbose = not True beam = Beam() scanner.execute(beam) print(beam) res = beam.area print(f"Number of points affected by the tractor beam: {res}") if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def run_tests_day_11_1(): print("=== Day 11 part 1 (tests) ===") # test as in task shape = (5, 5) startpos = (2, 2) hull = np.zeros(shape, dtype='int8') tape = Tape.read_from_file("input.11.txt") # any will do robot = PaintingRobot(tape, hull) robot.move_to(startpos) commands = [[1, 0], [0, 0], [1, 0], [1, 0], [0, 1], [1, 0], [1, 0]] for paint, turn in commands: robot.do_paint(paint) robot.do_turn(turn) print(robot.canvas)
def run_day_17_1(): """ What is the sum of the alignment parameters for the scaffold intersections? """ print("=== Day 17, Task 1 ===") tape = Tape.read_from_file("input.txt") expected = 13580 bb = BoardBuilder(tape) board = bb.execute() res = board.total_alignment print(f"Answer: sum of all camera alignment parameters: {res}") if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")
def run_day_11_1(): """ Before you deploy the robot, you should probably have an estimate of the area it will cover: specifically, you need to know the number of panels it paints at least once, regardless of color. Build a new emergency hull painting robot and run the Intcode program on it. How many panels does it paint at least once? """ print("=== Day 11 part 1 (takes long to run) ===") tape = Tape.read_from_file("input.11.txt") expected = 2226 shape = (100, 80) startpos = (49, 29) hull = np.zeros(shape, dtype='int8') # all BLACK robot = PaintingRobot(tape, hull) robot.verbose = not True painted_panels = {} def count_painted_panels(coord, oldcolor, newcolor): painted_panels[coord] = True robot.on_paint = count_painted_panels robot.move_to(startpos) robot.execute() # print(f"Finished after {robot.num_iterations}") # print(robot._show_canvas()) res = len(painted_panels) print(f"Answer: number of painted panels is {res}") if res == expected: print(f"SUCCESS: Got {res} as expected") else: print(f"FAILED: Expected {expected} but got {res}")