-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.py
More file actions
95 lines (86 loc) · 2.31 KB
/
day6.py
File metadata and controls
95 lines (86 loc) · 2.31 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
l = []
with open("d6big.txt", "r") as fin:
lines = fin.readlines()
for x in lines:
l.append(list(x.strip()))
startI = 0
startJ = 0
for i in range(len(l)):
for j in range(len(l[i])):
if (l[i][j] == "^"):
startI = i
startJ = j
# part 1
i = startI
j = startJ
dirY = -1
dirX = 0
visited = set()
while ((i >= 0 and i < len(l)) and (j >= 0 and j < len(l[i]))):
if (l[i][j] == "#"):
# turn
if (dirY == 0 and dirX == 1): # facing right
i += 1 # go down
j -= 1
dirY = 1
dirX = 0
elif (dirY == 1 and dirX == 0): # facing down
i -= 1 # go left
j -= 1
dirY = 0
dirX = -1
elif (dirY == -1 and dirX == 0): # facing up
i += 1 # go right
j += 1
dirY = 0
dirX = 1
elif (dirY == 0 and dirX == -1): # facing left
i += -1 # go up
j += 1
dirY = -1
dirX = 0
else:
visited.add((i, j))
# move forward
i += dirY
j += dirX
print(len(visited))
# part 2
def testLoop(i, j, dirY, dirX, blockI, blockJ):
steps = 0
while ((i >= 0 and i < len(l)) and (j >= 0 and j < len(l[i]))):
if (steps > 10000):
return True
if ((i == blockI and j == blockJ) or l[i][j] == "#"):
# turn
if (dirY == 0 and dirX == 1): # facing right
i += 1 # go down
j -= 1
dirY = 1
dirX = 0
elif (dirY == 1 and dirX == 0): # facing down
i -= 1 # go left
j -= 1
dirY = 0
dirX = -1
elif (dirY == -1 and dirX == 0): # facing up
i += 1 # go right
j += 1
dirY = 0
dirX = 1
elif (dirY == 0 and dirX == -1): # facing left
i += -1 # go up
j += 1
dirY = -1
dirX = 0
else:
# move forward
i += dirY
j += dirX
steps += 1
return False
uniquePos = set()
for pos in visited:
if (testLoop(startI, startJ, -1, 0, pos[0], pos[1])):
uniquePos.add((pos[0], pos[1]))
print(len(uniquePos))