Beispiel #1
0
def run_program_sequence(instructions: List[int], phase_settings):
    input = 0
    for setting in phase_settings:
        amp = Intcode(instructions, [setting, input])
        amp.run_program()
        input = amp.outputs[0]
    return input
def create_map(instructions, height, width, xs=0, ys=0):
    map = {}
    for y in range(ys, ys + height):
        for x in range(xs, xs + width):
            computer = Intcode(instructions, [x, y])
            computer.run_program()
            map[Point(x, y)] = computer.get_output()
    return map
Beispiel #3
0
def part1(instructions):
    computer = Intcode(instructions, [])
    computer.run_program()
    map = create_map(computer.outputs)
    render(map)
    intersections = get_intersections(map)
    parameters = []
    for i in intersections:
        parameters.append(i.x * i.y)
    print(sum(parameters))
Beispiel #4
0
def part2(instructions):
    computer = Intcode(instructions, [])
    computer.memory[0] = 2
    computer.run_program()
    state = create_state(computer.outputs)
    render(computer, state)
    while computer.stopped == False:
        handle_input(computer, state)
        update(computer, state)
        render(computer, state)
    render(computer, state)
def get_beam_height(instructions, x):
    y = 0
    start_pos = 0
    end_pos = 0
    while True:
        computer = Intcode(instructions, [x, y])
        computer.run_program()
        output = computer.get_output()
        if start_pos == 0 and output == 1:
            start_pos = y
        elif output == 1:
            end_pos = y
        if end_pos != 0 and output == 0:
            break
        y += 1
    return (end_pos - start_pos, start_pos, end_pos)
def get_beam_width(instructions, y, start_x_at=0):
    x = start_x_at
    start_pos = 0
    end_pos = 0
    while True:
        computer = Intcode(instructions, [x, y])
        computer.run_program()
        output = computer.get_output()
        if start_pos == 0 and output == 1:
            start_pos = x
        elif output == 1:
            end_pos = x
        if end_pos != 0 and output == 0:
            break
        x += 1
    return (end_pos - start_pos, start_pos, end_pos)
Beispiel #7
0
def main():
    with open(sys.argv[1], 'r') as program:
        code = [int(i) for i in program.readline().split(",")]
    inpt = int(sys.argv[2])

    computer = Intcode(code)
    output = computer.run_program([inpt])
    print(output)
Beispiel #8
0
def main():
    with open(sys.argv[1], 'r') as program:
        code = [int(i) for i in program.readline().split(",")]
    input_vals = [int(i) for i in sys.argv[2:]]

    computer = Intcode(code)
    output = computer.run_program(input_vals)

    print(output)
Beispiel #9
0
def explore_map(instructions):
    computer = Intcode(instructions, [])
    computer.run_program()
    pos = Point(0,0)
    map = {}
    map[pos] = 1
    while not computer.stopped:
        next = calculate_next_move(pos, map)
        if next == None:
            break
        direction = get_direction(pos, next)
        computer.set_input(direction)
        computer.run_program()
        output = computer.get_output()
        if output == 0:
            map[next] = 0
        elif output == 1:
            map[next] = 1
            pos = next
        elif output == 2:
            map[next] = 2
            pos = next
            print(find_shortest_distance((0,0), pos, map))
    print(fill_with_oxygen(map))
Beispiel #10
0
def paint_hull(code: List[int], starting_colour: int) -> Dict:
    computer = Intcode(code)
    dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)]
    cur_dir = 0
    x = y = 0
    hull = {(x, y): starting_colour}

    while not computer.halted:
        output = computer.run_program([hull.get((x, y), 0)])

        # Paint the hull
        hull[(x, y)] = output.popleft()
        # Update move the robot
        cur_dir = (cur_dir + 1) if output.popleft() == 1 else (cur_dir - 1)
        cur_dir = cur_dir % len(dirs)
        x += dirs[cur_dir][0]
        y += dirs[cur_dir][1]

    return hull
Beispiel #11
0
def part1(instructions):
    computer = Intcode(instructions, [])
    computer.run_program()
    tiles = create_state(computer.outputs)
    block_tiles = [k for k, v in tiles.items() if v == 2]
    return len(block_tiles)
def test_run_program(instructions, inputs, expected):
    intcode = Intcode(instructions, inputs)
    intcode.run_program()
    assert intcode.outputs == expected
Beispiel #13
0
def part1(instructions):
    intcode = Intcode(instructions, [1], True)
    intcode.run_program()
    return intcode.outputs
def get_value(instructions, x, y):
    computer = Intcode(instructions, [x, y])
    computer.run_program()
    return computer.get_output()
Beispiel #15
0
def part2(instructions):
    computer = Intcode(instructions, [])
    computer.run_program()
    map = create_map(computer.outputs)
    robot = [key for (key, value) in map.items() if value == '^'][0]
    dir = Point(0, -1)
    movements = []
    while True:
        #walk to edge
        dist = walk_to_edge(robot, dir, map)
        if dist > 0:
            movements.append(dist)
            robot = Point(robot.x + dir.x * dist, robot.y + dir.y * dist)
        #try turn
        direction = turn(robot, dir, map)
        if direction != None:
            movements.append(direction[0])
            dir = direction[1]
        if dist == 0 and direction == None:
            break
    print(movements)
    computer = Intcode(instructions, [])
    computer.memory[0] = 2
    computer.run_program()
    #Main routine: [A,B,B,C,C,A,B,B,C,A]
    computer.inputs += convert_to_ascii('A,B,B,C,C,A,B,B,C,A')
    computer.run_program()
    print_message(computer.outputs)
    #Function A: 'R', 4, 'R', 12, 'R', 10, 'L', 12
    computer.inputs += convert_to_ascii('R,4,R,12,R,10,L,12')
    computer.run_program()
    print_message(computer.outputs)
    #Function B: 'L', 12, 'R', 4, 'R', 12
    computer.inputs += convert_to_ascii('L,12,R,4,R,12')
    computer.run_program()
    print_message(computer.outputs)
    #Function C: 'L', 12, 'L', 8, 'R', 10
    computer.inputs += convert_to_ascii('L,12,L,8,R,10')
    computer.run_program()
    print_message(computer.outputs)
    computer.inputs += convert_to_ascii('n')
    computer.run_program()
    print(computer.outputs[-1])