1
0
Fork 0

Puzzle du 1 décembre 2016

This commit is contained in:
Lucidiot 2017-12-06 11:50:48 +01:00
parent 81b461493a
commit 379e242297
1 changed files with 19 additions and 0 deletions

19
2016/1/route.py Normal file
View File

@ -0,0 +1,19 @@
#!/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]))