Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit eaaa49e

Browse files
committed
Refactor day 6 part 1 with tVisitedMap
1 parent a7fb307 commit eaaa49e

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

solutions/day06/main.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ type Coordinate = util.Coordinate
1414
type BoolMatrix = util.BoolMatrix
1515
type direction = int
1616

17+
// VisitedDirectionMap is meant to go from x => y => direction
18+
type tVisitedMap map[int]map[int]map[int]bool
19+
1720
var directions = []util.Direction{
1821
Coordinate.North,
1922
Coordinate.East,
@@ -25,7 +28,7 @@ type parsedInput struct {
2528
Guard Coordinate
2629
Direction direction
2730
ObstacleMap BoolMatrix
28-
VisitedMap BoolMatrix
31+
VisitedMap tVisitedMap
2932
}
3033

3134
func main() {
@@ -43,7 +46,12 @@ func part1(
4346
for parsed.move() {
4447
// Do nothing, move() does all the work
4548
}
46-
count := util.CountInMatrix(parsed.VisitedMap, true)
49+
50+
var count int
51+
for _, xMap := range parsed.VisitedMap {
52+
count += len(xMap)
53+
}
54+
4755
return fmt.Sprintf("Visited spots in the Matrix: %d", count)
4856
}
4957

@@ -52,28 +60,25 @@ func parseInput(
5260
) (parsedInput, error) {
5361
var guard Coordinate
5462
var obstacles BoolMatrix
55-
var visited BoolMatrix
5663
var err error
5764

5865
lines := strings.Split(input, "\n")
5966
maxY := len(lines) - 1
6067
maxX := len(lines[0]) - 1
6168

6269
rawObstacles := make([][]bool, maxY+1)
63-
rawVisited := make([][]bool, maxY+1)
70+
visitedMap := make(tVisitedMap)
6471

6572
foundGuard := false
6673
for y := 0; y <= maxY; y++ {
6774
rawObstacles[y] = make([]bool, maxX+1)
68-
rawVisited[y] = make([]bool, maxX+1)
6975

7076
for x := 0; x <= maxX; x++ {
7177
c := lines[y][x]
7278
rawObstacles[y][x] = c == '#'
7379
if c == '^' {
7480
foundGuard = true
7581
guard = Coordinate{X: x, Y: y}
76-
rawVisited[y][x] = true
7782
}
7883
}
7984
}
@@ -86,17 +91,15 @@ func parseInput(
8691
return parsedInput{}, fmt.Errorf("failed to create obstacle matrix: %v", err)
8792
}
8893

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{
9595
Guard: guard,
9696
Direction: 0, // This means north
9797
ObstacleMap: obstacles,
98-
VisitedMap: visited,
99-
}, nil
98+
VisitedMap: visitedMap,
99+
}
100+
out.markVisited(guard)
101+
102+
return out, nil
100103
}
101104

102105
// Move and return a boolean indicating whether the guard is inside the matrix or not
@@ -112,9 +115,24 @@ func (
112115
newPos = getNewPosition(p.Guard, p.Direction)
113116
}
114117
p.Guard = newPos
118+
if !p.ObstacleMap.IsInMatrix(p.Guard.X, p.Guard.Y) {
119+
return false
120+
}
115121

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
118136
}
119137

120138
func (p *parsedInput) rotate() {

0 commit comments

Comments
 (0)