def solve(inputs: list) -> list: computer = IntcodeInterpreter(inputs) computer.interpret_program() return computer.mem ## MAIN ### tests ins = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] computer = IntcodeInterpreter(ins) #result = solve(ins) computer.interpret_program() result = computer.mem expectd = [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] aoc.assert_msg(f"input={ins} expects output={expectd}, got {result}", expectd == result) ins = [1, 0, 0, 0, 99] expectd = [2, 0, 0, 0, 99] result = solve(ins) aoc.assert_msg(f"input={ins} expects output={expectd}, got {result}", expectd == result) ins = [2, 3, 0, 3, 99] expectd = [2, 3, 0, 6, 99] result = solve(ins) aoc.assert_msg(f"input={ins} expects output={expectd}, got {result}", expectd == result) ins = [2, 4, 4, 5, 99, 0] expectd = [2, 4, 4, 5, 99, 9801]
# ### Day 1: Chronal Calibration # In[ ]: print("Day 1 a") # In[ ]: test = """ +1, -2, +3, +1 """.strip().split(', ') tests = aoc.map_list(int, test) aoc.assert_msg("test 1", 3 == sum(tests)) # In[ ]: ins = aoc.read_file_to_list('./day01/day01.in') print("Day 1 a solution:", sum(aoc.map_list(int, ins))) # In[ ]: print("Day 1 b", "TODO here, but already solved 2018")
# example "1" ins = [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ] computer = IntcodeInterpreter(ins, mem_growable=True) print(f"mem-type={type(computer.mem)}, mem-len={len(computer.mem)}") computer.interpret_program() result = computer.pseudo_stdout print(f"post mem-len={len(computer.mem)}") print(f"result={result}") expected = ins aoc.assert_msg( f"input={aoc.cl(ins)} expects output={aoc.cl(expected)}, got {aoc.cl(result)}", expected == result) # In[ ]: # example "2" ins = [1102, 34915192, 34915192, 7, 4, 7, 99, 0] computer = IntcodeInterpreter(ins, mem_growable=True) print(f"mem-type={type(computer.mem)}, mem-len={len(computer.mem)}") computer.interpret_program() result = computer.pseudo_stdout print(f"post mem-len={len(computer.mem)}") print(f"result={result}") expected = 16
# In[ ]: # example "1", zero input ins = [3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9] # "using position mode" param = 0 # "output 0 if the input was zero or 1 if the input was non-zero" computer = IntcodeInterpreter(ins) computer.store_int_stdin(param) computer.interpret_program() result = computer.pseudo_stdout print(f"result out={result}") result = result[0] expected = 0 aoc.assert_msg(f"input={ins} with param={param} expects output={expected}, got {result}", expected == result) # In[ ]: # example "1", non-zero input ins = [3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9] # "using position mode" param = 99 # "output 0 if the input was zero or 1 if the input was non-zero" computer = IntcodeInterpreter(ins) computer.store_int_stdin(param) computer.interpret_program() result = computer.pseudo_stdout print(f"result out={result}") result = result[0] expected = 1
# In[ ]: ### tests # In[ ]: ## example 1 cards_num = 10 cmds_str = ["deal into new stack"] expected = '9 8 7 6 5 4 3 2 1 0' cmds = parse_commands(cmds_str) cards = shuffle_cards_cmds(cards_num, cmds) result = ' '.join(map_e(str, cards)) aoc.assert_msg( f"input={cards_num}, #cmds={len(cmds)} expects output={expected}, got {result}", expected == result) # In[ ]: ## example 2 cards_num = 10 cmds_str = ["cut 3"] expected = '3 4 5 6 7 8 9 0 1 2' cmds = parse_commands(cmds_str) cards = shuffle_cards_cmds(cards_num, cmds) result = ' '.join(map_e(str, cards)) aoc.assert_msg( f"input={cards_num}, #cmds={len(cmds)} expects output={expected}, got {result}", expected == result)