robot

mail@pastecode.io avatar
unknown
python
2 years ago
477 B
1
Indexable
Never
def parse_commands(filename):
    x = y = 0

    coordinate_by = lambda direction, distance, x, y: {
            'North': (x, y + distance),
            'East' : (x + distance, y),
            'South': (x, y - distance),
            'West' : (x - distance, y),
        }.get(direction)

    with open(filename) as lines:
        for line in lines:
            direction, distance = line.split()
            x, y = coordinate_by(direction, int(distance), x, y)

    return x, y