-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.py
133 lines (101 loc) · 2.2 KB
/
day06.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
grid = []
with open(inputfile,"r") as input:
grid = [line.rstrip() for line in input]
for i,x in enumerate(grid):
pos = x.find("^")
if pos >= 0:
startguard = (i,pos,"^")
break
def get(x,y,grid):
if 0 <= x < len(grid):
if 0 <= y < len(grid[x]):
return grid[x][y]
return "OoB"
def next_pos(guard, grid):
(x,y,facing) = guard
new_x = x
new_y = y
new_facing = facing
if facing == "^":
new_x = x-1
new_facing = ">"
elif facing == ">":
new_y = y+1
new_facing = "v"
elif facing == "<":
new_y = y-1
new_facing = "^"
elif facing == "v":
new_x = x+1
new_facing = "<"
else:
print("huh?!")
new_pos = get(new_x,new_y,grid)
if new_pos == "." or new_pos == "^":
return (new_x,new_y,facing)
elif new_pos == "#":
return (x,y,new_facing)
elif new_pos == "OoB":
return None
def mycopy(x,y):
global grid
newgrid = []
for i in range(len(grid)):
if not i == x:
newgrid.append(grid[i])
else:
new_y = ""
for j in range(len(grid[i])):
if not j == y:
new_y += grid[i][j]
else:
new_y += "#"
newgrid.append(new_y)
return newgrid
path = set()
def run_part1():
global startguard
fullpath = set()
print(startguard)
guard = startguard
while guard:
# print(guard)
path.add((guard[0],guard[1]))
if guard in fullpath:
print(guard)
print("LOOP!")
fullpath.add(guard)
guard = next_pos(guard,grid)
# for line in grid:
# print(line)
# print(path)
print(len(path))
# print(len(fullpath))
def run_part2():
options = 0
for (x,y) in path:
if not grid[x][y] == "^":
newgrid = mycopy(x,y)
# for l in newgrid:
# print(l)
# print()
guard = startguard
fullpath = set()
while guard:
# print(guard)
if guard in fullpath:
# print(guard)
# print("LOOP!")
options += 1
break
fullpath.add(guard)
guard = next_pos(guard,newgrid)
print(options)
run_part1()
run_part2()