From 5abbcae11296c94caa658fc91cee0e0b8d725ffd Mon Sep 17 00:00:00 2001 From: mStar Date: Mon, 16 Dec 2024 21:22:05 +0100 Subject: [PATCH] And 15 done. Gaming --- day15/main.go | 122 ++++++++++++++++++++++++++++++++++++++++++-------- day15/sample3 | 8 ++-- 2 files changed, 107 insertions(+), 23 deletions(-) diff --git a/day15/main.go b/day15/main.go index e79d516..09161d2 100644 --- a/day15/main.go +++ b/day15/main.go @@ -75,10 +75,8 @@ func (area *Area) moveRight(from util.Vec2) bool { target := area.a[from.Y][from.X+1] switch target { case '#': - // fmt.Println("Wall") return false case '.': - // fmt.Println("moving right") area.a[from.Y][from.X+1] = area.a[from.Y][from.X] area.a[from.Y][from.X] = '.' return true @@ -87,31 +85,91 @@ func (area *Area) moveRight(from util.Vec2) bool { } } -func (area *Area) moveUp(from util.Vec2) bool { - target := area.a[from.Y-1][from.X] - switch target { +func (area *Area) canMoveUp(at util.Vec2) bool { + // fmt.Println("Checking at", at) + if area.a[at.Y][at.X] == '#' { + return false + } + switch area.a[at.Y-1][at.X] { case '#': return false case '.': + return true + case 'O': + return area.canMoveUp(at.Add(util.Vec2{X: 0, Y: -1})) + case '[': + return area.canMoveUp(at.Add(util.Vec2{X: 0, Y: -1})) && + area.canMoveUp(at.Add(util.Vec2{X: 1, Y: -1})) + case ']': + return area.canMoveUp(at.Add(util.Vec2{X: 0, Y: -1})) && + area.canMoveUp(at.Add(util.Vec2{X: -1, Y: -1})) + default: + return false + } +} + +func (area *Area) canMoveDown(at util.Vec2) bool { + if area.a[at.Y][at.X] == '#' { + return false + } + switch area.a[at.Y+1][at.X] { + case '#': + return false + case '.': + return true + case 'O': + return area.canMoveDown(at.Add(util.Vec2{X: 0, Y: 1})) + case '[': + return area.canMoveDown(at.Add(util.Vec2{X: 0, Y: 1})) && + area.canMoveDown(at.Add(util.Vec2{X: 1, Y: 1})) + case ']': + return area.canMoveDown(at.Add(util.Vec2{X: 0, Y: 1})) && + area.canMoveDown(at.Add(util.Vec2{X: -1, Y: 1})) + default: + return false + } +} + +func (area *Area) moveUp(from util.Vec2) bool { + // fmt.Printf("Moving up at %v (%s)\n", from, string(area.a[from.Y][from.X])) + target := area.a[from.Y-1][from.X] + switch target { + case '#': + // fmt.Println("Hit Wall") + return false + case '.': + // fmt.Printf("Empty, moving from %v to %v\n", from, from.Add(util.Vec2{X: 0, Y: -1})) area.a[from.Y-1][from.X] = area.a[from.Y][from.X] area.a[from.Y][from.X] = '.' return true case '[': + if !(area.canMoveUp(from.Add(util.Vec2{X: 0, Y: -1})) && + area.canMoveUp(from.Add(util.Vec2{X: 1, Y: -1}))) { + return false + } + // fmt.Println("Moving Box left first") return area.moveUp(from.Add(util.Vec2{X: 0, Y: -1})) && area.moveUp(from.Add(util.Vec2{X: 1, Y: -1})) && - area.moveUp(from) && - area.moveUp(from.Add(util.Vec2{X: 1, Y: 0})) + area.moveUp(from) case ']': - return area.moveUp(from.Add(util.Vec2{X: 0, Y: -1})) && + // fmt.Println("Box right") + return area.canMoveUp(from.Add(util.Vec2{X: 0, Y: -1})) && + area.canMoveUp(from.Add(util.Vec2{X: -1, Y: -1})) && + area.moveUp(from.Add(util.Vec2{X: 0, Y: -1})) && area.moveUp(from.Add(util.Vec2{X: -1, Y: -1})) && - area.moveUp(from) && - area.moveUp(from.Add(util.Vec2{X: -1, Y: 0})) + area.moveUp(from) + case '@': + // fmt.Println("Moving bot") + return area.moveUp(from.Add(util.Vec2{X: 0, Y: -1})) && area.moveUp(from) + case 'O': + return area.moveUp(from.Add(util.Vec2{X: 0, Y: -1})) && area.moveUp(from) default: return area.moveUp(from.Add(util.Vec2{X: 0, Y: -1})) && area.moveUp(from) } } func (area *Area) moveDown(from util.Vec2) bool { + // fmt.Printf("Moving down at %v\n", from) target := area.a[from.Y+1][from.X] switch target { case '#': @@ -121,15 +179,17 @@ func (area *Area) moveDown(from util.Vec2) bool { area.a[from.Y][from.X] = '.' return true case '[': - return area.moveUp(from.Add(util.Vec2{X: 0, Y: 1})) && - area.moveUp(from.Add(util.Vec2{X: 1, Y: 1})) && - area.moveUp(from) && - area.moveUp(from.Add(util.Vec2{X: 1, Y: 0})) + return area.canMoveDown(from.Add(util.Vec2{X: 0, Y: 1})) && + area.canMoveDown(from.Add(util.Vec2{X: 1, Y: 1})) && + area.moveDown(from.Add(util.Vec2{X: 0, Y: 1})) && + area.moveDown(from.Add(util.Vec2{X: 1, Y: 1})) && + area.moveDown(from) case ']': - return area.moveUp(from.Add(util.Vec2{X: 0, Y: 1})) && - area.moveUp(from.Add(util.Vec2{X: -1, Y: 1})) && - area.moveUp(from) && - area.moveUp(from.Add(util.Vec2{X: -1, Y: 0})) + return area.canMoveDown(from.Add(util.Vec2{X: 0, Y: 1})) && + area.canMoveDown(from.Add(util.Vec2{X: -1, Y: 1})) && + area.moveDown(from.Add(util.Vec2{X: 0, Y: 1})) && + area.moveDown(from.Add(util.Vec2{X: -1, Y: 1})) && + area.moveDown(from) default: return area.moveDown(from.Add(util.Vec2{X: 0, Y: 1})) && area.moveDown(from) } @@ -169,6 +229,24 @@ func Widen(in string) string { ) } +func (area *Area) confirmBoxes(step int) { + for iy, line := range area.a { + lastWasBoxLeft := false + for ix, char := range line { + if lastWasBoxLeft && char != ']' { + fmt.Println(area.visualise()) + panic(fmt.Sprintf("Broken box right missing at %d %d, step %d", ix, iy, step)) + } + switch char { + case '[': + lastWasBoxLeft = true + default: + lastWasBoxLeft = false + } + } + } +} + func calcPosVal(x, y int) int { return x + y*100 } @@ -186,9 +264,15 @@ func main() { wArea, _ := parseInput([]byte(Widen(string(rawInput)))) fmt.Println(wArea.visualise()) - for _, action := range inputs { + for i, action := range inputs { + fmt.Println(string(action)) wArea.applyAction(action) + // if i == 806 { + // fmt.Println(wArea.visualise()) + // } + wArea.confirmBoxes(i) fmt.Println(wArea.visualise()) } + fmt.Println(wArea.visualise()) fmt.Printf("Task 2: %d\n", wArea.calcPosScore()) } diff --git a/day15/sample3 b/day15/sample3 index 6ee6098..93db4bb 100644 --- a/day15/sample3 +++ b/day15/sample3 @@ -1,9 +1,9 @@ ####### -#...#.# -#.....# -#..OO@# +#..#..# #..O..# +#.@O..# +#...O.# #.....# ####### ->vv>>>^