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
3 changed files
with
156 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
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,96 @@ | ||
package day17p2b | ||
|
||
import ( | ||
"io" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
m := NewMaze(lines) | ||
|
||
dij := utils.NewDijkstra[State]() | ||
|
||
finalState, err := dij.Run(m) | ||
utils.Check(err, "error in search") | ||
|
||
return dij.Distance[finalState] | ||
} | ||
|
||
type Maze struct { | ||
HeatLoss map[utils.Point]uint64 | ||
BottomRight utils.Point | ||
} | ||
|
||
func NewMaze(lines []string) *Maze { | ||
m := Maze{HeatLoss: make(map[utils.Point]uint64)} | ||
|
||
for j, ln := range lines { | ||
for i, r := range ln { | ||
p := utils.Point{X: i, Y: -j} | ||
v := uint64(r - '0') | ||
m.HeatLoss[p] = v | ||
m.BottomRight = p | ||
} | ||
} | ||
|
||
return &m | ||
} | ||
|
||
type State struct { | ||
Position utils.Point | ||
Direction utils.Point | ||
} | ||
|
||
var zero = utils.Point{X: 0, Y: 0} | ||
|
||
func (m *Maze) GetInitial() State { | ||
// special starting direction from which we will go East to South | ||
return State{zero, zero} | ||
} | ||
|
||
func (m *Maze) GetEdges(s State) []utils.Edge[State] { | ||
ret := []utils.Edge[State]{} | ||
|
||
nextDirections := make([]utils.Point, 2) | ||
|
||
switch s.Direction { | ||
case zero: | ||
// This is the special starting state | ||
nextDirections[0] = utils.East | ||
nextDirections[1] = utils.South | ||
case utils.East, utils.West: | ||
nextDirections[0] = utils.South | ||
nextDirections[1] = utils.North | ||
case utils.North, utils.South: | ||
nextDirections[0] = utils.East | ||
nextDirections[1] = utils.West | ||
} | ||
|
||
// Left and Right | ||
for _, dir := range nextDirections { | ||
p := s.Position | ||
var length uint64 | ||
for step := 1; step <= 10; step++ { | ||
p = p.Add(dir) | ||
if delta, ok := m.HeatLoss[p]; ok { | ||
length += delta | ||
if step >= 4 { | ||
ret = append(ret, utils.Edge[State]{Node: State{p, dir}, Distance: length}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func (m *Maze) IsFinal(s State) bool { | ||
if s.Position == m.BottomRight { | ||
return true | ||
} | ||
|
||
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,52 @@ | ||
package day17p2b | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `2413432311323 | ||
3215453535623 | ||
3255245654254 | ||
3446585845452 | ||
4546657867536 | ||
1438598798454 | ||
4457876987766 | ||
3637877979653 | ||
4654967986887 | ||
4564679986453 | ||
1224686865563 | ||
2546548887735 | ||
4322674655533` | ||
|
||
var testInput2 = `111111111111 | ||
999999999991 | ||
999999999991 | ||
999999999991 | ||
999999999991` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer uint64 | ||
}{ | ||
{testInput, 94}, | ||
{testInput2, 71}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(uint64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |