|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
| 5 | + "fmt" |
4 | 6 | "github.com/terminalnode/adventofcode2024/common"
|
| 7 | + "github.com/terminalnode/adventofcode2024/common/util" |
| 8 | + "strings" |
5 | 9 | )
|
6 | 10 |
|
| 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 | + |
7 | 31 | 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) |
9 | 130 | }
|
0 commit comments