56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
package day19
|
|
|
|
import (
|
|
"math"
|
|
"tools"
|
|
)
|
|
|
|
func getNextIndex(m map[int]int, start, max int) int {
|
|
index := start
|
|
for {
|
|
index++
|
|
if index > max {
|
|
index = 0
|
|
}
|
|
if _, ok := m[index]; ok {
|
|
return index
|
|
}
|
|
}
|
|
}
|
|
|
|
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
|
elfCount := puzzle.GetInputInt()
|
|
elfs := make(map[int]int)
|
|
for i := 0; i < elfCount; i++ {
|
|
elfs[i] = 1
|
|
}
|
|
|
|
index := 0
|
|
for len(elfs) > 1 {
|
|
nextIndex := getNextIndex(elfs, index, elfCount)
|
|
delete(elfs, nextIndex)
|
|
index = getNextIndex(elfs, nextIndex, elfCount)
|
|
}
|
|
|
|
return index + 1
|
|
}
|
|
|
|
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
|
elfCount := puzzle.GetInputInt()
|
|
var elfs []int
|
|
for i := 0; i < elfCount; i++ {
|
|
elfs = append(elfs, i+1)
|
|
}
|
|
|
|
// Future optimization path?
|
|
// 0..4; 0 eliminates 2 (index + floor(half), 1 eliminates 4 (1 + half(len)), 3 eliminates 1 (index + floor(half))
|
|
// eliminate (1:?)2:1:2:1:...until len(2) (start with 1 for equal lens and 2 for unequal lens)
|
|
for len(elfs) > 1 {
|
|
half := int(math.Floor(float64(len(elfs) / 2)))
|
|
elfs = append(elfs, elfs[0])
|
|
elfs = append(elfs[1:half], elfs[half+1:]...)
|
|
}
|
|
|
|
return elfs[0]
|
|
}
|