def compute_computer_output(program: Program, noun: int, verb: int) -> int: initial_state = program.copy() initial_state[1] = noun initial_state[2] = verb cmp = Computer(initial_state, None, None) cmp.run_until_stop() return cmp._state[0]
def test_day9_test3(): """output the large number in the middle""" prog = [104, 1125899906842624, 99] q2 = Queue() c = Computer(prog, None, q2) c.run_until_stop() output = queue_to_list(q2)[0] assert output== 1125899906842624
def test_day9_test1(): """Can copy self?""" prog = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99] q2 = Queue() c = Computer(prog, None, q2) c.run_until_stop() outputs = [i for i in queue_to_list(q2) if not isinstance(i,ComputerSignals)] assert outputs== prog
def test_day9_test2(): """Can output a 16-digit number?""" prog = [1102, 34915192, 34915192, 7, 4, 7, 99, 0] q2 = Queue() c = Computer(prog, None, q2) c.run_until_stop() output = queue_to_list(q2)[0] output_n_digit = len(str(output)) assert output_n_digit== 16
def test_day5_test2(): """TEST program runs okay""" p = thermal_environment_supervision_terminal() inval = 1 q1 = Queue() q1.put(inval) q2 = Queue() c = Computer(p, q1, q2) c.run_until_stop() outputs = [i for i in queue_to_list(q2) if not isinstance(i,ComputerSignals)] diagnostic_codes = outputs[:-1] assert diagnostic_codes== [0, 0, 0, 0, 0, 0, 0, 0, 0]
def solve_part_one(): p = get_program('../inputs/day13.txt') c = Computer(p, Queue(), Queue()) c.run_until_stop() all_outputs = queue_to_list(c.output_queue) all_outputs = all_outputs[: -1] # skip the final "program terminated"-output screen = defaultdict(int) for x, y, tile_id in grouper(all_outputs, 3): assert tile_id in tile_ids, f"invalid code: {tile_id}" screen[(x, y)] = tile_id return sum(1 for tid in screen.values() if tid == BLOCK)
def calc_amp(amp_program: Program, phase_config: Iterable[int]): input_ = 0 for phase in phase_config: inq = Queue() inq.put(phase) inq.put(input_) outq = Queue() amplifier = Computer(amp_program, input_queue=inq, output_queue=outq) amplifier.run_until_stop() amp_output = [ i for i in queue_to_list(outq) if not isinstance(i, ComputerSignals) ] assert len(amp_output) == 1 input_ = amp_output[-1] return amp_output[-1]
def test_day2_test1(): state1 = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] c = Computer(state1, None, None) c.step() assert c.state== [1, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] c.step() assert c.state== [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] c.step() assert c.has_stopped
def __init__(self): p = get_program('../inputs/day13.txt') p[0] = 2 # insert two coins in the slot machine self.brain = Computer(p, Queue(), Queue()) self.screen = defaultdict(int) self.ball_position = None self.paddle_position: Optional[Pos] = None self.prev_ball_position: Optional[Pos] = None self.score = 0
def test_day9_test5(): """Can change relative base""" q = Queue() c = Computer([109,19,204,-34],None,q) c.relative_base = 2000 c._state[1985] = 1234 c.step() c.step() assert q.get()==1234
def test1(): def test_run(robot): expected_camera_readings = queue_from_iterable([0, 0, 0, 0, 1, 0, 0]) instruction_list = queue_from_iterable( [1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0]) while not expected_camera_readings.empty(): assert robot.use_camera() == expected_camera_readings.get() color_instr = PanelColor(instruction_list.get(timeout=1)) robot.paint(color_instr) turn_instr = RotDir(instruction_list.get(timeout=1)) robot.turn(turn_instr) robot.move_forward() grid = defaultdict(lambda: PanelColor.Black) p = get_program('day11.txt') test_brain = Computer(p, Queue(), Queue()) test_robot = EmergencyHullPaintingRobot(test_brain, grid) test_run(test_robot)
def test_day2_test5(): instate = [1, 1, 1, 4, 99, 5, 6, 0, 99] outstate = [30, 1, 1, 4, 2, 5, 6, 0, 99] c = Computer(instate, None, None) c.run_until_stop() assert c.state== outstate
def test_day2_test4(): instate = [2, 4, 4, 5, 99, 0] outstate = [2, 4, 4, 5, 99, 9801] c = Computer(instate, None, None) c.run_until_stop() assert c.state== outstate
def test_day2_test3(): instate = [2, 3, 0, 3, 99] outstate = [2, 3, 0, 6, 99] c = Computer(instate, None, None) c.run_until_stop() assert c.state== outstate
def run_computer_on_queues(program: Program, in_q: Queue, out_q: Queue): c = Computer(program, input_queue=in_q, output_queue=out_q) logging.debug("Starting execution of some program.") c.run_until_stop() logging.debug("The program has stopped.")
from collections import defaultdict from queue import Queue from day11.day11_lib import PanelColor, EmergencyHullPaintingRobot, chargrid_to_str, plot_panels_and_robot from util import get_program, Computer p = get_program('day11.txt') grid = defaultdict(lambda: PanelColor.Black) brain = Computer(p, Queue(), Queue()) robot = EmergencyHullPaintingRobot(brain, grid) robot.run_until_stop(verbose=False) print(f"ans a:{len(robot.painted_coordinates)}") # 1964 is just right! grid = defaultdict(lambda: PanelColor.Black) grid[(0, 0)] = PanelColor.White brain = Computer(p, Queue(), Queue()) robot = EmergencyHullPaintingRobot(brain, grid) robot.run_until_stop(verbose=False) print(chargrid_to_str(plot_panels_and_robot(robot))) print(f"ans b in image above") # FKEKCFRK is right
from queue import Queue from util import queue_to_list, thermal_environment_supervision_terminal, Computer if __name__ == "__main__": p = thermal_environment_supervision_terminal() q1 = Queue() q2 = Queue() q1.put(1) c = Computer(p, q1, q2) c.run_until_stop() outputs = queue_to_list(q2) print(f"Ans A:{outputs[-2]}") # 11049715 q1 = Queue() q2 = Queue() q1.put(5) c = Computer(p, q1, q2) c.run_until_stop() outputs = queue_to_list(q2) print(f"Ans B:{outputs[-2]}") # 2140710