142 lines
3.3 KiB
Go
142 lines
3.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"AOC2022/helper"
|
||
|
"fmt"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
//args := os.Args[1:]
|
||
|
lines := helper.ReadTextFile("day22/input")
|
||
|
field := [][]rune{}
|
||
|
for _, line := range lines {
|
||
|
if line == "" {
|
||
|
break
|
||
|
}
|
||
|
field = append(field, []rune(line))
|
||
|
}
|
||
|
commandsLine := lines[len(lines)-1]
|
||
|
commandsLine = strings.ReplaceAll(commandsLine, "R", "R,")
|
||
|
commandsLine = strings.ReplaceAll(commandsLine, "L", "L,")
|
||
|
commands := strings.Split(commandsLine, ",")
|
||
|
currentPosition := [3]int{0, 0, 0}
|
||
|
for i, c := range field[0] {
|
||
|
if c == '.' {
|
||
|
currentPosition[1] = i
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
fmt.Println(currentPosition)
|
||
|
|
||
|
for _, command := range commands {
|
||
|
fmt.Println(command)
|
||
|
currentPosition = runCommand(field, command, currentPosition)
|
||
|
fmt.Println(currentPosition)
|
||
|
}
|
||
|
fmt.Println(currentPosition)
|
||
|
fmt.Println(1000*(currentPosition[0]+1) + 4*(currentPosition[1]+1) + currentPosition[2])
|
||
|
|
||
|
}
|
||
|
|
||
|
func runCommand(field [][]rune, command string, currentPosition [3]int) [3]int {
|
||
|
rotation := command[len(command)-1]
|
||
|
if rotation < 57 {
|
||
|
rotation = 0
|
||
|
} else {
|
||
|
command = command[:len(command)-1]
|
||
|
}
|
||
|
distance := helper.RemoveError(strconv.Atoi(command))
|
||
|
currentPosition = move(field, currentPosition, distance)
|
||
|
if rotation == 'R' {
|
||
|
newDirection := currentPosition[2] + 1
|
||
|
if newDirection > 3 {
|
||
|
newDirection = 0
|
||
|
}
|
||
|
currentPosition[2] = newDirection
|
||
|
}
|
||
|
if rotation == 'L' {
|
||
|
newDirection := currentPosition[2] - 1
|
||
|
if newDirection < 0 {
|
||
|
newDirection = 3
|
||
|
}
|
||
|
currentPosition[2] = newDirection
|
||
|
}
|
||
|
|
||
|
return currentPosition
|
||
|
}
|
||
|
|
||
|
func getDirection(currentPosition [3]int) [2]int {
|
||
|
directions := [][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
|
||
|
return directions[currentPosition[2]]
|
||
|
}
|
||
|
|
||
|
func move(field [][]rune, currentPosition [3]int, distance int) [3]int {
|
||
|
direction := getDirection(currentPosition)
|
||
|
for i := 0; i < distance; i++ {
|
||
|
tmpPosition := [2]int{currentPosition[0], currentPosition[1]}
|
||
|
tmpPosition[0] += direction[0]
|
||
|
tmpPosition[1] += direction[1]
|
||
|
check := checkPosition(field, tmpPosition)
|
||
|
if check == 2 {
|
||
|
break
|
||
|
}
|
||
|
if check == 1 {
|
||
|
currentPosition = loop(field, currentPosition)
|
||
|
}
|
||
|
if check == 0 {
|
||
|
currentPosition[0] = tmpPosition[0]
|
||
|
currentPosition[1] = tmpPosition[1]
|
||
|
}
|
||
|
}
|
||
|
return currentPosition
|
||
|
}
|
||
|
|
||
|
//2 - Collision
|
||
|
//1 - Out Of Bounds
|
||
|
//0 - Fine
|
||
|
func checkPosition(field [][]rune, position [2]int) int {
|
||
|
if position[0] >= len(field) || position[0] < 0 {
|
||
|
return 1
|
||
|
}
|
||
|
if position[1] >= len(field[position[0]]) || position[1] < 0 {
|
||
|
return 1
|
||
|
}
|
||
|
switch field[position[0]][position[1]] {
|
||
|
case ' ':
|
||
|
return 1
|
||
|
case '.':
|
||
|
return 0
|
||
|
case '#':
|
||
|
return 2
|
||
|
}
|
||
|
return -1
|
||
|
}
|
||
|
|
||
|
func loop(field [][]rune, currentPosition [3]int) [3]int {
|
||
|
direction := getDirection(currentPosition)
|
||
|
newStartPosition := [2]int{0, 0}
|
||
|
if direction[0] == 0 {
|
||
|
newStartPosition[0] = currentPosition[0]
|
||
|
}
|
||
|
if direction[0] < 0 {
|
||
|
newStartPosition[0] = len(field) - 1
|
||
|
}
|
||
|
if direction[1] == 0 {
|
||
|
newStartPosition[1] = currentPosition[1]
|
||
|
}
|
||
|
if direction[1] < 0 {
|
||
|
newStartPosition[1] = len(field[newStartPosition[0]]) - 1
|
||
|
}
|
||
|
for checkPosition(field, newStartPosition) == 1 {
|
||
|
newStartPosition[0] += direction[0]
|
||
|
newStartPosition[1] += direction[1]
|
||
|
}
|
||
|
if checkPosition(field, newStartPosition) == 0 {
|
||
|
currentPosition[0] = newStartPosition[0]
|
||
|
currentPosition[1] = newStartPosition[1]
|
||
|
}
|
||
|
return currentPosition
|
||
|
}
|