aoc2016/day19/day.go
2021-10-25 04:00:33 +02:00

57 lines
1.2 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)
}
// Does one half not always eliminate the other half +/- 1? Not Quite!!!
// Does it eliminate half of the "upper" half? As in "every second one"? Also not Quite!
// 0..4; 0 eliminates 2 (index + floor(half), 1 eliminates 4 (1 + half(len)), 3 eliminates 1 (index + floor(half))
// (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]
}