Improve day 20
This commit is contained in:
@@ -13,42 +13,24 @@ type Arrangement struct {
|
||||
|
||||
func (arrangement *Arrangement) Mix(number int) {
|
||||
newSequence := make([]int, len(arrangement.numbers))
|
||||
oldIndices := make([]int, 0, len(arrangement.numbers))
|
||||
var newIndex int
|
||||
|
||||
currentIndices := make([]int, len(arrangement.numbers))
|
||||
for i := range arrangement.numbers {
|
||||
oldIndices = append(oldIndices, i)
|
||||
currentIndices[i] = 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++ {
|
||||
for j := 0; j < len(currentIndices); 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 {
|
||||
@@ -58,12 +40,6 @@ func (arrangement *Arrangement) Mix(number int) {
|
||||
}
|
||||
}
|
||||
currentIndices[j] = newIndex
|
||||
|
||||
offset %= len(newSequence)
|
||||
}
|
||||
|
||||
for i := range currentIndices {
|
||||
oldIndices[i] = currentIndices[i]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user