Day20
This commit is contained in:
parent
6feb1f428a
commit
aec2b2c848
102
day20/day20.go
Normal file
102
day20/day20.go
Normal file
@ -0,0 +1,102 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"AOC2022/helper"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
lines := helper.ReadTextFile(args[0])
|
||||
part1(lines)
|
||||
part2(lines)
|
||||
|
||||
}
|
||||
|
||||
func part2(lines []string) {
|
||||
numbers := helper.StringSliceToIntSlice(lines)
|
||||
numbersWithOriginalIndex := make([][2]int, len(numbers))
|
||||
for i, number := range numbers {
|
||||
numbersWithOriginalIndex[i] = [2]int{number * 811589153, i}
|
||||
}
|
||||
for j := 0; j < 10; j++ {
|
||||
for i := 0; i < len(numbers); i++ {
|
||||
shift(i, &numbersWithOriginalIndex)
|
||||
}
|
||||
}
|
||||
printNumbers := []int{}
|
||||
currentIndex0 := -1
|
||||
for i, number := range numbersWithOriginalIndex {
|
||||
if number[0] == 0 {
|
||||
currentIndex0 = i
|
||||
}
|
||||
printNumbers = append(printNumbers, number[0])
|
||||
}
|
||||
sum := 0
|
||||
iterator := 1000 % len(printNumbers)
|
||||
for i := 0; i < 3; i++ {
|
||||
currentIndex0 = (currentIndex0 + iterator) % len(printNumbers)
|
||||
sum += printNumbers[currentIndex0]
|
||||
}
|
||||
fmt.Println(sum)
|
||||
}
|
||||
|
||||
func part1(lines []string) {
|
||||
numbers := helper.StringSliceToIntSlice(lines)
|
||||
numbersWithOriginalIndex := make([][2]int, len(numbers))
|
||||
for i, number := range numbers {
|
||||
numbersWithOriginalIndex[i] = [2]int{number, i}
|
||||
}
|
||||
for i := 0; i < len(numbers); i++ {
|
||||
shift(i, &numbersWithOriginalIndex)
|
||||
}
|
||||
printNumbers := []int{}
|
||||
currentIndex0 := -1
|
||||
for i, number := range numbersWithOriginalIndex {
|
||||
if number[0] == 0 {
|
||||
currentIndex0 = i
|
||||
}
|
||||
printNumbers = append(printNumbers, number[0])
|
||||
}
|
||||
fmt.Println(printNumbers)
|
||||
sum := 0
|
||||
iterator := 1000 % len(printNumbers)
|
||||
for i := 0; i < 3; i++ {
|
||||
currentIndex0 = (currentIndex0 + iterator) % len(printNumbers)
|
||||
sum += printNumbers[currentIndex0]
|
||||
}
|
||||
fmt.Println(sum)
|
||||
}
|
||||
|
||||
func getNthElementAfterN(currentIndex, MoveForward, lengthArray int) int {
|
||||
remaining := MoveForward % (lengthArray - 1)
|
||||
newIndex := currentIndex + remaining
|
||||
if newIndex >= lengthArray {
|
||||
newIndex = newIndex % (lengthArray - 1)
|
||||
}
|
||||
if newIndex <= 0 {
|
||||
newIndex = lengthArray - 1 + newIndex
|
||||
}
|
||||
return newIndex
|
||||
}
|
||||
|
||||
func shift(index int, numbersWithOriginalIndex *[][2]int) {
|
||||
currentIndex := getNumberWithOriginalIndexN(index, numbersWithOriginalIndex)
|
||||
currentValue := (*numbersWithOriginalIndex)[currentIndex][0]
|
||||
move(numbersWithOriginalIndex, currentIndex, currentValue)
|
||||
}
|
||||
|
||||
func move[T any](numbersWithOriginalIndex *[]T, currentIndex int, currentValue int) {
|
||||
newPosition := getNthElementAfterN(currentIndex, currentValue, len(*numbersWithOriginalIndex))
|
||||
helper.Move(numbersWithOriginalIndex, currentIndex, newPosition)
|
||||
}
|
||||
|
||||
func getNumberWithOriginalIndexN(index int, numbersWithOriginalIndex *[][2]int) int {
|
||||
for i, number := range *numbersWithOriginalIndex {
|
||||
if number[1] == index {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
5000
day20/input
Normal file
5000
day20/input
Normal file
File diff suppressed because it is too large
Load Diff
7
day20/testinput
Normal file
7
day20/testinput
Normal file
@ -0,0 +1,7 @@
|
||||
1
|
||||
2
|
||||
-3
|
||||
3
|
||||
-2
|
||||
0
|
||||
4
|
@ -104,6 +104,20 @@ func Remove[T any](s *[]T, i int) {
|
||||
*s = (*s)[:len(*s)-1]
|
||||
}
|
||||
|
||||
func RemoveOrder[T any](s *[]T, i int) {
|
||||
*s = append((*s)[:i], (*s)[i+1:]...)
|
||||
}
|
||||
|
||||
func Insert[T any](s *[]T, v T, i int) {
|
||||
*s = append((*s)[:i], append([]T{v}, (*s)[i:]...)...)
|
||||
}
|
||||
|
||||
func Move[T any](s *[]T, srcIndex int, dstIndex int) {
|
||||
value := (*s)[srcIndex]
|
||||
RemoveOrder(s, srcIndex)
|
||||
Insert(s, value, dstIndex)
|
||||
}
|
||||
|
||||
func RemoveElement[T constraints.Ordered](s []T, i T) []T {
|
||||
newS := []T{}
|
||||
for _, val := range s {
|
||||
|
Loading…
Reference in New Issue
Block a user