Temporary Solution Day 22
This commit is contained in:
parent
86f941e5d7
commit
6847e3241a
257
day22/day22.go
257
day22/day22.go
@ -10,6 +10,68 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
//args := os.Args[1:]
|
//args := os.Args[1:]
|
||||||
lines := helper.ReadTextFile("day22/input")
|
lines := helper.ReadTextFile("day22/input")
|
||||||
|
commandsLine := lines[len(lines)-1]
|
||||||
|
commandsLine = strings.ReplaceAll(commandsLine, "R", "R,")
|
||||||
|
commandsLine = strings.ReplaceAll(commandsLine, "L", "L,")
|
||||||
|
commands := strings.Split(commandsLine, ",")
|
||||||
|
fmt.Println(commands)
|
||||||
|
part1(lines, commands)
|
||||||
|
part2(lines, commands)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(lines []string, commands []string) {
|
||||||
|
fields := [][][]rune{}
|
||||||
|
fields, cutFields := part2Parser(lines, fields, 50)
|
||||||
|
currentPosition := [4]int{0, 0, 0, 0}
|
||||||
|
for i, c := range cutFields[0][0] {
|
||||||
|
if c == '.' {
|
||||||
|
currentPosition[1] = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(currentPosition)
|
||||||
|
for i, command := range commands {
|
||||||
|
fmt.Println(command)
|
||||||
|
currentPosition = runCommand(cutFields, command, currentPosition, true)
|
||||||
|
fmt.Printf("%v: %v \n", i+1, currentPosition)
|
||||||
|
}
|
||||||
|
fmt.Println(currentPosition)
|
||||||
|
fmt.Println(1000*(currentPosition[0]+101) + 4*(currentPosition[1]+51) + currentPosition[2])
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2Parser(lines []string, fields [][][]rune, sidelength int) ([][][]rune, [][][]rune) {
|
||||||
|
for i := 0; i < 4*sidelength; i += sidelength {
|
||||||
|
field := [][]rune{}
|
||||||
|
for j := i; j < i+sidelength; j++ {
|
||||||
|
line := lines[j]
|
||||||
|
if line == "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fieldLine := strings.ReplaceAll(line, " ", "")
|
||||||
|
field = append(field, []rune(fieldLine))
|
||||||
|
}
|
||||||
|
fields = append(fields, field)
|
||||||
|
}
|
||||||
|
cutFields := [][][]rune{}
|
||||||
|
for i := 0; i < len(fields); i++ {
|
||||||
|
field := fields[i]
|
||||||
|
if len(field[0]) < 2*sidelength {
|
||||||
|
cutFields = append(cutFields, field)
|
||||||
|
} else {
|
||||||
|
tmpFields := [2][][]rune{}
|
||||||
|
for _, fieldLine := range field {
|
||||||
|
tmpFields[0] = append(tmpFields[0], fieldLine[:sidelength])
|
||||||
|
tmpFields[1] = append(tmpFields[1], fieldLine[sidelength:])
|
||||||
|
}
|
||||||
|
cutFields = append(cutFields, tmpFields[:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(len(cutFields))
|
||||||
|
return fields, cutFields
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(lines []string, commands []string) {
|
||||||
field := [][]rune{}
|
field := [][]rune{}
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
if line == "" {
|
if line == "" {
|
||||||
@ -17,30 +79,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
field = append(field, []rune(line))
|
field = append(field, []rune(line))
|
||||||
}
|
}
|
||||||
commandsLine := lines[len(lines)-1]
|
fields := [][][]rune{field}
|
||||||
commandsLine = strings.ReplaceAll(commandsLine, "R", "R,")
|
currentPosition := [4]int{0, 0, 0, 0}
|
||||||
commandsLine = strings.ReplaceAll(commandsLine, "L", "L,")
|
|
||||||
commands := strings.Split(commandsLine, ",")
|
|
||||||
currentPosition := [3]int{0, 0, 0}
|
|
||||||
for i, c := range field[0] {
|
for i, c := range field[0] {
|
||||||
if c == '.' {
|
if c == '.' {
|
||||||
currentPosition[1] = i
|
currentPosition[1] = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(currentPosition)
|
|
||||||
|
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
fmt.Println(command)
|
currentPosition = runCommand(fields, command, currentPosition, false)
|
||||||
currentPosition = runCommand(field, command, currentPosition)
|
|
||||||
fmt.Println(currentPosition)
|
|
||||||
}
|
}
|
||||||
fmt.Println(currentPosition)
|
|
||||||
fmt.Println(1000*(currentPosition[0]+1) + 4*(currentPosition[1]+1) + currentPosition[2])
|
fmt.Println(1000*(currentPosition[0]+1) + 4*(currentPosition[1]+1) + currentPosition[2])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCommand(field [][]rune, command string, currentPosition [3]int) [3]int {
|
func runCommand(field [][][]rune, command string, currentPosition [4]int, part2 bool) [4]int {
|
||||||
rotation := command[len(command)-1]
|
rotation := command[len(command)-1]
|
||||||
if rotation < 57 {
|
if rotation < 57 {
|
||||||
rotation = 0
|
rotation = 0
|
||||||
@ -48,7 +102,13 @@ func runCommand(field [][]rune, command string, currentPosition [3]int) [3]int {
|
|||||||
command = command[:len(command)-1]
|
command = command[:len(command)-1]
|
||||||
}
|
}
|
||||||
distance := helper.RemoveError(strconv.Atoi(command))
|
distance := helper.RemoveError(strconv.Atoi(command))
|
||||||
currentPosition = move(field, currentPosition, distance)
|
currentPosition = move(field, currentPosition, distance, part2)
|
||||||
|
currentPosition = rotate(rotation, currentPosition)
|
||||||
|
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
func rotate(rotation uint8, currentPosition [4]int) [4]int {
|
||||||
if rotation == 'R' {
|
if rotation == 'R' {
|
||||||
newDirection := currentPosition[2] + 1
|
newDirection := currentPosition[2] + 1
|
||||||
if newDirection > 3 {
|
if newDirection > 3 {
|
||||||
@ -63,18 +123,22 @@ func runCommand(field [][]rune, command string, currentPosition [3]int) [3]int {
|
|||||||
}
|
}
|
||||||
currentPosition[2] = newDirection
|
currentPosition[2] = newDirection
|
||||||
}
|
}
|
||||||
|
|
||||||
return currentPosition
|
return currentPosition
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDirection(currentPosition [3]int) [2]int {
|
func getDirection(currentPosition [4]int) [2]int {
|
||||||
directions := [][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
|
directions := [][2]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
|
||||||
return directions[currentPosition[2]]
|
return directions[currentPosition[2]]
|
||||||
}
|
}
|
||||||
|
|
||||||
func move(field [][]rune, currentPosition [3]int, distance int) [3]int {
|
func move(fields [][][]rune, currentPosition [4]int, distance int, part2 bool) [4]int {
|
||||||
|
field := fields[0]
|
||||||
direction := getDirection(currentPosition)
|
direction := getDirection(currentPosition)
|
||||||
for i := 0; i < distance; i++ {
|
for i := 0; i < distance; i++ {
|
||||||
|
if part2 {
|
||||||
|
field = fields[currentPosition[3]]
|
||||||
|
direction = getDirection(currentPosition)
|
||||||
|
}
|
||||||
tmpPosition := [2]int{currentPosition[0], currentPosition[1]}
|
tmpPosition := [2]int{currentPosition[0], currentPosition[1]}
|
||||||
tmpPosition[0] += direction[0]
|
tmpPosition[0] += direction[0]
|
||||||
tmpPosition[1] += direction[1]
|
tmpPosition[1] += direction[1]
|
||||||
@ -83,7 +147,11 @@ func move(field [][]rune, currentPosition [3]int, distance int) [3]int {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if check == 1 {
|
if check == 1 {
|
||||||
currentPosition = loop(field, currentPosition)
|
if !part2 {
|
||||||
|
currentPosition = loop(field, currentPosition)
|
||||||
|
} else {
|
||||||
|
currentPosition = loopPart2(fields, currentPosition)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if check == 0 {
|
if check == 0 {
|
||||||
currentPosition[0] = tmpPosition[0]
|
currentPosition[0] = tmpPosition[0]
|
||||||
@ -114,7 +182,7 @@ func checkPosition(field [][]rune, position [2]int) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func loop(field [][]rune, currentPosition [3]int) [3]int {
|
func loop(field [][]rune, currentPosition [4]int) [4]int {
|
||||||
direction := getDirection(currentPosition)
|
direction := getDirection(currentPosition)
|
||||||
newStartPosition := [2]int{0, 0}
|
newStartPosition := [2]int{0, 0}
|
||||||
if direction[0] == 0 {
|
if direction[0] == 0 {
|
||||||
@ -139,3 +207,156 @@ func loop(field [][]rune, currentPosition [3]int) [3]int {
|
|||||||
}
|
}
|
||||||
return currentPosition
|
return currentPosition
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loopPart2(fields [][][]rune, currentPosition [4]int) [4]int {
|
||||||
|
tmpPosition := getNewStartPos(currentPosition, len(fields[0][0]))
|
||||||
|
field := fields[tmpPosition[3]]
|
||||||
|
if checkPosition(field, [2]int{tmpPosition[0], tmpPosition[1]}) == 0 {
|
||||||
|
return tmpPosition
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNewStartPos(currentPosition [4]int, sidelength int) [4]int {
|
||||||
|
highestPos := sidelength - 1
|
||||||
|
if currentPosition[3] == 0 {
|
||||||
|
switch currentPosition[2] {
|
||||||
|
case 0:
|
||||||
|
currentPosition[3] = 1
|
||||||
|
currentPosition[1] = 0
|
||||||
|
case 1:
|
||||||
|
currentPosition[3] = 2
|
||||||
|
currentPosition[0] = 0
|
||||||
|
case 2:
|
||||||
|
currentPosition[3] = 3
|
||||||
|
currentPosition[1] = 0
|
||||||
|
currentPosition[0] = invertPosition(currentPosition[0], highestPos)
|
||||||
|
currentPosition[2] = invertDirection(currentPosition[2])
|
||||||
|
case 3:
|
||||||
|
currentPosition[3] = 5
|
||||||
|
currentPosition[0] = currentPosition[1]
|
||||||
|
currentPosition[1] = 0
|
||||||
|
currentPosition = rotate('R', currentPosition)
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
if currentPosition[3] == 1 {
|
||||||
|
switch currentPosition[2] {
|
||||||
|
case 0:
|
||||||
|
currentPosition[3] = 4
|
||||||
|
currentPosition[0] = invertPosition(currentPosition[0], highestPos)
|
||||||
|
currentPosition[1] = highestPos
|
||||||
|
currentPosition[2] = invertDirection(currentPosition[2])
|
||||||
|
case 1:
|
||||||
|
currentPosition[3] = 2
|
||||||
|
currentPosition[0] = currentPosition[1]
|
||||||
|
currentPosition[1] = highestPos
|
||||||
|
currentPosition = rotate('L', currentPosition)
|
||||||
|
case 2:
|
||||||
|
currentPosition[3] = 0
|
||||||
|
currentPosition[1] = highestPos
|
||||||
|
case 3:
|
||||||
|
currentPosition[3] = 5
|
||||||
|
currentPosition[0] = highestPos
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
|
||||||
|
}
|
||||||
|
if currentPosition[3] == 2 {
|
||||||
|
switch currentPosition[2] {
|
||||||
|
case 0:
|
||||||
|
currentPosition[3] = 1
|
||||||
|
currentPosition[1] = currentPosition[0]
|
||||||
|
currentPosition[0] = highestPos
|
||||||
|
currentPosition = rotate('L', currentPosition)
|
||||||
|
case 1:
|
||||||
|
currentPosition[3] = 4
|
||||||
|
currentPosition[0] = 0
|
||||||
|
case 2:
|
||||||
|
currentPosition[3] = 3
|
||||||
|
currentPosition[1] = currentPosition[0]
|
||||||
|
currentPosition[0] = 0
|
||||||
|
currentPosition = rotate('L', currentPosition)
|
||||||
|
case 3:
|
||||||
|
currentPosition[3] = 0
|
||||||
|
currentPosition[0] = highestPos
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
if currentPosition[3] == 3 {
|
||||||
|
switch currentPosition[2] {
|
||||||
|
case 0:
|
||||||
|
currentPosition[3] = 4
|
||||||
|
currentPosition[1] = 0
|
||||||
|
case 1:
|
||||||
|
currentPosition[3] = 5
|
||||||
|
currentPosition[0] = 0
|
||||||
|
case 2:
|
||||||
|
currentPosition[3] = 0
|
||||||
|
currentPosition[0] = invertPosition(currentPosition[0], highestPos)
|
||||||
|
currentPosition[1] = 0
|
||||||
|
currentPosition[2] = invertDirection(currentPosition[2])
|
||||||
|
case 3:
|
||||||
|
currentPosition[3] = 2
|
||||||
|
currentPosition[0] = currentPosition[1]
|
||||||
|
currentPosition[1] = 0
|
||||||
|
currentPosition = rotate('R', currentPosition)
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
if currentPosition[3] == 4 {
|
||||||
|
switch currentPosition[2] {
|
||||||
|
//Potential Error
|
||||||
|
case 0:
|
||||||
|
currentPosition[3] = 1
|
||||||
|
currentPosition[0] = invertPosition(currentPosition[0], highestPos)
|
||||||
|
currentPosition[1] = highestPos
|
||||||
|
currentPosition[2] = invertDirection(currentPosition[2])
|
||||||
|
case 1:
|
||||||
|
currentPosition[3] = 5
|
||||||
|
currentPosition[0] = currentPosition[1]
|
||||||
|
currentPosition[1] = highestPos
|
||||||
|
currentPosition = rotate('R', currentPosition)
|
||||||
|
case 2:
|
||||||
|
currentPosition[3] = 3
|
||||||
|
currentPosition[1] = highestPos
|
||||||
|
case 3:
|
||||||
|
currentPosition[3] = 2
|
||||||
|
currentPosition[0] = highestPos
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
if currentPosition[3] == 5 {
|
||||||
|
switch currentPosition[2] {
|
||||||
|
case 0:
|
||||||
|
currentPosition[3] = 4
|
||||||
|
currentPosition[1] = currentPosition[0]
|
||||||
|
currentPosition[0] = highestPos
|
||||||
|
currentPosition = rotate('L', currentPosition)
|
||||||
|
case 1:
|
||||||
|
currentPosition[3] = 1
|
||||||
|
currentPosition[0] = 0
|
||||||
|
case 2:
|
||||||
|
currentPosition[3] = 0
|
||||||
|
currentPosition[1] = currentPosition[0]
|
||||||
|
currentPosition[0] = 0
|
||||||
|
currentPosition = rotate('L', currentPosition)
|
||||||
|
case 3:
|
||||||
|
currentPosition[3] = 3
|
||||||
|
currentPosition[0] = highestPos
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
return currentPosition
|
||||||
|
}
|
||||||
|
|
||||||
|
func invertPosition(input int, highestPos int) int {
|
||||||
|
return highestPos - input
|
||||||
|
}
|
||||||
|
func invertDirection(input int) int {
|
||||||
|
if input > 1 {
|
||||||
|
return input - 2
|
||||||
|
} else {
|
||||||
|
return input + 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
17
day22/testinput2
Normal file
17
day22/testinput2
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
...#..#.
|
||||||
|
.#......
|
||||||
|
#....#..
|
||||||
|
........
|
||||||
|
...#
|
||||||
|
#...
|
||||||
|
....
|
||||||
|
..#.
|
||||||
|
.......#
|
||||||
|
........
|
||||||
|
...#.#..
|
||||||
|
........
|
||||||
|
...#
|
||||||
|
....
|
||||||
|
..#.
|
||||||
|
....
|
||||||
|
10R10R2L10L1R3L5L2L2L5R3L10
|
17
day22/testinput2Copy
Normal file
17
day22/testinput2Copy
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
0>1#..#^
|
||||||
|
.#v...8^
|
||||||
|
#.v..#^^
|
||||||
|
..v..3>4
|
||||||
|
..v#
|
||||||
|
#.2>
|
||||||
|
>>>>
|
||||||
|
..#.
|
||||||
|
..^....#
|
||||||
|
..7<<<<6
|
||||||
|
...#.#.^
|
||||||
|
.......^
|
||||||
|
...#
|
||||||
|
....
|
||||||
|
..#.
|
||||||
|
...5>
|
||||||
|
10R5L5R10L4R5L5R10
|
17
day22/testinput2Copy2
Normal file
17
day22/testinput2Copy2
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
>>1#..#.
|
||||||
|
.#v.....
|
||||||
|
#.v..#..
|
||||||
|
<<2.....
|
||||||
|
...#
|
||||||
|
#...
|
||||||
|
<8..
|
||||||
|
.^#.
|
||||||
|
>3v..^.#
|
||||||
|
6v>>>7..
|
||||||
|
^vv#.#..
|
||||||
|
54v.....
|
||||||
|
..v#
|
||||||
|
..9.
|
||||||
|
..#.
|
||||||
|
....
|
||||||
|
10R3R4R3R1R2R5L3L50
|
17
day22/testinput2Copy3
Normal file
17
day22/testinput2Copy3
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
>1.#..#8
|
||||||
|
.#....5>
|
||||||
|
#....#4<
|
||||||
|
........
|
||||||
|
...#
|
||||||
|
#...
|
||||||
|
....
|
||||||
|
..#.
|
||||||
|
.......#
|
||||||
|
..3>>>>>
|
||||||
|
..^#.#6<
|
||||||
|
..^...7>
|
||||||
|
..^#
|
||||||
|
>>2.
|
||||||
|
..#.
|
||||||
|
....
|
||||||
|
1L3L4R7R1R10L1L7
|
17
day22/testinput2Copy4
Normal file
17
day22/testinput2Copy4
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
>>1#..#.
|
||||||
|
7#2>>>3.
|
||||||
|
#....#v.
|
||||||
|
<<<<<<4.
|
||||||
|
...#
|
||||||
|
#...
|
||||||
|
....
|
||||||
|
..#.
|
||||||
|
>5.....#
|
||||||
|
.v......
|
||||||
|
<6.#.#..
|
||||||
|
........
|
||||||
|
...#
|
||||||
|
....
|
||||||
|
..#.
|
||||||
|
....
|
||||||
|
10R1L4R2R9R2R10
|
17
day22/testinputCopy5
Normal file
17
day22/testinputCopy5
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
>>1#.v#^
|
||||||
|
.#v..7>8
|
||||||
|
#.v..#..
|
||||||
|
..v.....
|
||||||
|
..v#
|
||||||
|
#.v.
|
||||||
|
3<2.
|
||||||
|
v.#.
|
||||||
|
....v..#
|
||||||
|
....v...
|
||||||
|
...#v#..
|
||||||
|
....45..
|
||||||
|
11..#
|
||||||
|
^6<<
|
||||||
|
^v#.
|
||||||
|
10v<9
|
||||||
|
10R10R2L10L1R3L5L2L2L5R3L10
|
Loading…
Reference in New Issue
Block a user