예제 #1
0
 def __init__(self, area):
     self.location = (0, 0)
     self.area = area
     self.cpu = CPU()
     self.process = Process(Memory(read_program('input.txt')))
     self.process.io.wait_input = self.provide_input
     self.process.io.listeners = [self.handle_output]
     self.latest_direction = Direction.UP
     self.location_gen = next_location_generator(self.location, self.area)
예제 #2
0
    if grid[y][x] != 35:
        return False
    total_neighbours = 0
    for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
        try:
            if grid[y + dy][x + dx] == 35:
                total_neighbours += 1
        except IndexError:
            pass
    return total_neighbours >= 3


if __name__ == '__main__':
    scaffolding = []
    cpu = CPU()
    process = Process(Memory(read_program('input.txt')))
    collector = OutputCollector(
        output_method=OutputCollector.Method.END_INT_EXCLUDE,
        output_until=10,
        callback=lambda val: scaffolding.append(val))
    collector.attach(process)
    cpu.process(process)
    print('\n'.join(''.join(chr(x) for x in sub) for sub in scaffolding))

    total_alignment_parameter = 0
    for y, row in enumerate(scaffolding):
        for x, el in enumerate(row):
            if is_intersection(x, y, scaffolding):
                total_alignment_parameter += x * y
    write_output(total_alignment_parameter)
    print(f'Total alignment: {total_alignment_parameter}')
예제 #3
0
    def move(self):
        self.position = self.direction.move(self.position)


def print_matrix(matrix):
    print('\n'.join(''.join(color.value[1] for color in row)
                    for row in matrix))


def write_output(result, width, height):
    header = f'P6\n{width} {height}\n255\n'
    with open('output.ppm', 'wb') as out_file:
        out_file.write(bytearray(header, 'ascii'))
        out_file.write(bytearray(result))


if __name__ == '__main__':
    raw_memory = read_program('input.txt')
    robot = Robot(raw_memory)
    cpu = CPU()
    grid = Grid()
    robot.run(cpu, grid)
    matrix = grid.get_color_matrix()
    color_array = [
        color_byte for row in matrix for color in row
        for color_byte in color.value[2]
    ]
    write_output(color_array, len(matrix[0]), len(matrix))
    print_matrix(matrix)
예제 #4
0
            if ball[1] < closest[1]:
                closest = ball
        paddle_middle_x = self.paddle[int(len(self.paddle) / 2)]
        if closest[0] == paddle_middle_x:
            return 0
        elif closest[0] > paddle_middle_x:
            return 1
        return -1


def write_output(result):
    with open('output.txt', 'w') as out_file:
        out_file.write(str(result))
        out_file.write('\n')


if __name__ == '__main__':
    game = Game()
    cpu = CPU()
    memory = Memory(read_program('input.txt'))
    # coins are at mem address 0
    # 2 coins means free play
    memory.memory[0] = 2
    process = Process(memory)
    collector = OutputCollector(3, game.handle_program_output)
    collector.attach(process)
    process.io.wait_input = game.handle_program_input
    cpu.process(process)
    write_output(game.score)
    print(f'Game has finished with score {game.score}')