generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
run.go | ||
aoc_run | ||
day20p2/relations.gv | ||
day20p2/relations.pdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
Submodule inputs
updated
from 55ae37 to 94f935