Skip to content

Commit

Permalink
finished day 21 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
devries committed Dec 21, 2023
1 parent 8b5081b commit babd255
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
run.go
aoc_run
day20p2/relations.gv
day20p2/relations.pdf
72 changes: 72 additions & 0 deletions day21p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package day21p1

import (
"io"

"aoc/utils"
)

func Solve(r io.Reader) any {
return solveSteps(r, 64)
}

func solveSteps(r io.Reader, steps int) int {
lines := utils.ReadLines(r)

garden := Maze{make(map[utils.Point]rune), utils.NewBFS[utils.Point](), steps}

for j, ln := range lines {
for i, r := range ln {
p := utils.Point{X: i, Y: -j}
garden.positions[p] = r
}
}

_, err := garden.bfs.Run(garden)
if err != utils.BFSNotFound {
panic("this should end when points are exhausted")
}

count := 0
for _, d := range garden.bfs.Distance {
if d%2 == 0 {
count++
}
}

return count
}

type Maze struct {
positions map[utils.Point]rune
bfs *utils.BreadthFirstSearch[utils.Point]
steps int
}

func (m Maze) GetInitial() utils.Point {
for k, v := range m.positions {
if v == 'S' {
return k
}
}
panic("unable to find starting point")
}

func (m Maze) GetNeighbors(pos utils.Point) []utils.Point {
ret := []utils.Point{}

if m.bfs.Distance[pos] < uint64(m.steps) {
for _, dir := range utils.Directions {
np := pos.Add(dir)
if m.positions[np] == '.' {
ret = append(ret, np)
}
}
}

return ret
}

func (m Maze) IsFinal(pos utils.Point) bool {
return false
}
43 changes: 43 additions & 0 deletions day21p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package day21p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 16},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := solveSteps(r, 6)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
2 changes: 1 addition & 1 deletion inputs

0 comments on commit babd255

Please sign in to comment.