示例#1
0
def task1(intcode):
    program = IntCodeProgram(intcode)

    pos = Pos(0, 0)
    states = {pos: 0}
    moves = [1, 2, 3, 4]
    cost = 0

    for i in range(10000000):
        cost = states[pos] + 1
        move = moves.pop()

        program.add_input(inverse_moves.get(move, move))
        out = program.run()[0]

        if program.terminated:
            print('terminated')
            break

        if out == 0:
            assert move > 0
            continue

        pos = update_pos(pos, inverse_moves.get(move, move))
        if move < 0:
            continue

        if out == 1:
            moves.append(-move)
            if pos not in states or states[pos] > cost:
                states[pos] = cost
                moves.extend(range(1, 5))
        elif 2 == out:
            return (pos, cost)
示例#2
0
def task1(intcode):
    prog = IntCodeProgram(intcode)
    out = prog.run()
    i = out.index(ord('\n'))
    a = np.array(out[:-1]).reshape(-1, i + 1)
    a = a[:, :-1]

    return (seq(
        itertools.product(range(1, a.shape[0] - 1), range(
            1, a.shape[1] -
            1))).filter(lambda x: is_crosspoint(a, x[0], x[1])).map(
                lambda x: x[0] * x[1]).sum())
示例#3
0
def main():
    intcode = lmap(int, input().split(','))
    intcode.extend([0] * 1000)

    computers = []
    for ip in range(50):
        pc = IntCodeProgram(intcode)
        pc.add_input(ip)
        computers.append(pc)

    print("1.)", task1(deepcopy(computers)))
    print("2.)", task2(computers))
示例#4
0
def main():
    intcode = lmap(int, open('input').read().split(','))
    intcode.extend([0] * 1000)

    prog = IntCodeProgram(intcode.copy())
    out = prog.run()
    render(out)
    history = []

    if len(sys.argv) >= 2:
        with open(sys.argv[1], 'r') as f:
            for i in f:
                i = i[:-1]
                history.append(i)
                add_input(prog, i)
                out = prog.run()

    find_subset(prog, history)
示例#5
0
def task2(intcode, O):
    program = IntCodeProgram(intcode)

    pos = Pos(0, 0)
    states = {pos}
    moves = [1, 2, 3, 4]

    while moves:
        move = moves.pop()
        program.add_input(inverse_moves.get(move, move))
        out = program.run()[0]

        if program.terminated:
            print('terminated')
            break

        if out == 0:
            assert move > 0
            continue

        pos = update_pos(pos, inverse_moves.get(move, move))
        if move < 0:
            continue

        if out == 1 or out == 2:
            moves.append(-move)
            if pos not in states:
                states.add(pos)
                moves.extend(range(1, 5))

    cnt = 0
    s = {O}
    seen = {O}
    while s:
        cnt += 1
        q = set()
        for i in s:
            q |= set(update_pos(i, j) for j in range(1, 5))
        s = (q & states) - seen
        seen |= q
    return cnt - 1
示例#6
0
def main():
    line = input()
    intcode = lmap(int, line.split(','))
    intcode.extend([0] * 1000)

    program = IntCodeProgram(intcode)
    out = np.array(program.run())
    out.shape = -1, 3
    d = {(x, y): i for (x, y, i) in out}
    print("1.)", Counter(d.values())[2])

    program = IntCodeProgram(intcode)
    program.intcode[0] = 2
    paddlex = 0
    ballx = 0
    score = 0

    while not program.terminated:
        out = np.array(program.run()).reshape(-1, 3)

        for (x, y, i) in out:
            if x == -1 and y == 0:
                score = i
            elif i == 3:
                paddlex = x
            elif i == 4:
                ballx = x

        if paddlex > ballx:
            joystick = -1
        elif paddlex < ballx:
            joystick = 1
        else:
            joystick = 0
        program.add_input(joystick)

    print("2.)", score)
示例#7
0
def task2(intcode):
    intcode[0] = 2
    prog = IntCodeProgram(intcode)
    out = prog.run()
    out = ''.join(lmap(chr, out))
    robot_pos = out.find('^')
    out = lmap(ord, out[:out.find('\n\n') + 1])
    i = out.index(ord('\n'))
    a = np.array(out).reshape(-1, i + 1)
    robot_pos = robot_pos // a.shape[1] + 1, robot_pos % a.shape[1] + 1
    moves = find_moves(np.pad(a, 1, constant_values=ord('.')), robot_pos)

    # solution found by hand using vim
    m = ('C,A,C,B,C,A,B,C,A,B\n'
         'R,6,L,8,R,10\n'
         'L,8,R,4,R,4,R,6\n'
         'R,12,R,4,R,10,R,12\nn\n')

    for i in m:
        prog.add_input(ord(i))
    out = prog.run()
    return out[-1]
示例#8
0
    1005, 63, 835, 4, 819, 1105, 1, 839, 1001, 64, 1, 64, 1002, 64, 2, 64, 109,
    11, 2105, 1, 7, 1106, 0, 857, 4, 845, 1001, 64, 1, 64, 1002, 64, 2, 64,
    109, 14, 2106, 0, -3, 1001, 64, 1, 64, 1106, 0, 875, 4, 863, 1002, 64, 2,
    64, 109, -22, 21102, 47, 1, 5, 1008, 1013, 48, 63, 1005, 63, 899, 1001, 64,
    1, 64, 1106, 0, 901, 4, 881, 4, 64, 99, 21102, 1, 27, 1, 21102, 915, 1, 0,
    1105, 1, 922, 21201, 1, 65718, 1, 204, 1, 99, 109, 3, 1207, -2, 3, 63,
    1005, 63, 964, 21201, -2, -1, 1, 21102, 1, 942, 0, 1105, 1, 922, 22101, 0,
    1, -1, 21201, -2, -3, 1, 21102, 957, 1, 0, 1106, 0, 922, 22201, 1, -1, -2,
    1105, 1, 968, 21201, -2, 0, -2, 109, -3, 2105, 1, 0
]

ex1 = [
    109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99
]
ex2 = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
ex3 = [104, 1125899906842624, 99]

prog = IntCodeProgram(day9_input, [1])

#while prog.status != Status.HALTED:
print(prog.process())

#203 too low
#4175314595

############################################################
# PART 2

prog = IntCodeProgram(day9_input, [2])

print(prog.process())
示例#9
0
    109, -4, 2105, 1, 0, 109, 5, 1207, -3, 1, 10, 1006, 10, 568, 2207, -4, -2,
    10, 1006, 10, 568, 22102, 1, -4, -4, 1106, 0, 636, 22102, 1, -4, 1, 21201,
    -3, -1, 2, 21202, -2, 2, 3, 21102, 587, 1, 0, 1105, 1, 545, 21201, 1, 0,
    -4, 21101, 0, 1, -1, 2207, -4, -2, 10, 1006, 10, 606, 21102, 0, 1, -1,
    22202, -2, -1, -2, 2107, 0, -3, 10, 1006, 10, 628, 22102, 1, -1, 1, 21102,
    1, 628, 0, 105, 1, 503, 21202, -2, -1, -2, 22201, -4, -2, -4, 109, -5,
    2106, 0, 0
]

program = day11_input

panel_map = dict()
robot_position = (0, 0)
robot_angle = 90

paint_robot = IntCodeProgram(program, [])
new_pos_counter = 0

while paint_robot.status != Status.HALTED:
    #scan current pos
    if robot_position not in panel_map.keys():  # first time at position
        panel_map[robot_position] = Color.BLACK.value
        new_pos_counter += 1

    paint_robot.set_input(panel_map[robot_position])
    paint_color = paint_robot.process()
    rotate_inst = paint_robot.process()

    #paint
    panel_map[robot_position] = paint_color
示例#10
0

def add_input(inp, prog):
    for i in map(ord, inp):
        prog.add_input(i)


def add_commands(cmds, prog, last='WALK'):
    for cmd in cmds:
        add_input(cmd + '\n', prog)
    add_input(f'{last}\n', prog)


task1_script = ('NOT A T', 'NOT B J', 'OR T J', 'NOT C J', 'OR T J', 'AND D J')

prog = IntCodeProgram(intcode.copy())
add_commands(task1_script, prog, last='WALK')
out = prog.run()
print("1.)", out[-1])

task2_script = (
    # if 3rd is hole and 4 and 5 is solid jump
    'NOT C J',
    'AND D J',
    'AND H J',

    # if 2nd is hole and 4 is solid jump
    'NOT B T',
    'AND D T',
    'OR T J',
示例#11
0
    2, 223, 1006, 224, 494, 1001, 223, 1, 223, 107, 677, 677, 224, 1002, 223,
    2, 223, 1006, 224, 509, 101, 1, 223, 223, 7, 677, 677, 224, 102, 2, 223,
    223, 1006, 224, 524, 1001, 223, 1, 223, 1007, 226, 677, 224, 1002, 223, 2,
    223, 1005, 224, 539, 1001, 223, 1, 223, 8, 226, 677, 224, 1002, 223, 2,
    223, 1005, 224, 554, 101, 1, 223, 223, 8, 677, 677, 224, 102, 2, 223, 223,
    1005, 224, 569, 101, 1, 223, 223, 7, 226, 677, 224, 102, 2, 223, 223, 1006,
    224, 584, 1001, 223, 1, 223, 1007, 226, 226, 224, 102, 2, 223, 223, 1006,
    224, 599, 1001, 223, 1, 223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1006,
    224, 614, 1001, 223, 1, 223, 1108, 677, 226, 224, 1002, 223, 2, 223, 1005,
    224, 629, 1001, 223, 1, 223, 1007, 677, 677, 224, 102, 2, 223, 223, 1006,
    224, 644, 1001, 223, 1, 223, 108, 226, 677, 224, 102, 2, 223, 223, 1005,
    224, 659, 101, 1, 223, 223, 8, 677, 226, 224, 1002, 223, 2, 223, 1006, 224,
    674, 1001, 223, 1, 223, 4, 223, 99, 226
]

my_program = IntCodeProgram(program, [5])
#out = my_program.process()
#print(out)

# Day 7 - Part 1

ex1 = [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
ex2 = [
    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
]
ex3 = [
    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
]
day7_input = [
示例#12
0
    86, 63, 51, 7, 29, 16, 5, 94, 15, 53, 26, 69, 67, 21, 38, 13, 65, 78, 34,
    94, 58, 25, 33, 14, 12, 57, 67, 96, 18, 79, 37, 64, 83, 23, 59, 23, 52, 13,
    50, 88, 98, 26, 11, 85, 39, 36, 47, 10, 77, 4, 81, 25, 6, 14, 11, 45, 72,
    70, 94, 2, 54, 23, 83, 95, 58, 20, 25, 15, 24, 69, 35, 96, 70, 93, 79, 79,
    5, 39, 83, 43, 29, 4, 64, 82, 52, 16, 84, 36, 89, 31, 21, 90, 41, 39, 23,
    35, 83, 65, 89, 53, 6, 64, 68, 55, 59, 57, 17, 78, 92, 6, 17, 1, 84, 86,
    19, 78, 69, 34, 12, 36, 41, 60, 16, 37, 24, 31, 31, 91, 13, 93, 38, 17, 80,
    25, 37, 9, 49, 59, 96, 80, 68, 64, 40, 35, 45, 10, 16, 13, 23, 33, 52, 63,
    84, 9, 93, 31, 40, 70, 69, 19, 22, 79, 25, 20, 47, 83, 40, 29, 86, 96, 84,
    23, 31, 42, 82, 87, 83, 5, 70, 25, 15, 23, 77, 41, 31, 73, 2, 3, 74, 69,
    44, 31, 10, 96, 52, 93, 88, 98, 56, 11, 55, 47, 34, 86, 63, 7, 11, 86, 77,
    77, 39, 75, 44, 31, 58, 10, 20, 1, 751761
]

program = day13_input
game = IntCodeProgram(program, [])

block_tiles = 0
while game.status != Status.HALTED:
    x = game.process()
    y = game.process()
    tile_id = game.process()

    if tile_id == 2:
        block_tiles += 1

print(block_tiles)

############################################################
# PART 2
示例#13
0
 def get_output(x, y):
     prog = IntCodeProgram(intcode.copy())
     prog.add_input(x)
     prog.add_input(y)
     return prog.run()[0]