-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-1.2.py
70 lines (61 loc) · 1.42 KB
/
day-1.2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import re
data = None
with open("input.txt",encoding="utf-8") as inpf:
data = inpf.read().split(",")
data = [x.strip() for x in data if x.strip()]
turns = {
"NR":"E",
"NL":"W",
"ER":"S",
"EL":"N",
"SR":"W",
"SL":"E",
"WR":"N",
"WL":"S",
}
currentposition = "N"
x = 0
y = 0
visited = []
found = None
for d in data:
newposition = turns[currentposition+d[:1]]
if newposition == "W":
for t in range(1,int(d[1:])+1):
if (x-t,y) not in visited:
visited.append((x-t,y))
else:
visited.append((x-t,y))
found = found if found else (x-t,y)
break
x = x - int(d[1:])
if newposition == "E":
for t in range(1,int(d[1:])+1):
if (x+t,y) not in visited:
visited.append((x+t,y))
else:
visited.append((x+t,y))
found = found if found else (x-t,y)
break
x = x + int(d[1:])
if newposition == "N":
for t in range(1,int(d[1:])+1):
if (x,y+t) not in visited:
visited.append((x,y+t))
else:
visited.append((x,y+t))
found = found if found else (x-t,y)
break
y = y + int(d[1:])
if newposition == "S":
for t in range(1,int(d[1:])+1):
if (x,y-t) not in visited:
visited.append((x,y-t))
else:
visited.append((x,y-t))
found = found if found else (x-t,y)
break
y = y - int(d[1:])
currentposition = newposition
x,y = found
print(abs(x)+abs(y))