123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package main
- import (
- "bufio"
- "fmt"
- "log"
- "os"
- )
- type Point struct {
- y, x int
- }
- func findRobot(line string) *Point {
- for i, char := range line {
- if char == '@' {
- return &Point{x: i}
- }
- }
- return nil
- }
- func readInput(file *os.File) (*Point, [][]byte, []byte) {
- scanner := bufio.NewScanner(file)
- var matrix [][]byte
- var moves []byte
- var robot *Point
- var y int
- readingMatrix := true
- for scanner.Scan() {
- line := scanner.Text()
- if line == "" {
- if readingMatrix {
- readingMatrix = false
- continue
- }
- break
- }
- if readingMatrix {
- matrix = append(matrix, []byte(line))
- if robot == nil {
- robot = findRobot(line)
- if robot != nil {
- robot.y = y
- }
- y++
- }
- } else {
- moves = append(moves, []byte(line)...)
- }
- }
- 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)
- case 'v':
- moveRobot(robot, matrix, 0, 1)
- case '<':
- moveRobot(robot, matrix, -1, 0)
- case '>':
- moveRobot(robot, matrix, 1, 0)
- }
- }
- }
- func calculateBoxes(matrix [][]byte) int {
- var result int
- for y := range matrix {
- for x := range matrix[y] {
- if matrix[y][x] == 'O' {
- result += 100*y + x
- }
- }
- }
- return result
- }
- func part1(robot *Point, matrix [][]byte, moves []byte) int {
- processMoves(robot, matrix, moves)
- return calculateBoxes(matrix)
- }
- func main() {
- if len(os.Args) < 2 {
- log.Fatal("You need to specify a file!")
- }
- filePath := os.Args[1]
- file, err := os.Open(filePath)
- if err != nil {
- log.Fatalf("Failed to open %s!\n", filePath)
- }
- robot, matrix, moves := readInput(file)
- fmt.Println("Part1:", part1(robot, matrix, moves))
- }
|