Day 20
This commit is contained in:
105
day20/common/mixing.go
Normal file
105
day20/common/mixing.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type Arrangement struct {
|
||||||
|
numbers []int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (arrangement *Arrangement) Mix(number int) {
|
||||||
|
newSequence := make([]int, len(arrangement.numbers))
|
||||||
|
oldIndices := make([]int, 0, len(arrangement.numbers))
|
||||||
|
var newIndex int
|
||||||
|
|
||||||
|
for i := range arrangement.numbers {
|
||||||
|
oldIndices = append(oldIndices, i)
|
||||||
|
newSequence[i] = arrangement.numbers[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < number; i++ {
|
||||||
|
currentIndices := make([]int, len(oldIndices))
|
||||||
|
for j := range oldIndices {
|
||||||
|
currentIndices[j] = oldIndices[j]
|
||||||
|
}
|
||||||
|
|
||||||
|
for j := 0; j < len(oldIndices); j++ {
|
||||||
|
offset := currentIndices[j]
|
||||||
|
currentNumber := arrangement.numbers[j]
|
||||||
|
newIndex = offset + currentNumber
|
||||||
|
newIndex = (newIndex % (len(newSequence) - 1) + len(newSequence) - 1) % (len(newSequence) - 1)
|
||||||
|
|
||||||
|
if offset == 0 {
|
||||||
|
newSequence = newSequence[1:]
|
||||||
|
} else if offset == len(newSequence) - 1 {
|
||||||
|
newSequence = newSequence[:len(newSequence) - 1]
|
||||||
|
} else {
|
||||||
|
newSequence = append(newSequence[:offset], newSequence[offset + 1:]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
if newIndex == 0 {
|
||||||
|
newSequence = append([]int{currentNumber}, newSequence...)
|
||||||
|
} else if newIndex == len(newSequence) {
|
||||||
|
newSequence = append(newSequence, currentNumber)
|
||||||
|
} else {
|
||||||
|
newSequence = append(newSequence[:newIndex+1], newSequence[newIndex:]...)
|
||||||
|
newSequence[newIndex] = currentNumber
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range currentIndices {
|
||||||
|
if newIndex <= currentIndices[i] && currentIndices[i] < offset {
|
||||||
|
currentIndices[i]++
|
||||||
|
} else if offset < currentIndices[i] && currentIndices[i] <= newIndex {
|
||||||
|
currentIndices[i]--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentIndices[j] = newIndex
|
||||||
|
|
||||||
|
offset %= len(newSequence)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range currentIndices {
|
||||||
|
oldIndices[i] = currentIndices[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
arrangement.numbers = newSequence
|
||||||
|
}
|
||||||
|
|
||||||
|
func (arrangement *Arrangement) ApplyDecryptionKey(key int) {
|
||||||
|
for i := range arrangement.numbers {
|
||||||
|
arrangement.numbers[i] *= key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (arrangement *Arrangement) GetCoordinates() (int, int, int) {
|
||||||
|
var locationOf0 int
|
||||||
|
for i := range(arrangement.numbers) {
|
||||||
|
if arrangement.numbers[i] == 0 {
|
||||||
|
locationOf0 = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
x := arrangement.numbers[(locationOf0 + 1000) % len(arrangement.numbers)]
|
||||||
|
y := arrangement.numbers[(locationOf0 + 2000) % len(arrangement.numbers)]
|
||||||
|
z := arrangement.numbers[(locationOf0 + 3000) % len(arrangement.numbers)]
|
||||||
|
|
||||||
|
return x, y, z
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func Parse(scanner bufio.Scanner) Arrangement {
|
||||||
|
arrangement := Arrangement{}
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
number, _ := strconv.Atoi(line)
|
||||||
|
arrangement.numbers = append(arrangement.numbers, number)
|
||||||
|
}
|
||||||
|
|
||||||
|
return arrangement
|
||||||
|
}
|
||||||
17
day20/ex1/main.go
Normal file
17
day20/ex1/main.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"aoc2022/day20/common"
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arrangement := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||||
|
|
||||||
|
arrangement.Mix(1)
|
||||||
|
x, y, z := arrangement.GetCoordinates()
|
||||||
|
|
||||||
|
fmt.Println(x + y + z)
|
||||||
|
}
|
||||||
18
day20/ex2/main.go
Normal file
18
day20/ex2/main.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"aoc2022/day20/common"
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
arrangement := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||||
|
|
||||||
|
arrangement.ApplyDecryptionKey(811589153)
|
||||||
|
arrangement.Mix(10)
|
||||||
|
x, y, z := arrangement.GetCoordinates()
|
||||||
|
|
||||||
|
fmt.Println(x + y + z)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user