|
@@ -58,6 +58,58 @@ func readInput(file *os.File) (*Point, [][]byte, []byte) {
|
|
return robot, matrix, moves
|
|
return robot, matrix, moves
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func moveBox(x, y int, matrix [][]byte, xPlus, yPlus int) bool {
|
|
|
|
+ field := matrix[y][x]
|
|
|
|
+ if field == '#' {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if field == 'O' {
|
|
|
|
+ if !moveBox(x+xPlus, y+yPlus, matrix, xPlus, yPlus) {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ matrix[y][x] = 'O'
|
|
|
|
+ return true
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func moveRobot(robot *Point, matrix [][]byte, x, y int) {
|
|
|
|
+ matrix[robot.y][robot.x] = '.'
|
|
|
|
+ field := matrix[robot.y+y][robot.x+x]
|
|
|
|
+ if field == '#' {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if field == 'O' {
|
|
|
|
+ if !moveBox(robot.x+x, robot.y+y, matrix, x, y) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ robot.x += x
|
|
|
|
+ robot.y += y
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func processMoves(robot *Point, matrix [][]byte, moves []byte) {
|
|
|
|
+ for _, move := range moves {
|
|
|
|
+ switch move {
|
|
|
|
+ case '^':
|
|
|
|
+ moveRobot(robot, matrix, 0, -1)
|
|
|
|
+ fmt.Println(robot, matrix)
|
|
|
|
+ case 'v':
|
|
|
|
+ moveRobot(robot, matrix, 0, 1)
|
|
|
|
+ fmt.Println(robot, matrix)
|
|
|
|
+ case '<':
|
|
|
|
+ moveRobot(robot, matrix, -1, 0)
|
|
|
|
+ fmt.Println(robot, matrix)
|
|
|
|
+ case '>':
|
|
|
|
+ moveRobot(robot, matrix, 1, 0)
|
|
|
|
+ fmt.Println(robot, matrix)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func main() {
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
if len(os.Args) < 2 {
|
|
log.Fatal("You need to specify a file!")
|
|
log.Fatal("You need to specify a file!")
|
|
@@ -70,5 +122,5 @@ func main() {
|
|
}
|
|
}
|
|
|
|
|
|
robot, matrix, moves := readInput(file)
|
|
robot, matrix, moves := readInput(file)
|
|
- fmt.Println(robot, matrix, moves)
|
|
|
|
|
|
+ processMoves(robot, matrix, moves)
|
|
}
|
|
}
|