@@ -14,6 +14,9 @@ type Coordinate = util.Coordinate
14
14
type BoolMatrix = util.BoolMatrix
15
15
type direction = int
16
16
17
+ // VisitedDirectionMap is meant to go from x => y => direction
18
+ type tVisitedMap map [int ]map [int ]map [int ]bool
19
+
17
20
var directions = []util.Direction {
18
21
Coordinate .North ,
19
22
Coordinate .East ,
@@ -25,7 +28,7 @@ type parsedInput struct {
25
28
Guard Coordinate
26
29
Direction direction
27
30
ObstacleMap BoolMatrix
28
- VisitedMap BoolMatrix
31
+ VisitedMap tVisitedMap
29
32
}
30
33
31
34
func main () {
@@ -43,7 +46,12 @@ func part1(
43
46
for parsed .move () {
44
47
// Do nothing, move() does all the work
45
48
}
46
- count := util .CountInMatrix (parsed .VisitedMap , true )
49
+
50
+ var count int
51
+ for _ , xMap := range parsed .VisitedMap {
52
+ count += len (xMap )
53
+ }
54
+
47
55
return fmt .Sprintf ("Visited spots in the Matrix: %d" , count )
48
56
}
49
57
@@ -52,28 +60,25 @@ func parseInput(
52
60
) (parsedInput , error ) {
53
61
var guard Coordinate
54
62
var obstacles BoolMatrix
55
- var visited BoolMatrix
56
63
var err error
57
64
58
65
lines := strings .Split (input , "\n " )
59
66
maxY := len (lines ) - 1
60
67
maxX := len (lines [0 ]) - 1
61
68
62
69
rawObstacles := make ([][]bool , maxY + 1 )
63
- rawVisited := make ([][] bool , maxY + 1 )
70
+ visitedMap := make (tVisitedMap )
64
71
65
72
foundGuard := false
66
73
for y := 0 ; y <= maxY ; y ++ {
67
74
rawObstacles [y ] = make ([]bool , maxX + 1 )
68
- rawVisited [y ] = make ([]bool , maxX + 1 )
69
75
70
76
for x := 0 ; x <= maxX ; x ++ {
71
77
c := lines [y ][x ]
72
78
rawObstacles [y ][x ] = c == '#'
73
79
if c == '^' {
74
80
foundGuard = true
75
81
guard = Coordinate {X : x , Y : y }
76
- rawVisited [y ][x ] = true
77
82
}
78
83
}
79
84
}
@@ -86,17 +91,15 @@ func parseInput(
86
91
return parsedInput {}, fmt .Errorf ("failed to create obstacle matrix: %v" , err )
87
92
}
88
93
89
- visited , err = util .NewMatrixFromRows (rawVisited )
90
- if err != nil {
91
- return parsedInput {}, fmt .Errorf ("failed to create visited matrix: %v" , err )
92
- }
93
-
94
- return parsedInput {
94
+ out := parsedInput {
95
95
Guard : guard ,
96
96
Direction : 0 , // This means north
97
97
ObstacleMap : obstacles ,
98
- VisitedMap : visited ,
99
- }, nil
98
+ VisitedMap : visitedMap ,
99
+ }
100
+ out .markVisited (guard )
101
+
102
+ return out , nil
100
103
}
101
104
102
105
// Move and return a boolean indicating whether the guard is inside the matrix or not
@@ -112,9 +115,24 @@ func (
112
115
newPos = getNewPosition (p .Guard , p .Direction )
113
116
}
114
117
p .Guard = newPos
118
+ if ! p .ObstacleMap .IsInMatrix (p .Guard .X , p .Guard .Y ) {
119
+ return false
120
+ }
115
121
116
- err = p .VisitedMap .Set (p .Guard .X , p .Guard .Y , true )
117
- return err == nil
122
+ p .markVisited (newPos )
123
+ return true
124
+ }
125
+
126
+ func (p * parsedInput ) markVisited (
127
+ c Coordinate ,
128
+ ) {
129
+ if p .VisitedMap [c .X ] == nil {
130
+ p .VisitedMap [c .X ] = make (map [int ]map [int ]bool )
131
+ }
132
+ if p.VisitedMap [c.X ][c.Y ] == nil {
133
+ p.VisitedMap [c.X ][c.Y ] = make (map [int ]bool )
134
+ }
135
+ p.VisitedMap [c.X ][c.Y ][p .Direction ] = true
118
136
}
119
137
120
138
func (p * parsedInput ) rotate () {
0 commit comments