File tree Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Expand file tree Collapse file tree 1 file changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -37,14 +37,26 @@ func main() {
37
37
}
38
38
39
39
// Walk the grid
40
- fmt .Println (walkGrid (copyGrid (grid )))
40
+ fmt .Println (walkGrid (copyGrid (grid ), position {- 1 , - 1 }))
41
+
42
+ // Add random obstacles and test again
43
+ var infLoops int
44
+ for y := range len (grid ) {
45
+ for x := range len (grid [0 ]) {
46
+ if walkGrid (copyGrid (grid ), position {x , y }) == - 1 {
47
+ infLoops ++
48
+ }
49
+ }
50
+ }
51
+
52
+ fmt .Println (infLoops )
41
53
}
42
54
43
55
type position struct {
44
56
X , Y int
45
57
}
46
58
47
- func walkGrid (grid [][]byte ) int {
59
+ func walkGrid (grid [][]byte , obstacle position ) int {
48
60
guard := position {guardStart .X , guardStart .Y }
49
61
50
62
stepCount := 1
@@ -58,12 +70,12 @@ func walkGrid(grid [][]byte) int {
58
70
return stepCount
59
71
}
60
72
61
- if grid [ny ][nx ] == obstacleBlock {
73
+ if grid [ny ][nx ] == obstacleBlock || ( nx == obstacle . X && ny == obstacle . Y ) {
62
74
dx , dy = (dx + 1 )% 4 , (dy + 1 )% 4
63
75
continue
64
76
} else if grid [ny ][nx ] == 0 {
65
77
stepCount ++
66
- } else if grid [ny ][nx ] > 4 {
78
+ } else if grid [ny ][nx ] == 5 {
67
79
return - 1 // Infinite loop detected!
68
80
}
69
81
You can’t perform that action at this time.
0 commit comments