Exemplo n.º 1
0
def part1(input_instruction):
    with open('inputs/day9.txt') as f:
        seq = f.read().rstrip('\n').split(',')
        computer = Intcode()
        computer.initialize_memory(seq)
        computer.set_pointer(0)
        computer.test(input_instruction)
Exemplo n.º 2
0
def part2(input_instruction):
    output = -1
    with open('inputs/day9.txt') as f:
        seq = f.read().rstrip('\n').split(',')
        computer = Intcode()
        computer.initialize_memory(seq)
        computer.set_pointer(0)
        output = computer.calculate(input_instruction)
    return output[-1]
Exemplo n.º 3
0
def get_map(seq, input_instruction):
    computer = Intcode()
    computer.initialize_memory(seq)
    computer.set_pointer(0)
    mapping = defaultdict(int)
    # initial position
    color, direc = computer.calculate(input_instruction)
    curr_pos = [0, 0]
    curr_ang = 0
    # running Intcode
    while computer.pointer != -1:
        mapping[str(curr_pos[0]) + ',' + str(curr_pos[1])] = color
        if direc == 0:
            # left 90 degrees
            curr_ang += 90
        elif direc == 1:
            # right 90 degrees
            curr_ang -= 90
        # adjusting to a range from 0-360
        if curr_ang == -90:
            curr_ang = 270
        elif curr_ang == 360:
            curr_ang = 0
        # moving one step in the defined direction
        if curr_ang == 0:
            curr_pos = [curr_pos[0] + 1, curr_pos[1]]
        elif curr_ang == 90:
            curr_pos = [curr_pos[0], curr_pos[1] + 1]
        elif curr_ang == 180:
            curr_pos = [curr_pos[0] - 1, curr_pos[1]]
        elif curr_ang == 270:
            curr_pos = [curr_pos[0], curr_pos[1] - 1]
        # getting next coordinates
        color, direc = computer.calculate(mapping[str(curr_pos[0]) + ',' +
                                                  str(curr_pos[1])])
    return mapping