Exemplo n.º 1
0
 def __init__(self, program, initial_inputs):
     self.ic = Intcode(program.copy() + [0] * 10000,
                       initial_inputs=initial_inputs)
     self.pos = (0, 0)
     self.facing = (0, 1)
     self.panel_state = {}
     self.output_count = 0
Exemplo n.º 2
0
def solve1(program):
    max_signal = 0
    for perm in permutations([0,1,2,3,4]):
        last_output = 0
        for phase in perm:
            working_program = program.copy()
            comp = Intcode(working_program, [phase, last_output])
            last_output = comp.process()
        if last_output > max_signal:
            max_signal = last_output
    print("Maximum signal = {}".format(max_signal))
    return max_signal
Exemplo n.º 3
0
def solve(file_name: str = "input"):
    opcode = Intcode(
        [int(n.strip()) for n in open(file_name).read().split(",")])

    print(opcode.run(12, 2))

    for noun in range(100):
        for verb in range(100):
            result = opcode.run(noun, verb)
            if result == 19690720:
                print(100 * noun + verb)
                break
Exemplo n.º 4
0
class Robot:
    def __init__(self, program, initial_inputs):
        self.ic = Intcode(program.copy() + [0] * 10000,
                          initial_inputs=initial_inputs)
        self.pos = (0, 0)
        self.facing = (0, 1)
        self.panel_state = {}
        self.output_count = 0

    def run(self):
        self.ic.add_output_listener(self.handle_output)
        self.ic.process()

    def handle_output(self):
        self.output_count += 1
        if self.output_count % 2 == 0:
            color = self.ic.output_log[-2:][0]
            turn_dir = self.ic.output_log[-2:][1]
            self.panel_state[self.pos] = color  # paint current location
            self.rotate(turn_dir)
            self.forward()

            # provide new input: color of panel in new position
            if self.pos in self.panel_state:
                panel_color = self.panel_state[self.pos]
            else:
                panel_color = 0  # haven't visited this panel, so it's black
            self.ic.inputs.append(panel_color)

    # update self.facing based on turn_dir
    def rotate(self, turn_dir):
        facings = [(0, 1), (1, 0), (0, -1), (-1, 0)]
        if turn_dir == 0:  # turn left
            new_idx = (facings.index(self.facing) - 1) % 4
        elif turn_dir == 1:
            new_idx = (facings.index(self.facing) + 1) % 4
        else:
            print(f"Unexpected turn dir {turn_dir}!!!")
        self.facing = facings[new_idx]

    # move forward in current direction
    def forward(self):
        self.pos = (self.pos[0] + self.facing[0], self.pos[1] + self.facing[1])

    def get_panel_bounds(self):
        min_x = min(self.panel_state, key=lambda pos: pos[0])[0]
        max_x = max(self.panel_state, key=lambda pos: pos[0])[0]
        min_y = min(self.panel_state, key=lambda pos: pos[1])[1]
        max_y = max(self.panel_state, key=lambda pos: pos[1])[1]
        return min_x, max_x, min_y, max_y
Exemplo n.º 5
0
def part_one():
    block_count = 0
    game = Intcode(my_input[:])

    while 1:
        x = game.process()
        if x is None:
            break
        y = game.process()
        tile = game.process()

        if tile == BLOCK:
            block_count += 1

    print(block_count)
Exemplo n.º 6
0
def part_two(data):

    max = 0
    for a in range(5):
        for b in range(5):
            if b == a:
                continue
            for c in range(5):
                if c in [a, b]:
                    continue
                for d in range(5):
                    if d in [a, b, c]:
                        continue
                    for e in range(5):
                        if e in [a, b, c, d]:
                            continue

                        phases = [x + 5 for x in [a, b, c, d, e]]

                        amp = []
                        for p in phases:
                            amp.append(Intcode(data[:], p))

                        cur = 0
                        last_result = 0
                        while (last_result is not None):
                            last_result = amp[cur].process(last_result)
                            cur = (cur + 1) % 5

                        if amp[4].getOutput() > max:
                            max = amp[4].getOutput()

    return max
Exemplo n.º 7
0
def solve2(program):
    max_signal = 0
    for perm in permutations([5,6,7,8,9]):
        last_output = 0
        computers = []
        for phase in perm:
            new_comp = Intcode(program.copy(), [phase, last_output])
            last_output = new_comp.process(stop_on_output=True)
            computers.append(new_comp)
        while not computers[4].halted:
            for c in computers:
                c.inputs = [last_output]
                last_output = c.process(stop_on_output=True)
        if last_output > max_signal:
            max_signal = last_output
    print("Maximum signal (part 2) = {}".format(max_signal))
    return max_signal
Exemplo n.º 8
0
def part_two():
    robot = Intcode(my_input[:])

    panels = {(0, 0): 1}
    painted = set()
    x = y = 0  # start at center
    cur_dir = 0

    while (1):
        if (x, y) not in panels:
            panels[(x, y)] = 0  # black
        robot.addInput(panels[(x, y)])

        new_color = robot.process()
        if new_color is None:
            print(len(painted))
            return panels
        panels[(x, y)] = new_color
        painted.add((x, y))

        new_dir = robot.process()
        if new_dir is None:
            print('error')
            exit()
        cur_dir = (cur_dir + (1 if new_dir else -1)) % 4

        delta = dirs[cur_dir]
        x += delta[0]
        y += delta[1]
Exemplo n.º 9
0
def part_two(verbose=False):
    game = Intcode(my_input[:])
    game.hack(0, 2)

    board = [[0 for i in range(HEIGHT)] for _ in range(WIDTH)]
    b_x = p_x = score = None
    while 1:
        x = game.process()
        if x is None:
            break
        elif x == []:
            if b_x is None or p_x is None:
                print('need input but incomplete data')
                exit()

            if b_x > p_x:
                joystick = 1
            elif b_x < p_x:
                joystick = -1
            else:
                joystick = 0

            game.addInput(joystick)
            continue

        y = game.process()
        tile = game.process()

        board[x][y] = tile

        if tile == BALL:
            b_x = x
        elif tile == PADDLE:
            p_x = x

        if x == -1 and y == 0:
            score = tile
            if verbose:
                display(board)
                print('score:', score)

    print(score)
Exemplo n.º 10
0
def part_one(data):
    max = 0
    for a in range(5):
        for b in range(5):
            if b == a:
                continue
            for c in range(5):
                if c in [a, b]:
                    continue
                for d in range(5):
                    if d in [a, b, c]:
                        continue
                    for e in range(5):
                        if e in [a, b, c, d]:
                            continue

                        result = 0
                        for phase in [a, b, c, d, e]:
                            amp = Intcode(data[:])
                            result = amp.process([phase, result])
                        if result > max:
                            max = result

    return max
Exemplo n.º 11
0
def solve(program):
    # comp = Intcode(program.copy() + [0]*10000, [1]) # part 1
    comp = Intcode(program.copy() + [0]*10000, [2]) # part 2
    last_output = comp.process()
    print("Last output = {}".format(last_output))
Exemplo n.º 12
0
def solve2(program, initial_input):
    ic = Intcode(program, initial_input)
    return ic.process()
Exemplo n.º 13
0
    1010, 829, 1106, 0, 835, 4, 819, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -4,
    21107, 46, 45, 10, 1005, 1016, 855, 1001, 64, 1, 64, 1106, 0, 857, 4, 841,
    1002, 64, 2, 64, 109, 3, 21101, 47, 0, 5, 1008, 1014, 47, 63, 1005, 63,
    883, 4, 863, 1001, 64, 1, 64, 1106, 0, 883, 1002, 64, 2, 64, 109, 10, 1205,
    2, 901, 4, 889, 1001, 64, 1, 64, 1105, 1, 901, 4, 64, 99, 21102, 27, 1, 1,
    21102, 915, 1, 0, 1106, 0, 922, 21201, 1, 13433, 1, 204, 1, 99, 109, 3,
    1207, -2, 3, 63, 1005, 63, 964, 21201, -2, -1, 1, 21101, 0, 942, 0, 1106,
    0, 922, 22102, 1, 1, -1, 21201, -2, -3, 1, 21102, 1, 957, 0, 1105, 1, 922,
    22201, 1, -1, -2, 1106, 0, 968, 21202, -2, 1, -2, 109, -3, 2106, 0, 0
]
example = [
    109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99
]

print('quine example:')
output = []
cpu = Intcode(example[:])
result = cpu.process()
while (result is not None):
    output.append(result)
    result = cpu.process()
print(output)

print('day one:')
cpu = Intcode(my_input[:], 1)
print(cpu.process())

print('day two:')
cpu = Intcode(my_input[:], 2)
print(cpu.process())