コード例 #1
0
def part1():
    # Find the robot.

    robot = Point(maze.maze[-1].find('^'), len(maze.maze)-1)
    lastturn = robot

    # Robot starts facing north.

    facing = Point(0,-1) 

    crossings = set()
    turns = []
    while 1:
        #  If we are facing empty space, turn.
        if maze[robot+facing] == '.':
            if robot != lastturn:
                turns.append( robot.dist(lastturn) )
            if maze[robot+facing.left()] == '#':
                facing = facing.left()
                turns.append( "L" )
            elif maze[robot+facing.right()] == '#':
                facing = facing.right()
                turns.append( "R" )
            else:
                # End of the scaffold.
                break
            lastturn = robot
            robot += facing

        # Otherwise, look for a crossing.
        else:
            if maze[robot+facing.left()] == '#' and maze[robot+facing.right()] == '#':
                crossings.add( robot )
            robot += facing

        if TRACE:
            print( robot )
    # We return the set of crossings and the record of turns.
    return crossings, turns
コード例 #2
0
def part2():
    ship = Point(0, 0)
    waypt = Point(10, -1)
    for ln in data:
        direc = ln[0]
        dist = int(ln[1:])
        if direc in 'NESW':
            waypt += deltas[direc] * dist
        elif direc in 'L':
            count = dist // 90
            for i in range(count):
                waypt = waypt.left()
        elif direc in 'R':
            count = dist // 90
            for i in range(count):
                waypt = waypt.right()
        elif direc == 'F':
            ship += waypt * dist
        if DEBUG:
            print(ln, ship, waypt)
    return ship.mandist()