add day-16 part1

This commit is contained in:
2024-12-17 01:58:33 +00:00
parent 090530ce72
commit 1429ece4f4
14 changed files with 394 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
package one
import (
hp "container/heap"
"errors"
"math"
"slices"
"strings"
log "github.com/sirupsen/logrus"
)
const turnCost int = 1000
type graph struct {
start node
end node
data []string
}
func newGraph() *graph {
return &graph{}
}
func (g *graph) String() string {
return strings.Join(g.data, "\n")
}
func (g *graph) valueAt(n node) rune {
return rune(g.data[n.y][n.x])
}
func (g *graph) dijkstra() (int, error) {
heap := newHeap()
hp.Push(heap, move{g.start, 0})
visited := make(map[node]struct{})
costs := make(map[node]int)
prev := make(map[node]node)
for heap.Len() > 0 {
current := hp.Pop(heap).(move)
if current.node.coords == g.end.coords {
log.Debugf("\n%s\n", g.debug(prev, costs))
return costs[g.end], nil
}
if _, ok := visited[current.node]; ok {
continue
}
visited[current.node] = struct{}{}
for _, n := range neighbours(current.node) {
if g.valueAt(n) == '#' {
continue
}
next_cost := current.cost + 1
if n.direction != current.node.direction {
next_cost += turnCost
}
_, ok := costs[n]
if !ok {
costs[n] = math.MaxInt
}
if next_cost < costs[n] {
costs[n] = next_cost
prev[n] = current.node
hp.Push(heap, move{n, next_cost})
}
}
}
return 0, errors.New("unable to get shortest path cost")
}
func (g *graph) debug(prev map[node]node, costs map[node]int) string {
path := []node{g.end}
node := prev[g.end]
for node != g.start {
path = append(path, prev[node])
node = prev[node]
}
temp := slices.Clone(g.data)
for _, node := range path {
if g.valueAt(node) == 'S' || g.valueAt(node) == 'E' {
continue
}
temp[node.y] = replaceAtIndex(temp[node.y], []rune{'^', '>', 'v', '<'}[node.direction], node.x)
}
log.Debugf("len of shortest path: %d", len(path))
log.Debugf("cost of shortest path: %d", costs[g.end])
return strings.Join(temp, "\n")
}

View File

@@ -0,0 +1,31 @@
package one
type minHeap []move
func newHeap() *minHeap {
return &minHeap{}
}
func (h minHeap) Len() int {
return len(h)
}
func (h minHeap) Less(i, j int) bool {
return h[i].cost < h[j].cost
}
func (h minHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
func (h *minHeap) Push(x interface{}) {
*h = append(*h, x.(move))
}
func (h *minHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}

View File

@@ -0,0 +1,6 @@
package one
type move struct {
node node
cost int
}

View File

@@ -0,0 +1,32 @@
package one
func neighbours(p node) [3]node {
switch p.direction {
case N:
return [3]node{
newNode(p.x, p.y-1, N),
newNode(p.x-1, p.y, W),
newNode(p.x+1, p.y, E),
}
case E:
return [3]node{
newNode(p.x+1, p.y, E),
newNode(p.x, p.y-1, N),
newNode(p.x, p.y+1, S),
}
case S:
return [3]node{
newNode(p.x, p.y+1, S),
newNode(p.x-1, p.y, W),
newNode(p.x+1, p.y, E),
}
case W:
return [3]node{
newNode(p.x-1, p.y, W),
newNode(p.x, p.y+1, S),
newNode(p.x, p.y-1, N),
}
default:
return [3]node{}
}
}

View File

@@ -0,0 +1,24 @@
package one
type direction int
const (
N direction = iota
E
S
W
)
type coords struct {
x int
y int
}
type node struct {
coords
direction direction
}
func newNode(x, y int, dir direction) node {
return node{coords{x, y}, dir}
}

View File

@@ -0,0 +1,20 @@
package one
import (
"bytes"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
graph, err := parseLines(r)
if err != nil {
return 0, err
}
lowestCost, err := graph.dijkstra()
if err != nil {
return 0, err
}
return lowestCost, nil
}

View File

@@ -0,0 +1,15 @@
package one
import (
_ "embed"
"os"
"testing"
)
//go:embed testdata/input.txt
var data []byte
func BenchmarkSolve(b *testing.B) {
os.Stdout, _ = os.Open(os.DevNull)
Solve(data)
}

View File

@@ -0,0 +1,52 @@
package one
import (
"bufio"
"io"
"strings"
)
func parseLines(r io.Reader) (*graph, error) {
graph := newGraph()
var linecount int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
r, indx := indxForAny(line, []rune{'S', 'E'})
if indx != -1 {
if r == 'S' {
graph.start = newNode(indx, linecount, E)
} else {
graph.end = newNode(indx, linecount, 0)
}
}
graph.data = append(graph.data, line)
linecount++
}
if err := scanner.Err(); err != nil {
return nil, err
}
return graph, nil
}
func indxForAny(s string, runes []rune) (rune, int) {
for _, r := range runes {
indx := strings.Index(s, string(r))
if indx != -1 {
return r, indx
}
}
return 0, -1
}
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}