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

Commit 279ff7a

Browse files
committed
Add solution for day 6 part 1
1 parent 309577b commit 279ff7a

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ automatically rebuilt and redeployed ever time the `common` module or their own
5050
| 03 | ⭐ ⭐ | 16 | |
5151
| 04 | ⭐ ⭐ | 17 | |
5252
| 05 | ⭐ ⭐ | 18 | |
53-
| 06 | | 19 | |
53+
| 06 | | 19 | |
5454
| 07 | | 20 | |
5555
| 08 | | 21 | |
5656
| 09 | | 22 | |

solutions/day06/main.go

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,130 @@
11
package main
22

33
import (
4+
"errors"
5+
"fmt"
46
"github.com/terminalnode/adventofcode2024/common"
7+
"github.com/terminalnode/adventofcode2024/common/util"
8+
"strings"
59
)
610

11+
// Due to function types not implementing == or !=, I need to store the direction as an int.
12+
13+
type Coordinate = util.Coordinate
14+
type BoolMatrix = util.BoolMatrix
15+
type direction = int
16+
17+
var directions = []util.Direction{
18+
Coordinate.North,
19+
Coordinate.East,
20+
Coordinate.South,
21+
Coordinate.West,
22+
}
23+
24+
type parsedInput struct {
25+
Guard Coordinate
26+
Direction direction
27+
ObstacleMap BoolMatrix
28+
VisitedMap BoolMatrix
29+
}
30+
731
func main() {
8-
common.Setup(6, nil, nil)
32+
common.Setup(6, part1, nil)
33+
}
34+
35+
func part1(
36+
input string,
37+
) string {
38+
parsed, err := parseInput(input)
39+
if err != nil {
40+
return fmt.Sprintf("Failed to parse input: %v", err)
41+
}
42+
43+
for parsed.move() {
44+
// Do nothing, move() does all the work
45+
}
46+
count := util.CountInMatrix(parsed.VisitedMap, true)
47+
return fmt.Sprintf("Visited spots in the Matrix: %d", count)
48+
}
49+
50+
func parseInput(
51+
input string,
52+
) (parsedInput, error) {
53+
var guard Coordinate
54+
var obstacles BoolMatrix
55+
var visited BoolMatrix
56+
var err error
57+
58+
lines := strings.Split(input, "\n")
59+
maxY := len(lines) - 1
60+
maxX := len(lines[0]) - 1
61+
62+
rawObstacles := make([][]bool, maxY+1)
63+
rawVisited := make([][]bool, maxY+1)
64+
65+
foundGuard := false
66+
for y := 0; y <= maxY; y++ {
67+
rawObstacles[y] = make([]bool, maxX+1)
68+
rawVisited[y] = make([]bool, maxX+1)
69+
70+
for x := 0; x <= maxX; x++ {
71+
c := lines[y][x]
72+
rawObstacles[y][x] = c == '#'
73+
if c == '^' {
74+
foundGuard = true
75+
guard = Coordinate{X: x, Y: y}
76+
rawVisited[y][x] = true
77+
}
78+
}
79+
}
80+
if foundGuard == false {
81+
return parsedInput{}, errors.New("could not find guard in matrix")
82+
}
83+
84+
obstacles, err = util.NewMatrixFromRows(rawObstacles)
85+
if err != nil {
86+
return parsedInput{}, fmt.Errorf("failed to create obstacle matrix: %v", err)
87+
}
88+
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{
95+
Guard: guard,
96+
Direction: 0, // This means north
97+
ObstacleMap: obstacles,
98+
VisitedMap: visited,
99+
}, nil
100+
}
101+
102+
// Move and return a boolean indicating whether the guard is inside the matrix or not
103+
func (
104+
p *parsedInput,
105+
) move() bool {
106+
newPos := getNewPosition(p.Guard, p.Direction)
107+
isBlocked, err := p.ObstacleMap.Get(newPos.X, newPos.Y)
108+
if err != nil {
109+
return false
110+
} else if isBlocked {
111+
p.rotate()
112+
newPos = getNewPosition(p.Guard, p.Direction)
113+
}
114+
p.Guard = newPos
115+
116+
err = p.VisitedMap.Set(p.Guard.X, p.Guard.Y, true)
117+
return err == nil
118+
}
119+
120+
func (p *parsedInput) rotate() {
121+
p.Direction = (p.Direction + 1) % 4
122+
}
123+
124+
func getNewPosition(
125+
guard Coordinate,
126+
direction int,
127+
) Coordinate {
128+
dirF := directions[direction]
129+
return dirF(guard)
9130
}

0 commit comments

Comments
 (0)