예제 #1
0
async def droid(squeue, _input, _output):
    loop = asyncio.get_running_loop()
    surface = Surface(default=-1, mapping={-1: ' ', 0: '#', 1: '.', 2: 'O'})
    pos = (0, 0)
    surface.set(0, 0, -1)
    surface.print_surface()
    while True:
        event = await loop.run_in_executor(None, squeue.get)
        if event.event_type != KEY_DOWN:
            continue
        if event.name == 'x':
            break

        await _input.put(key2direction[event.name])
        result = await _output.get()

        v = key2vector[event.name]
        discovered = (pos[0] + v[0], pos[1] + v[1])

        if result == 0:
            surface.set(*discovered, 0)
        elif result == 1:
            surface.set(*discovered, 1)
            pos = discovered
        elif result == 2:
            surface.set(*discovered, 2)
            break

        surface.print_surface(*pos, 'D')
    surface.print_surface()
예제 #2
0
    s = Surface(default=46, mapping=asciimapper())
    x = 0
    y = 0

    pos = (0, 0)
    for c in output:
        if c == 10:
            y += 1
            x = 0
            continue

        if c == ord('^'):
            pos = (x, y)

        s.set(x, y, c)
        x += 1

    s.print_surface()

    direction = 0

    counter = Counter()
    while direction != -1:
        pos = move(s, pos, direction, counter)
        direction = turn(s, pos, direction)

    s.print_surface()

    print(sum([p[0][0] * p[0][1] for p in counter.items() if p[1] == 2]))
예제 #3
0
파일: aoc15_1.py 프로젝트: husi/aoc19
async def droid(_input, _output):
    surface = Surface(default=-1,
                      mapping={
                          -2: 'X',
                          -1: ' ',
                          0: '#',
                          1: '.',
                          2: 'O'
                      })
    start_point = PointInfo((0, 0), 0)
    floor_map = {start_point.pos: start_point}
    path = [start_point]
    current_point = start_point
    surface.set(0, 0, -2)
    surface.print_surface()

    def step(_pos, _direction):
        return _pos[0] + direction[_direction][0], _pos[1] + direction[
            _direction][1]

    async def look_around():
        sight = []
        for i in range(1, 5):
            target = step(current_point.pos, i)
            await _input.put(i)
            res = await _output.get()
            surface.set(*target, res)
            if res:  # step back
                sight.append(i)
                await _input.put(reverse[i])
                await _output.get()

        # surface.print_surface(*current_point.pos, 'D')
        return sight

    async def move(_direction):
        await _input.put(_direction)
        await _output.get()

    async def backtrack(_path):
        while not _path[-1].undiscovered:
            p = _path.pop()
            await move(reverse[p.arrived_facing_to])  # move back the robot
        return _path[-1]

    possible_directions = await look_around()

    start_point.undiscovered = possible_directions
    while True:
        if not current_point.undiscovered:
            current_point = await backtrack(path)
            continue
        d = current_point.undiscovered.pop()
        target = step(current_point.pos, d)
        if target in floor_map:  # we were there so remove this direction as no more undiscovered
            floor_map[target].undiscovered.remove(reverse(d))
            continue
        await move(d)  # move the robot
        current_point = PointInfo(target, d)
        floor_map[target] = current_point
        path.append(current_point)

        possible_directions = await look_around()
        possible_directions.remove(reverse[d])  # remove where we come from
        current_point.undiscovered = possible_directions

        if surface.surface[current_point.pos] == 2:
            break

    surface.print_surface()

    print(current_point, len(path))