示例#1
0
 def testinputmode(self):
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], [1])
     assert next(m.run()) == 0, 'result is not False for val < 8'
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], [8])
     assert next(m.run()) == 1, 'result is not True for val == 8'
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8], [10])
     assert next(m.run()) == 0, 'result is not False for val > 8'
示例#2
0
def run():
    memory = open_file("input_day17.txt")
    memory[0] = 2
    machine = IntCodeMachine(memory, onread=oninput, onwrite=onoutput)
    machine.run()
    for line in scaffold:
        print(''.join(line))
示例#3
0
 def testjumponeimmode(self):
     m = IntCodeMachine([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 1, 'result is not True for val != 0'
     m.reset()
     with mock.patch('builtins.input', return_value=0):
         assert next(m.run()) == 0, 'result is not False for val == 0'
示例#4
0
 def testjumponeposmode(self):
     m = IntCodeMachine(
         [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 1, 'result is not True for val != 0'
     m.reset()
     with mock.patch('builtins.input', return_value=0):
         assert next(m.run()) == 0, 'result is not False for val == 0'
示例#5
0
def run():
    memory = open_file("input_day17.txt")
    machine = IntCodeMachine(memory, None, onwrite=onoutput)
    machine.run()

    for line in scaffold:
        print(''.join(line))

    print(intersections())
示例#6
0
def run():
    memory = read_input()
    robot = Robot()
    robot.squares[robot.pos] = 1
    machine = IntCodeMachine(memory, robot.read, robot.instruct)
    machine.run()
    
    for y in range(6):
        print(''.join([' ', '█'][robot.squares[x + y * 1j]] for x in range(42)))
示例#7
0
 def testequalseightposmode(self):
     m = IntCodeMachine([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 0, 'result is not False for val < 8'
     m.reset()
     with mock.patch('builtins.input', return_value=8):
         assert next(m.run()) == 1, 'result is not True for val == 8'
     m.reset()
     with mock.patch('builtins.input', return_value=10):
         assert next(m.run()) == 0, 'result is not False for val > 8'
示例#8
0
 def testlesseightimmode(self):
     m = IntCodeMachine([3, 3, 1107, -1, 8, 3, 4, 3, 99])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 1, 'result is not True for val < 8'
     m.reset()
     with mock.patch('builtins.input', return_value=8):
         assert next(m.run()) == 0, 'result is not False for val == 8'
     m.reset()
     with mock.patch('builtins.input', return_value=10):
         assert next(m.run()) == 0, 'result is not False for val > 8'
示例#9
0
def run():
    memory = read_input()
    robot = Robot()
    machine = IntCodeMachine(memory, robot.oninput, robot.onoutput)
    try:
        machine.run()
    except RuntimeError:
        draw_map(robot.map, (0, 0))
        print("Part 1", robot.solution, robot.oxygen)

    oxygen(robot.map, robot.oxygen)
示例#10
0
def run():
    memory = read_input()
    machine = IntCodeMachine(memory, None, output.append)
    machine.run()
    coords = defaultdict(int)
    for n in range(0, len(output), 3):
        x, y, t = output[n:n + 3]
        coords[(x, y)] = t

    print(coords.values())
    print(len([t for t in coords.values() if t == 2]))
示例#11
0
def run():
    memory = open_file("input_day19.txt")
    for y in range(max_y):
        for x in range(max_x):
            lst = [y, x]
            print(lst)
            machine = IntCodeMachine(memory.copy(), lst.pop,
                                     lambda v: update_grid(x, y, v))
            machine.run()

    for line in grid:
        print(''.join(str(x) for x in line))

    print(sum(sum(line) for line in grid))
示例#12
0
 def test(self):
     m = IntCodeMachine([
         3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
         1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104,
         999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99
     ])
     with mock.patch('builtins.input', return_value=1):
         assert next(m.run()) == 999, 'result is not 999 for val < 8'
     m.reset()
     with mock.patch('builtins.input', return_value=8):
         assert next(m.run()) == 1000, 'result is not 1000 for val == 8'
     m.reset()
     with mock.patch('builtins.input', return_value=10):
         assert next(m.run()) == 1001, 'result is not 1001 for val > 8'
示例#13
0
def run():
    memory = read_input()

    machine = IntCodeMachine(memory, None, output.append)
    machine.run()

    field = load_field(glyphs)
    memory = read_input()
    memory[0] = 2

    draw_field(field)
    game = Game(field)
    machine2 = IntCodeMachine(memory, game.oninput, game.onoutput)
    machine2.run()
示例#14
0
 def testone(self):
     m = IntCodeMachine([1002, 4, 3, 4, 33])
     try:
         while True:
             next(m.run())
     except StopIteration:
         assert m.memory[0:5] == [1002, 4, 3, 4, 99]
示例#15
0
 def testnegative(self):
     m = IntCodeMachine([1101, 100, -1, 4, 0])
     try:
         while True:
             next(m.run())
     except StopIteration:
         assert m.memory[0:5] == [1101, 100, -1, 4, 99]
示例#16
0
def loop(memory):
    for u_y in range(1059 - 2, 1059 + 3):
        l_y = u_y + 99

        x = int(l_y * 0.7586)

        for l_x in range(x - 1, x + 2):
            r_x = l_x + 99

            outputs = []
            for x,y in [(l_x, u_y), (r_x, u_y), (l_x, l_y), (r_x, l_y)]:
                lst = [y, x]
                machine = IntCodeMachine(memory.copy(), lst.pop, outputs.append)
                machine.run()
                if (outputs == [1,1,1,1]):
                    return(l_x, u_y)
示例#17
0
 def testrelativebase(self):
     m = IntCodeMachine([
         109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101,
         0, 99
     ])
     try:
         while True:
             next(m.run())
     except StopIteration:
         assert m.output == [
             109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006,
             101, 0, 99
         ]
示例#18
0
def test_day_5_part_2(day_5_input):
    stack_io_handler = StackIOHandler([str(5)])
    machine = IntCodeMachine(day_5_input, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack[-1] == "11189491"
示例#19
0
def test_day_5_part_1(day_5_input):
    stack_io_handler = StackIOHandler([str(1)])
    machine = IntCodeMachine(day_5_input, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack[-1] == "13978427"
示例#20
0
def test_day_5_large_test(input, expected_output):
    code = "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99"  # noqa: E501
    stack_io_handler = StackIOHandler([str(input)])
    machine = IntCodeMachine(code, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack == [str(expected_output)]
示例#21
0
def test_day_5_jump(program, input, expected_output):
    stack_io_handler = StackIOHandler([str(input)])
    machine = IntCodeMachine(program, io_handler=stack_io_handler)
    machine.run()
    assert stack_io_handler.io_stack == [str(expected_output)]
示例#22
0
 def testmore6(self):
     m = IntCodeMachine([109, 1, 209, -1, 204, -106, 99])
     assert next(m.run()) == 204
示例#23
0
def run():
    memory = open_file("input_day25.txt")
    machine = IntCodeMachine(memory, oninput, onoutput)
    machine.run()
示例#24
0
def run():
    memory = open_file("input_day21.txt")
    machine = IntCodeMachine(memory, onread=oninput, onwrite=onoutput)
    machine.run()
示例#25
0
 def testlargenumber(self):
     m = IntCodeMachine([1102, 34915192, 34915192, 7, 4, 7, 99, 0])
     assert int(math.log10(next(m.run()))) + 1 == 16
示例#26
0
 def testlargenumber2(self):
     m = IntCodeMachine([104, 1125899906842624, 99])
     assert next(m.run()) == 1125899906842624
示例#27
0
 def testmore3(self):
     m = IntCodeMachine([109, -1, 204, 1, 99])
     assert next(m.run()) == 109
示例#28
0
from aocd.models import Puzzle

from intcode import IntCodeMachine

if __name__ == '__main__':
    puzzle = Puzzle(year=2019, day=9)
    m = IntCodeMachine(list(map(int, puzzle.input_data.split(','))))
    m.set_inputs([1])
    puzzle.answer_a = next(m.run())
    m.reset()
    m.set_inputs([2])
    puzzle.answer_b = next(m.run())
示例#29
0
def run():
    memory = read_input()
    robot = Robot()
    machine = IntCodeMachine(memory, robot.read, robot.instruct)
    machine.run()
    print(len(robot.squares))
示例#30
0
 def testmore5(self):
     m = IntCodeMachine([109, 1, 109, 9, 204, -6, 99])
     assert next(m.run()) == 204