Esempio n. 1
0
def main(puzzle_input):
    computer = Intcode(puzzle_input)
    computer.run_program()
    part1 = sum([
        1 for i in range(2, len(computer.output), 3) if computer.output[i] == 2
    ])

    computer = Intcode(puzzle_input)
    computer.memory[0] = 2
    score = 0
    while not computer.finished:
        computer.output.clear()
        computer.run_program()
        output = computer.output
        for i in range(0, len(output), 3):
            #X=-1, Y=0 -> third is score
            if output[i] == -1 and output[i + 1] == 0:
                score = output[i + 2]
            else:
                #3 is a horizontal paddle tile. The paddle is indestructible.
                if output[i + 2] == 3:
                    paddle_pos = output[i]
                #4 is a ball tile. The ball moves diagonally and bounces off objects.
                elif output[i + 2] == 4:
                    ball_pos = output[i]
        if paddle_pos > ball_pos:
            computer.input.append(-1)
        elif paddle_pos < ball_pos:
            computer.input.append(1)
        else:
            computer.input.append(0)

    return part1, score
Esempio n. 2
0
def main(puzzle_input):
    part1 = 0
    for phaseSettings in itertools.permutations(range(5)):
        prev_output = [0]
        for phaseSetting in phaseSettings:
            computer = Intcode(puzzle_input)
            computer.input = [phaseSetting, prev_output[0]]
            computer.run_program()
            part1 = max(part1, computer.output[0])
            prev_output = computer.output

    part2 = 0
    computer_count = 5
    for phaseSettings in itertools.permutations(range(5, 10)):
        computers = [Intcode("0") for _ in range(computer_count)]
        for i in range(computer_count):
            computers[i].memory = computer.memory.copy()
            computers[i].output = [phaseSettings[i]]
        for i in range(computer_count):
            computers[i].input = computers[i - 1].output
        computers[0].input.append(0)

        while computers[0].finished == False:
            for i in range(computer_count):
                computers[i].run_program()
        part2 = max(part2, computers[-1].output[0])

    return part1, part2
Esempio n. 3
0
def tests():
    i = Intcode(
        [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0])
    assert thrusters.thrusters(i, [4, 3, 2, 1, 0]) == 43210
    i = Intcode([
        3, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1,
        24, 23, 23, 4, 23, 99, 0, 0
    ])
    assert thrusters.thrusters(i, [0, 1, 2, 3, 4]) == 54321
    i = Intcode([
        3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33,
        1002, 33, 7, 33, 1, 33, 31, 31, 1, 32, 31, 31, 4, 31, 99, 0, 0, 0
    ])
    assert thrusters.thrusters(i, [1, 0, 4, 3, 2]) == 65210

    prog = [
        3, 26, 1001, 26, -4, 26, 3, 27, 1002, 27, 2, 27, 1, 27, 26, 27, 4, 27,
        1001, 28, -1, 28, 1005, 28, 6, 99, 0, 0, 5
    ]
    i = [Intcode(prog, name=i) for i in 'ABCDE']
    assert thrusters.feedback_thrusters(i, [9, 8, 7, 6, 5]) == 139629729
    prog = [
        3, 52, 1001, 52, -5, 52, 3, 53, 1, 52, 56, 54, 1007, 54, 5, 55, 1005,
        55, 26, 1001, 54, -5, 54, 1105, 1, 12, 1, 53, 54, 53, 1008, 54, 0, 55,
        1001, 55, 1, 55, 2, 53, 55, 53, 4, 53, 1001, 56, -1, 56, 1005, 56, 6,
        99, 0, 0, 0, 0, 10
    ]
    i = [Intcode(prog, name=i) for i in 'ABCDE']
    assert thrusters.feedback_thrusters(i, [9, 7, 8, 5, 6]) == 18216
Esempio n. 4
0
def tests():
    i = Intcode([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50])
    i.execute_next()
    assert i.memory == [1, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]
    i.execute_next()
    assert i.memory == [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]

    i = Intcode([1, 0, 0, 0, 99])
    i.run()
    assert i.memory == [2, 0, 0, 0, 99]

    i = Intcode([2, 3, 0, 3, 99])
    i.run()
    assert i.memory == [2, 3, 0, 6, 99]

    i = Intcode([2, 4, 4, 5, 99, 0])
    i.run()
    assert i.memory == [2, 4, 4, 5, 99, 9801]

    i = Intcode([1, 1, 1, 4, 99, 5, 6, 0, 99])
    i.run()
    assert i.memory == [30, 1, 1, 4, 2, 5, 6, 0, 99]
    assert i.pointer == 8
    i.reset()
    assert i.memory == [1, 1, 1, 4, 99, 5, 6, 0, 99]
Esempio n. 5
0
 def run_two(self, inputs):
     intcode_a = Intcode(self.code[:], [inputs[0], 0], name='A')
     intcode_b = Intcode(self.code[:], [inputs[1]], name='B')
     intcode_c = Intcode(self.code[:], [inputs[2]], name='C')
     intcode_d = Intcode(self.code[:], [inputs[3]], name='D')
     intcode_e = Intcode(self.code[:], [inputs[4]], name='E')
     while intcode_a.is_running() and intcode_b.is_running(
     ) and intcode_c.is_running() and intcode_a.is_running(
     ) and intcode_d.is_running() and intcode_e.is_running():
         intcode_a.run()
         if intcode_b.is_running() and len(intcode_a.outputs) > 0:
             intcode_b.inputs += intcode_a.outputs
             intcode_a.outputs = []
         intcode_b.run()
         if intcode_c.is_running() and len(intcode_b.outputs) > 0:
             intcode_c.inputs += intcode_b.outputs
             intcode_b.outputs = []
         intcode_c.run()
         if intcode_d.is_running() and len(intcode_c.outputs) > 0:
             intcode_d.inputs += intcode_c.outputs
             intcode_c.outputs = []
         intcode_d.run()
         if intcode_e.is_running() and len(intcode_d.outputs) > 0:
             intcode_e.inputs += intcode_d.outputs
             intcode_d.outputs = []
         intcode_e.run()
         if intcode_a.is_running() and len(intcode_e.outputs) > 0:
             intcode_a.inputs += intcode_e.outputs
             intcode_e.outputs = []
     return intcode_e.outputs[0]
Esempio n. 6
0
def test_day9_solutions():
    data = [int(s) for s in open('./day9.input').read().strip().split(',')]
    intcode = Intcode(data[:], inputs=[1], verbose=False)
    intcode.run()
    assert intcode.outputs == [3345854957]
    intcode = Intcode(data[:], inputs=[2], verbose=False)
    intcode.run()
    assert intcode.outputs == [68938]
Esempio n. 7
0
def main():
    vm = Intcode(PROGRAM, [1])
    vm.compute()
    print("day 9/1", vm.outputs)

    vm = Intcode(PROGRAM, [2])
    vm.compute()
    print("day 9/2", vm.outputs)

    test()
Esempio n. 8
0
def main():
    filename = 'input.txt'

    intcode1 = Intcode(filename)
    intcode2 = Intcode(filename)
    print(
        f"Part #1: The diagnosic code output from the test diagnostic program: {intcode1.run([1])[-1]}"
    )
    print(
        f"Part #2: The diagnosic code output from the thermal radiation controller: {intcode2.run([5])[0]}"
    )
Esempio n. 9
0
def main(puzzle_input):
    computer = Intcode(puzzle_input)
    computer.input = [1]
    computer.run_program()
    part1 = computer.output[-1]

    computer = Intcode(puzzle_input)
    computer.input = [5]
    computer.run_program()
    part2 = computer.output[0]

    return part1, part2
Esempio n. 10
0
 def run_one(self, inputs):
     intcode_a = Intcode(self.code[:], [inputs[0], 0])
     intcode_a.run()
     intcode_b = Intcode(self.code[:], [inputs[1], intcode_a.outputs[0]])
     intcode_b.run()
     intcode_c = Intcode(self.code[:], [inputs[2], intcode_b.outputs[0]])
     intcode_c.run()
     intcode_d = Intcode(self.code[:], [inputs[3], intcode_c.outputs[0]])
     intcode_d.run()
     intcode_e = Intcode(self.code[:], [inputs[4], intcode_d.outputs[0]])
     intcode_e.run()
     return intcode_e.outputs[0]
Esempio n. 11
0
def main(puzzle_input):
    computer = Intcode(puzzle_input)
    computer.input.append(1)
    computer.run_program()
    part1 = computer.output[0]
    
    computer = Intcode(puzzle_input)
    computer.input.append(2)
    computer.run_program()
    part2 = computer.output[0]

    return part1, part2
Esempio n. 12
0
def main(puzzle_input):
    computer = Intcode(puzzle_input)
    computer.run_program()

    grid = []
    inner = []
    for c in computer.output:
        if c == 10:
            if len(inner) > 0:
                grid.append(inner)
                inner = []
        else:
            inner.append(chr(c))
        #print(chr(c), end = '')

    part1 = 0
    for x in range(1, len(grid[0]) - 1):
        for y in range(1, len(grid) - 1):
            if grid[y][x] == "#" and all(
                    grid[y + yy][x + xx] == "#"
                    for (xx, yy) in [(0, -1), (1, 0), (0, 1), (-1, 0)]):
                part1 += x * y

    for x in range(len(grid[0])):
        for y in range(len(grid)):
            if grid[y][x] in "^v<>":
                pos = x + y * 1j
                direction = {"^": -1j, "v": 1j, "<": -1, ">": 1}[grid[y][x]]
                break

    commands = find_commands(pos, direction, grid)
    functions = find_functions(commands, [])
    main_routine = create_main_routine(commands, functions)

    program_input = []
    add_function(program_input, main_routine)
    for function in functions:
        add_function(program_input, ",".join(map(str, function)))
    add_function(program_input, "n")
    #add_function(program_input, "A,B,A,B,A,C,A,C,B,C")
    #add_function(program_input, "R,6,L,10,R,10,R,10")
    #add_function(program_input, "L,10,L,12,R,10")
    #add_function(program_input, "R,6,L,12,L,10")
    #add_function(program_input, "n")

    computer = Intcode(puzzle_input)
    computer.memory[0] = 2
    computer.input = program_input
    computer.run_program()
    part2 = computer.output[-1]

    return part1, part2
Esempio n. 13
0
def get_answer(data, part2=False, mode=None):
    program = [int(x) for x in data[0].split(',')]

    if mode is None:
        if part2:
            comp = Intcode(program, secondary=0, input=2)
        else:
            comp = Intcode(program, secondary=0)
    else:
        if part2:
            comp = Intcode(program, mode=mode, secondary=0, input=2)
        else:
            comp = Intcode(program, mode=mode, secondary=0)
    return comp.run()
Esempio n. 14
0
def test_mul_opcode_immediate():
    computer = Intcode([1102, 5, 6, 0, 99, 10, 15], None)
    computer.run_program()
    if computer.program[0] == 30:
        assert True
    else:
        assert False
Esempio n. 15
0
def test_mul_opcode_position():
    computer = Intcode([2, 5, 6, 0, 99, 10, 15], None)
    computer.run_program()
    if computer.program[0] == 150:
        assert True
    else:
        assert False
Esempio n. 16
0
def test_equal_fail():
    computer = Intcode([8, 5, 6, 0, 99, 31, 29], None)
    computer.equals_opcode(0, 0)
    if computer.program[0] == 0:
        assert True
    else:
        assert False
Esempio n. 17
0
def test_equal_succeed():
    computer = Intcode([8, 2, 2, 0, 99], None)
    computer.equals_opcode(0, 0)
    if computer.program[0] == 1:
        assert True
    else:
        assert False
Esempio n. 18
0
def test_less_than_fail():
    computer = Intcode([7, 5, 6, 0, 99, 31, 29], None)
    computer.less_than_opcode(0, 0)
    if computer.program[0] == 0:
        assert True
    else:
        assert False
def move(codes, is_origin_white=False):
    visited = set()
    white_pos = set()
    pos = (0, 0)  # (y, x)
    direction = (-1, 0)  # (y, x): face up

    if is_origin_white:
        white_pos.add(pos)

    intcode = Intcode(0, codes)

    while not intcode.halted():
        inst = [1] if pos in white_pos else [0]
        color, turn = intcode.run(2, inst).get_output()[-2:]

        visited.add(pos)
        if color == 1:
            white_pos.add(pos)
        elif pos in white_pos:
            white_pos.remove(pos)

        direction = right(direction[0], direction[1]) if turn == 1 else left(
            direction[0], direction[1])
        pos = sum(i[0]
                  for i in [pos, direction]), sum(i[1]
                                                  for i in [pos, direction])

    return visited, white_pos
Esempio n. 20
0
def run_amplifiers(file_data):

    combinations = list(permutations(range(5, 10)))
    maximum = 0
    winning_combo = None

    for combination in combinations:
        signal = 0
        visited = list(combination)
        amplifier_outputs = defaultdict(list)
        while visited:
            step = visited.pop(0)
            intcode = Intcode(file_data)
            try:
                if not amplifier_outputs[step]:
                    amplifier_outputs[step].append(0)
                outputs = amplifier_outputs[step]
                signal = amplifier(intcode, amplifiers=[step] + outputs)
                if visited:
                    amplifier_outputs[visited[0]].append(signal)
            except NeedMoreInfoException:
                amplifier_outputs[visited[0]].append(intcode.output)
                visited.append(step)

        if signal > maximum:
            maximum = signal
            winning_combo = combination
    print("Highest value is {} for combination {}".format(
        maximum, winning_combo))
Esempio n. 21
0
def test_jump_true_opcode():
    computer = Intcode([5, 1, 4, 99, 44], None)
    computer.jump_true_opcode(0, 0)
    if computer.pointer == 44:
        assert True
    else:
        assert False
Esempio n. 22
0
def test_jump_false_fail():
    computer = Intcode([6, 2, 1, 99], None)
    computer.jump_false_opcode(0, 0)
    if computer.pointer == 3:
        assert True
    else:
        assert False
Esempio n. 23
0
def test_jump_false_opcode_mixed_2():
    computer = Intcode([6, 4, 1, 99, 0], None)
    computer.jump_false_opcode(0, 1)
    if computer.pointer == 1:
        assert True
    else:
        assert False
Esempio n. 24
0
def test_jump_false_opcode_mixed_1():
    computer = Intcode([6, 0, 4, 99, 44], None)
    computer.jump_false_opcode(1, 0)
    if computer.pointer == 44:
        assert True
    else:
        assert False
Esempio n. 25
0
def find_tank(d, pos, data, trace):
    global order, loc

    # Is the requested direction already explored?
    old_pos = pos
    pos = tuple(map(operator.add, pos, p[d]))
    if pos in explored:
        return

    # Load a copy of the bot
    comp = Intcode()
    bot = comp.load(data)

    # Send it in the requested direction
    r = bot.send(d)
    if r == 0:
        explored[pos] = "."
        pos = old_pos
    elif r == 1:
        explored[pos] = " "
        trace.append(pos)
    elif r == 2:
        explored[pos] = "X"
        order = trace
        loc = old_pos
        trace.append(pos)
    for x in p.keys():
        find_tank(x, pos, comp.save(), trace.copy())
Esempio n. 26
0
def test_add_opcode_mixed_1():
    computer = Intcode([1001, 5, 6, 0, 99, 10, 15], None)
    computer.run_program()
    if computer.program[0] == 16:
        assert True
    else:
        assert False
Esempio n. 27
0
def ask_about_position(data, x, y):
    beam = Intcode()
    beam.load(data)
    beam.add_input(x)
    beam.add_input(y)
    beam.run()
    return beam.get_output()
Esempio n. 28
0
def test_jump_false_fail_mixed_2():
    computer = Intcode([6, 2, 32, 99], None)
    computer.jump_false_opcode(0, 1)
    if computer.pointer == 3:
        assert True
    else:
        assert False
Esempio n. 29
0
def test_jump_false_fail_immediate():
    computer = Intcode([6, 2, 33, 99], None)
    computer.jump_false_opcode(1, 1)
    if computer.pointer == 3:
        assert True
    else:
        assert False
Esempio n. 30
0
def test_less_than_succeed():
    computer = Intcode([7, 1, 2, 0, 99], None)
    computer.less_than_opcode(0, 0)
    if computer.program[0] == 1:
        assert True
    else:
        assert False