Untitled

 avatar
unknown
python
3 years ago
474 B
8
Indexable
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
Editor is loading...