And 15 done. Gaming

This commit is contained in:
Melody Becker 2024-12-16 21:22:05 +01:00
parent 8d914de120
commit 5abbcae112
No known key found for this signature in database
2 changed files with 107 additions and 23 deletions

View file

@ -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())
}

View file

@ -1,9 +1,9 @@
#######
#...#.#
#.....#
#..OO@#
#..#..#
#..O..#
#.@O..#
#...O.#
#.....#
#######
<vv<<^^<<^^
>>vv>>>^