advent/01-1.py

18 lines
434 B
Python
Raw Normal View History

2017-01-14 00:34:25 +00:00
# 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))