47 lines
853 B
Python
47 lines
853 B
Python
# Python!
|
|
import collections
|
|
|
|
|
|
def step(cardinal, distance):
|
|
global coordinates
|
|
global pathcrossed
|
|
global visited
|
|
|
|
if cardinal in ['N', 'S']:
|
|
cindex = 1
|
|
else:
|
|
cindex = 0
|
|
|
|
for s in range(distance):
|
|
if cardinal in ['N', 'E']:
|
|
coordinates[cindex] += 1
|
|
else:
|
|
coordinates[cindex] -= 1
|
|
|
|
t = (coordinates[0], coordinates[1])
|
|
if t in visited:
|
|
pathcrossed = True
|
|
break
|
|
|
|
visited.append(t)
|
|
|
|
|
|
with open('01.txt') as file:
|
|
turns = file.read().replace(' ', '').split(',')
|
|
|
|
moves = collections.defaultdict(int)
|
|
direction = collections.deque(['N', 'E', 'S', 'W'])
|
|
pathcrossed = False
|
|
coordinates = [0, 0]
|
|
visited = [(0, 0)]
|
|
|
|
for turn in turns:
|
|
direction.rotate(1 if turn[0] == 'L' else -1)
|
|
d = direction[0]
|
|
dist = int(turn[1:])
|
|
|
|
step(d, dist)
|
|
if pathcrossed:
|
|
break
|
|
|
|
print('Distance in blocks:', coordinates[0] + coordinates[1]) |