day19; p2 being horribly slow

This commit is contained in:
Stefan Harmuth 2021-10-25 04:00:33 +02:00
parent 44b8be5a9e
commit 9878e51904
4 changed files with 60 additions and 1 deletions

56
day19/day.go Normal file
View File

@ -0,0 +1,56 @@
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]
}

1
inputs/19 Normal file
View File

@ -0,0 +1 @@
3004953

1
inputs/19_test Normal file
View File

@ -0,0 +1 @@
5

View File

@ -18,6 +18,7 @@ import (
"aoc2016/day16" "aoc2016/day16"
"aoc2016/day17" "aoc2016/day17"
"aoc2016/day18" "aoc2016/day18"
"aoc2016/day19"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -53,7 +54,7 @@ func initDayFunctions() {
16: {1: day16.Part1, 2: day16.Part2}, 16: {1: day16.Part1, 2: day16.Part2},
17: {1: day17.Part1, 2: day17.Part2}, 17: {1: day17.Part1, 2: day17.Part2},
18: {1: day18.Part1, 2: day18.Part2}, 18: {1: day18.Part1, 2: day18.Part2},
// 19: {1: day19.Part1, 2: day19.Part2}, 19: {1: day19.Part1, 2: day19.Part2},
// 20: {1: day20.Part1, 2: day20.Part2}, // 20: {1: day20.Part1, 2: day20.Part2},
// 21: {1: day21.Part1, 2: day21.Part2}, // 21: {1: day21.Part1, 2: day21.Part2},
// 22: {1: day22.Part1, 2: day22.Part2}, // 22: {1: day22.Part1, 2: day22.Part2},