This commit is contained in:
onyx-and-iris 2023-12-22 21:56:07 +00:00
parent 58f36581f0
commit 497da642b2
10 changed files with 334 additions and 0 deletions

3
day-17/go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/onyx-and-iris/aoc2023/day-17
go 1.21.5

10
day-17/makefile Normal file
View File

@ -0,0 +1,10 @@
TEST="test.txt"
INPUT="input.txt"
test:
go run . < $(TEST)
run:
go run . < $(INPUT)
all: test

32
day-17/node.go Normal file
View File

@ -0,0 +1,32 @@
package main
import "fmt"
type coords struct {
X int
Y int
}
func newCoords(x, y int) coords {
return coords{X: x, Y: y}
}
// node represents a single point on the graph
type node struct {
cost int
distance int
directionX int
directionY int
coords
index int
}
func newNode(cost, distance, directionX, directionY, x, y int) *node {
c := newCoords(x, y)
return &node{cost: cost, distance: distance, directionX: directionX, directionY: directionY, coords: c}
}
// String implements the fmt.Stringer interface
func (n node) String() string {
return fmt.Sprintf("%d%d%d%v", n.distance, n.directionX, n.directionY, n.coords)
}

114
day-17/one.go Normal file
View File

@ -0,0 +1,114 @@
package main
import (
"container/heap"
log "github.com/sirupsen/logrus"
)
type option func(*dijkstra)
func WithMinDistance(distance int) option {
return func(d *dijkstra) {
d.minDistance = distance
}
}
func WithMaxDistance(distance int) option {
return func(d *dijkstra) {
d.maxDistance = distance
}
}
type dijkstra struct {
graph [][]int
minDistance int
maxDistance int
}
func newDijkstra(graph [][]int, opts ...option) *dijkstra {
d := &dijkstra{graph: graph}
for _, opt := range opts {
opt(d)
}
return d
}
func (d dijkstra) initialize(start coords) *pqueue {
pq := newPriorityQueue()
heap.Init(pq)
// we don't encounter heat loss for start point unless we enter this block again
heap.Push(pq, newNode(0, 0, 0, 0, start.X, start.Y))
return pq
}
// run performs the lowest cost dijkstra algorithm with a min heap
func (d dijkstra) run(start, end coords) int {
pq := d.initialize(start)
visited := map[string]bool{}
for pq.Len() > 0 {
cost, node := func() (int, *node) {
x := heap.Pop(pq).(*node)
return x.cost, x
}()
// we reached final location, return its lowest cost
if node.X == end.X && node.Y == end.Y && node.distance >= d.minDistance {
log.Debug("returning lowest cost with min distance >= ", d.minDistance)
return node.cost
}
if _, ok := visited[node.String()]; ok {
continue
}
visited[node.String()] = true
var neighbours = [][]int{{0, -1}, {0, 1}, {-1, 0}, {1, 0}} // N, S, W, E
for _, n := range neighbours {
nextX := node.X + n[0]
nextY := node.Y + n[1]
if nextY < 0 || nextY >= len(d.graph) || nextX < 0 || nextX >= len(d.graph[nextY]) {
continue
}
if node.directionX == -n[0] && node.directionY == -n[1] { // are we going backwards?
continue
}
var distance = 1
if node.directionX == n[0] || node.directionY == n[1] { // same direction
distance = node.distance + 1
} else {
if node.distance < d.minDistance {
continue
}
}
if distance > d.maxDistance {
continue
}
new_cost := cost + d.graph[nextY][nextX]
heap.Push(pq, newNode(new_cost, distance, n[0], n[1], nextX, nextY))
}
}
return 0
}
// one returns the lowest cost path from start to end
func one(lines []string) int {
graph := buildGraph(lines)
start := newCoords(0, 0)
end := newCoords(len(graph[0])-1, len(graph)-1)
dijkstra := newDijkstra(graph, WithMaxDistance(3))
cost := dijkstra.run(start, end)
return cost
}

49
day-17/pqueue.go Normal file
View File

@ -0,0 +1,49 @@
package main
import (
"container/heap"
)
// pqueue implements the heap.Interface interface
// it represents a min heap priority queue
type pqueue []*node
func newPriorityQueue() *pqueue {
pq := make(pqueue, 0)
return &pq
}
func (pq pqueue) Len() int {
return len(pq)
}
func (pq *pqueue) Push(x interface{}) {
n := len(*pq)
node := x.(*node)
node.index = n
*pq = append(*pq, node)
}
func (pq *pqueue) Pop() interface{} {
old := *pq
n := len(old)
node := old[n-1]
node.index = -1
*pq = old[0 : n-1]
return node
}
func (pq *pqueue) Update(node *node, value int) {
node.cost = value
heap.Fix(pq, node.index)
}
func (pq pqueue) Less(i, j int) bool {
return pq[i].cost < pq[j].cost
}
func (pq pqueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}

27
day-17/pqueue_test.go Normal file
View File

@ -0,0 +1,27 @@
package main
import (
"container/heap"
"testing"
"github.com/go-playground/assert/v2"
)
func TestPriorityQueue(t *testing.T) {
//t.Skip("skipping test")
pq := newPriorityQueue()
heap.Push(pq, newNode(30, 0, 0, 0, 0, 0))
heap.Push(pq, newNode(10, 0, 0, 0, 8, 0))
heap.Push(pq, newNode(20, 0, 0, 0, 13, 0))
t.Run("Should create a queue size 3", func(t *testing.T) {
assert.Equal(t, 3, pq.Len())
})
item := heap.Pop(pq).(*node)
t.Run("Should return item with cost 10", func(t *testing.T) {
assert.Equal(t, 10, item.cost)
})
}

21
day-17/solution.go Normal file
View File

@ -0,0 +1,21 @@
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func init() {
log.SetLevel(log.InfoLevel)
}
func main() {
lines := readlines()
ans := one(lines)
fmt.Printf("solution one: %d\n", ans)
ans = two(lines)
fmt.Printf("solution two: %d\n", ans)
}

14
day-17/two.go Normal file
View File

@ -0,0 +1,14 @@
package main
// two returns the lowest cost path from start to end
// with a min/max distance set
func two(lines []string) int {
graph := buildGraph(lines)
start := newCoords(0, 0)
end := newCoords(len(graph[0])-1, len(graph)-1)
dijkstra := newDijkstra(graph, WithMinDistance(4), WithMaxDistance(10))
cost := dijkstra.run(start, end)
return cost
}

26
day-17/two_test.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
_ "embed"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
//go:embed test2.txt
testInput2 []byte
)
func TestDjistraWithMinDistance(t *testing.T) {
//t.Skip("skipping test")
input := strings.Split(string(testInput2), "\n")
cost := two(input)
t.Run("Should return a lowest cost of 71", func(t *testing.T) {
assert.Equal(t, 71, cost)
})
}

38
day-17/util.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"bufio"
"log"
"os"
)
// readlines reads lines from stdin.
// it returns them as an array of strings
func readlines() []string {
lines := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return lines
}
// buildGraph parses lines into costs for graph
func buildGraph(lines []string) [][]int {
graph := make([][]int, len(lines))
for i, line := range lines {
graph[i] = make([]int, len(line))
for j, r := range line {
graph[i][j] = int(r - '0')
}
}
return graph
}