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
6 changed files
with
265 additions
and
2 deletions.
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,80 @@ | ||
package day17p1 | ||
|
||
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 | ||
Steps int | ||
} | ||
|
||
func (m *Maze) GetInitial() State { | ||
return State{utils.Point{X: 0, Y: 0}, utils.East, 0} | ||
} | ||
|
||
func (m *Maze) GetEdges(s State) []utils.Edge[State] { | ||
ret := []utils.Edge[State]{} | ||
|
||
// forward | ||
np := s.Position.Add(s.Direction) | ||
if hl, ok := m.HeatLoss[s.Position.Add(s.Direction)]; ok && s.Steps < 3 { | ||
newState := State{np, s.Direction, s.Steps + 1} | ||
ret = append(ret, utils.Edge[State]{Node: newState, Distance: hl}) | ||
} | ||
|
||
// Left and Right | ||
for _, dir := range []utils.Point{s.Direction.Right(), s.Direction.Left()} { | ||
np := s.Position.Add(dir) | ||
if hl, ok := m.HeatLoss[np]; ok { | ||
newState := State{np, dir, 1} | ||
ret = append(ret, utils.Edge[State]{Node: newState, Distance: hl}) | ||
} | ||
} | ||
|
||
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,45 @@ | ||
package day17p1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `2413432311323 | ||
3215453535623 | ||
3255245654254 | ||
3446585845452 | ||
4546657867536 | ||
1438598798454 | ||
4457876987766 | ||
3637877979653 | ||
4654967986887 | ||
4564679986453 | ||
1224686865563 | ||
2546548887735 | ||
4322674655533` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer uint64 | ||
}{ | ||
{testInput, 102}, | ||
} | ||
|
||
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) | ||
} | ||
} | ||
} |
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,80 @@ | ||
package day17p2 | ||
|
||
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 | ||
Steps int | ||
} | ||
|
||
func (m *Maze) GetInitial() State { | ||
return State{utils.Point{X: 0, Y: 0}, utils.East, 0} | ||
} | ||
|
||
func (m *Maze) GetEdges(s State) []utils.Edge[State] { | ||
ret := []utils.Edge[State]{} | ||
|
||
// forward | ||
np := s.Position.Add(s.Direction) | ||
if hl, ok := m.HeatLoss[s.Position.Add(s.Direction)]; ok && s.Steps < 10 { | ||
newState := State{np, s.Direction, s.Steps + 1} | ||
ret = append(ret, utils.Edge[State]{Node: newState, Distance: hl}) | ||
} | ||
|
||
// Left and Right | ||
for _, dir := range []utils.Point{s.Direction.Right(), s.Direction.Left()} { | ||
np := s.Position.Add(dir) | ||
if hl, ok := m.HeatLoss[np]; ok && s.Steps >= 4 { | ||
newState := State{np, dir, 1} | ||
ret = append(ret, utils.Edge[State]{Node: newState, Distance: hl}) | ||
} | ||
} | ||
|
||
return ret | ||
} | ||
|
||
func (m *Maze) IsFinal(s State) bool { | ||
if s.Position == m.BottomRight && s.Steps >= 4 { | ||
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 day17p2 | ||
|
||
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) | ||
} | ||
} | ||
} |
Submodule inputs
updated
from 9d230b to e88f25