1
0
Fork 0
adventofcode/2016/1/route.py

20 lines
616 B
Python
Executable File

#!/usr/bin/env python3
import sys, re
instructions = re.findall(r"(R|L)([0-9]+)", sys.stdin.readline().strip())
headings = [(0, 1), (1, 0), (0, -1), (-1, 0)]
h, x, y = 0, 0, 0
visited, part2 = [(x, y)], None
for i in instructions:
if i[0] == 'L':
h = (h - 1) % len(headings)
elif i[0] == 'R':
h = (h + 1) % len(headings)
for _ in range(int(i[1])):
x = x + headings[h][0]
y = y + headings[h][1]
if (x, y) in visited and part2 is None:
part2 = (x, y)
else:
visited.append((x, y))
print(abs(x) + abs(y), abs(part2[0]) + abs(part2[1]))