# Python! import collections with open('01.txt') as file: turns = file.read().replace(' ', '').split(',') moves = collections.defaultdict(int) direction = collections.deque(['N', 'E', 'S', 'W']) for turn in turns: direction.rotate(1 if turn[0] == 'L' else -1) dist = int(turn[1:]) moves[direction[0]] += dist vert = abs(moves['N'] - moves['S']) hor = abs(moves['E'] - moves['W']) print('Distance in blocks:', str(vert + hor))