package day_15 import ( "strings" ) const ( Wall = '#' Player = '@' Box = 'O' Empty = '.' ) const ( InstUp = '^' InstDown = 'v' InstLeft = '<' InstRight = '>' ) type Warehouse [][]rune func (w Warehouse) At(x, y int) rune { if x < 0 || y < 0 || x >= len(w[0]) || y >= len(w) { return Wall } return w[y][x] } // task:https://adventofcode.com/2024/day/13 func SolveBasic(input string) int { warehouse, instructions, rX, rY := load(input) for _, instr := range instructions { switch instr { case InstUp: rX, rY = moveUp(warehouse, rX, rY) case InstDown: rX, rY = moveDown(warehouse, rX, rY) case InstLeft: rX, rY = moveLeft(warehouse, rX, rY) case InstRight: rX, rY = moveRight(warehouse, rX, rY) default: panic("unknown instruction " + string(instr)) } } sum := 0 for y, line := range warehouse { for x, val := range line { if val == Box { sum += y*100 + x } } } return sum } // task:https://adventofcode.com/2024/day/12#part2 func SolveComplex(input string) int { return 0 } func load(input string) (Warehouse, []rune, int, int) { substrings := strings.Split(input, "\n\n") warehouse := Warehouse{} rX := 0 rY := 0 for y, line := range strings.Split(substrings[0], "\n") { if strings.ContainsRune(line, Player) { rY = y rX = strings.IndexRune(line, Player) } warehouse = append(warehouse, []rune(line)) } instructions := []rune(strings.ReplaceAll(substrings[1], "\n", "")) return warehouse, instructions, rX, rY } func moveUp(warehouse Warehouse, x int, y int) (int, int) { if warehouse.At(x, y-1) == Wall { return x, y } else if warehouse.At(x, y-1) == Empty { warehouse[y][x] = Empty warehouse[y-1][x] = Player return x, y - 1 } else { newY := y - 1 for ; warehouse.At(x, newY) == Box; newY-- { } if warehouse.At(x, newY) == Empty { warehouse[newY][x] = Box warehouse[y-1][x] = Player warehouse[y][x] = Empty return x, y - 1 } } return x, y } func moveDown(warehouse Warehouse, x int, y int) (int, int) { if warehouse.At(x, y+1) == Wall { return x, y } else if warehouse.At(x, y+1) == Empty { warehouse[y][x] = Empty warehouse[y+1][x] = Player return x, y + 1 } else { newY := y + 1 for ; warehouse.At(x, newY) == Box; newY++ { } if warehouse.At(x, newY) == Empty { warehouse[newY][x] = Box warehouse[y+1][x] = Player warehouse[y][x] = Empty return x, y + 1 } } return x, y } func moveLeft(warehouse Warehouse, x int, y int) (int, int) { if warehouse.At(x-1, y) == Wall { return x, y } else if warehouse.At(x-1, y) == Empty { warehouse[y][x] = Empty warehouse[y][x-1] = Player return x - 1, y } else { newX := x - 1 for ; warehouse.At(newX, y) == Box; newX-- { } if warehouse.At(newX, y) == Empty { warehouse[y][newX] = Box warehouse[y][x-1] = Player warehouse[y][x] = Empty return x - 1, y } } return x, y } func moveRight(warehouse Warehouse, x int, y int) (int, int) { if warehouse.At(x+1, y) == Wall { return x, y } else if warehouse.At(x+1, y) == Empty { warehouse[y][x] = Empty warehouse[y][x+1] = Player return x + 1, y } else { newX := x + 1 for ; warehouse.At(newX, y) == Box; newX++ { } if warehouse.At(newX, y) == Empty { warehouse[y][newX] = Box warehouse[y][x+1] = Player warehouse[y][x] = Empty return x + 1, y } } return x, y }