adventofcode/2021/day23/main.go

78 lines
1.9 KiB
Go

package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
var costs map[byte]int
var homes map[byte][]h.Coordinate
var burrow h.CoordByteMap
func main() {
costs = map[byte]int{'A': 1, 'B': 10, 'C': 100, 'D': 1000}
homes = map[byte][]h.Coordinate{
'A': []h.Coordinate{h.Coordinate{X: 3, Y: 3}, h.Coordinate{X: 3, Y: 2}},
'B': []h.Coordinate{h.Coordinate{X: 5, Y: 3}, h.Coordinate{X: 5, Y: 2}},
'C': []h.Coordinate{h.Coordinate{X: 7, Y: 3}, h.Coordinate{X: 7, Y: 2}},
'D': []h.Coordinate{h.Coordinate{X: 9, Y: 3}, h.Coordinate{X: 9, Y: 2}},
}
inp := h.StdinToStringSlice()
burrow = h.StringSliceToCoordByteMap(inp)
fmt.Println(burrow)
pods := findAllPods()
for k, v := range pods {
fmt.Println(k, string(v))
}
}
func findAllPods() map[h.Coordinate]byte {
return burrow.FindAllNot(' ', '#', '.')
}
func getHome(b byte) h.Coordinate {
// Returns the home spot for this pod...
if burrow.Get(homes[b][0]) == b {
return homes[b][1]
}
return homes[b][0]
}
func isPod(b byte) bool {
return b == 'A' || b == 'B' || b == 'C' || b == 'd'
}
// pathClear checks:
// 1: is the given coordinate a pod?
// 2: is the path to the pod's home clear?
func pathClear(c h.Coordinate) bool {
v := burrow.Get(c)
if !isPod(v) {
return false
}
return canMoveFromTo(c, getHome(v))
}
func canMoveFromTo(from, to h.Coordinate) bool {
if from.Equals(to) { // Not actually moving...
return true
}
if burrow.Get(to) != '.' {
return false
}
if from.X == to.X {
// Just moving up or down
if from.Y > to.Y {
return canMoveFromTo(h.Coordinate{X: from.X, Y: from.Y - 1}, to)
}
return canMoveFromTo(h.Coordinate{X: from.X, Y: from.Y + 1}, to)
} else if from.Y == to.Y {
// Just moving left or right
if from.X > to.X {
return canMoveFromTo(h.Coordinate{X: from.X - 1, Y: from.Y}, to)
}
return canMoveFromTo(h.Coordinate{X: from.X + 1, Y: from.Y}, to)
}
}