-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.py
58 lines (40 loc) · 1017 Bytes
/
03.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
data = open('03.input').read()
# Part 1:
directions = {
'>' : (0 , 1),
'<' : (0 , -1),
'^' : (1 , 0),
'v' : (-1 , 0),
}
position = (0, 0)
visited = { (0,0): 0}
for s in data:
if s not in directions:
continue
newx, newy = directions[s]
x,y = position
position = (x + newx, y + newy)
if position not in visited:
visited[position] = 0
else:
visited[position] += 1
print("Solution part 1: %d" % len(visited.items()))
# Part 2:
positions = ((0, 0), (0,0))
visited = { (0,0): 2}
for i, s in enumerate(data):
if s not in directions:
continue
newx, newy = directions[s]
position = positions[i%2]
x,y = position
position = (x + newx, y + newy)
if position not in visited:
visited[position] = 0
else:
visited[position] += 1
if i%2 == 0:
positions = (position, positions[1])
else:
positions = (positions[0], position)
print("Solution part 2: %d" % len(visited.items()))