Beispiel #1
0
def puzzle_1(data):
    machine = IntCode(data)

    command = []

    # x __#
    #  abcd
    # !B && !C && D
    # Jump, if there is a two-space gap with ground afterwards
    command.append("NOT B T")
    command.append("NOT C J")
    command.append("AND T J")
    command.append("AND D J")
    # x_
    # Always jump, if we have to (A is a gap)
    command.append("NOT A T")
    command.append("OR T J")
    # x  _#
    # !C && D
    # Whatever... works. Like the first one, but ignoring B. Don't ask.
    command.append("NOT C T")
    command.append("AND D T")
    command.append("OR T J")

    for c in command:
        machine.set_inputs(list(map(ord, c + "\n")))
    machine.set_inputs(list(map(ord, "WALK\n")))

    outputs = machine.get_outputs()
    # Print output from IntCode machine. This also gives nices images when the puzzle fails
    #print("".join(list(map(chr, outputs[:-1]))))
    return outputs[-1]
Beispiel #2
0
def run_intcode(data, id, other_input_queues, stop_queue, nat_queue):

    m = IntCode(data)
    in_queue = other_input_queues[id]

    m.set_input(id)
    m.set_input(-1)

    while True:

        if not stop_queue.empty():
            return

        while not in_queue.empty():
            in_data = in_queue.get()
            m.set_inputs([in_data[0], in_data[1]])

        out = m.get_outputs()
        if len(out) > 0:
            for oi in range(len(out) // 3):
                if out[oi * 3] == 255:
                    nat_queue.put((out[oi * 3 + 1], out[oi * 3 + 2]))
                else:
                    # directly feeds the correct input queue with data!
                    other_input_queues[out[oi * 3]].put(
                        (out[oi * 3 + 1], out[oi * 3 + 2]))
Beispiel #3
0
def try_beam_count(y):
    dx = 0
    dy = y
    last_value = 0
    beam_cnt = 0

    while True:
        machine = IntCode(data)
        machine.set_inputs([dx, dy])
        beam = machine.get_outputs()[0]

        if beam == 0 and last_value == 1:
            return first_x, dy + 1, beam_cnt
        if beam == 1 and last_value == 0:
            first_x = dx

        beam_cnt += beam
        last_value = beam
        dx += 1
        dy -= 1
    return 0, 0, 0
Beispiel #4
0
def puzzle_2(data):
    machine = IntCode(data)
    command = []

    # x   #
    #  abcdefghi
    # H || E && I || E && F
    # Either the second jump works right away, or we can go one step and then jump, or we can just continue walking with
    # no second jump (At least for two further steps. We deal with what comes after walking/jumping for a bit).
    command.append("OR E T")
    command.append("AND F T")
    command.append("OR E J")
    command.append("AND I J")
    command.append("OR T J")
    command.append("OR H J")
    # !A || !B || !C
    # Don't jump, if there is nothing to jump over!
    command.append("NOT A T")
    command.append("NOT T T")
    command.append("AND B T")
    command.append("AND C T")
    command.append("NOT T T")
    # combine
    command.append("AND T J")
    # D
    # Immediate jump location MUST be ground!
    command.append("AND D J")

    for c in command:
        machine.set_inputs(list(map(ord, c + "\n")))
    machine.set_inputs(list(map(ord, "RUN\n")))

    outputs = machine.get_outputs()
    # Print output from IntCode machine. This also gives nices images when the puzzle fails
    #print("".join(list(map(chr, outputs[:-1]))))
    return outputs[-1]
Beispiel #5
0
        -4, -3, -4, 21202, -3, -1, -1, 22201, -4, -1, 2, 21202, 2, -1, -1,
        22201, -4, -1, 1, 22101, 0, -2, 3, 21102, 343, 1, 0, 1105, 1, 303,
        1106, 0, 415, 22207, -2, -3, -1, 1206, -1, 387, 22201, -3, -2, -3,
        21202, -2, -1, -1, 22201, -3, -1, 3, 21202, 3, -1, -1, 22201, -3, -1,
        2, 21201, -4, 0, 1, 21101, 0, 384, 0, 1105, 1, 303, 1106, 0, 415,
        21202, -4, -1, -4, 22201, -4, -3, -4, 22202, -3, -2, -2, 22202, -2, -4,
        -4, 22202, -3, -2, -3, 21202, -4, -1, -2, 22201, -3, -2, 1, 21201, 1,
        0, -4, 109, -5, 2106, 0, 0
    ]

    field = defaultdict(int)

    for y in range(50):
        for x in range(50):
            machine = IntCode(data)
            machine.set_inputs([x, y])
            beam = machine.get_outputs()[0]
            field[(x, y)] = beam

    # solution for puzzle 1
    print(sum(field.values()))
    #draw(field)

    x = y = 0
    # just make it kinda large.
    diff = 2000
    while True:
        p_x, p_y, cnt = try_beam_count(y)

        if cnt > 100:
            diff //= 2
Beispiel #6
0
        22101, 0, -1, 1, 21102, 646, 1, 0, 106, 0, 521, 21202, -2, -1, -2,
        22201, -4, -2, -4, 109, -5, 2106, 0, 0
    ]

    machine = IntCode(data)

    painted_color = defaultdict(int)
    pos = (0, 0)
    direction = (0, 1)

    # for puzzle 2:
    painted_color[pos] = 1

    while not machine.is_finished():

        machine.set_inputs([painted_color[pos]])
        out = machine.get_outputs()

        painted_color[(pos)] = out[0]

        direction = rotate(direction, not out[1])
        pos = (pos[0] + direction[0], pos[1] + direction[1])

    # Output for puzzle 1:
    print(len(painted_color))

    min_x = min(painted_color.keys(), key=lambda x: x[0])[0]
    max_x = max(painted_color.keys(), key=lambda x: x[0])[0]
    min_y = min(painted_color.keys(), key=lambda x: x[1])[1]
    max_y = max(painted_color.keys(), key=lambda x: x[1])[1]
Beispiel #7
0
        for s in split(states, 3):
            if s[:2] == [-1, 0]:
                # Solution for puzzle 2
                print("\rScore: {}".format(s[2]), end="")
                continue

            # update ball and paddle position
            if s[2] == 4:
                ball_pos_x = s[0]
            elif s[2] == 3:
                last_js_x = s[0]

            # paddle movement
            if ball_pos_x > last_js_x:
                move = 1
            elif ball_pos_x < last_js_x:
                move = -1

            # field update
            field[(s[0], s[1])] = s[2]

        game.set_inputs([move])
        states = game.get_outputs()

    print()

    #draw(field)

# solution for 13.01: 253
# solution for 13.02: 12263
Beispiel #8
0
    #draw(field, {-1: " ", ord("#"): "█", ord("^"): "^"})

    # solution for puzzle 1:
    print(sum(score(x, field) for x in list(field)))

    command = create_movement_command(field, hoover)
    found, bot_command, (a, b, c) = try_combination(
        command, list(set(find_all_combinations(command))))

    if found:
        data[0] = 2
        machine = IntCode(data)
        # will print the map again!
        #print("".join(map(chr, machine.get_outputs())))
        machine.set_inputs(
            list(map(ord, ",".join(list(bot_command)))) + [ord("\n")])
        #print("".join(map(chr, machine.get_outputs())))
        split_a = "".join(
            list(map(lambda x: x[0] + "," + x[1:] + ",", split(a))))[:-1]
        machine.set_inputs(list(map(ord, split_a)) + [ord("\n")])
        #print("".join(map(chr, machine.get_outputs())))
        split_b = "".join(
            list(map(lambda x: x[0] + "," + x[1:] + ",", split(b))))[:-1]
        machine.set_inputs(list(map(ord, split_b)) + [ord("\n")])
        #print("".join(map(chr, machine.get_outputs())))
        split_c = "".join(
            list(map(lambda x: x[0] + "," + x[1:] + ",", split(c))))[:-1]
        machine.set_inputs(list(map(ord, split_c)) + [ord("\n")])
        #print("".join(map(chr, machine.get_outputs())))
        machine.set_inputs([ord("n"), 10])
Beispiel #9
0
        755, 4, 739, 1001, 64, 1, 64, 1105, 1, 755, 1002, 64, 2, 64, 109, 2,
        21101, 47, 0, -2, 1008, 1013, 47, 63, 1005, 63, 777, 4, 761, 1106, 0,
        781, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, 10, 1205, -5, 793, 1106, 0,
        799, 4, 787, 1001, 64, 1, 64, 1002, 64, 2, 64, 109, -1, 2105, 1, -1,
        1001, 64, 1, 64, 1105, 1, 817, 4, 805, 1002, 64, 2, 64, 109, 9, 2105,
        1, -9, 4, 823, 1001, 64, 1, 64, 1105, 1, 835, 1002, 64, 2, 64, 109,
        -36, 2108, 38, 7, 63, 1005, 63, 855, 1001, 64, 1, 64, 1106, 0, 857, 4,
        841, 1002, 64, 2, 64, 109, 13, 2102, 1, -6, 63, 1008, 63, 36, 63, 1005,
        63, 879, 4, 863, 1106, 0, 883, 1001, 64, 1, 64, 1002, 64, 2, 64, 109,
        10, 2106, 0, 8, 4, 889, 1105, 1, 901, 1001, 64, 1, 64, 4, 64, 99,
        21101, 0, 27, 1, 21101, 915, 0, 0, 1106, 0, 922, 21201, 1, 49329, 1,
        204, 1, 99, 109, 3, 1207, -2, 3, 63, 1005, 63, 964, 21201, -2, -1, 1,
        21102, 1, 942, 0, 1105, 1, 922, 21201, 1, 0, -1, 21201, -2, -3, 1,
        21102, 957, 1, 0, 1106, 0, 922, 22201, 1, -1, -2, 1105, 1, 968, 22102,
        1, -2, -2, 109, -3, 2105, 1, 0
    ]
    #data = [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99]
    #data = [104,1125899906842624,99]
    #data = [1102,34915192,34915192,7,4,7,99,0]

    ic = IntCode(data)
    ic.set_inputs([1])
    print(ic.get_outputs())

    ic2 = IntCode(data)
    ic2.set_inputs([2])
    print(ic2.get_outputs())

# solution for 09.01: 2457252183
# solution for 09.02: 70634