From 9878e51904527ab9b0b9e88b75c26b41eca3923d Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 25 Oct 2021 04:00:33 +0200 Subject: [PATCH] day19; p2 being horribly slow --- day19/day.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ inputs/19 | 1 + inputs/19_test | 1 + main.go | 3 ++- 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 day19/day.go create mode 100644 inputs/19 create mode 100644 inputs/19_test diff --git a/day19/day.go b/day19/day.go new file mode 100644 index 0000000..12da140 --- /dev/null +++ b/day19/day.go @@ -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] +} diff --git a/inputs/19 b/inputs/19 new file mode 100644 index 0000000..bdf0aa3 --- /dev/null +++ b/inputs/19 @@ -0,0 +1 @@ +3004953 \ No newline at end of file diff --git a/inputs/19_test b/inputs/19_test new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/inputs/19_test @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/main.go b/main.go index f804b3f..e5e2cca 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "aoc2016/day16" "aoc2016/day17" "aoc2016/day18" + "aoc2016/day19" "flag" "fmt" "os" @@ -53,7 +54,7 @@ func initDayFunctions() { 16: {1: day16.Part1, 2: day16.Part2}, 17: {1: day17.Part1, 2: day17.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}, // 21: {1: day21.Part1, 2: day21.Part2}, // 22: {1: day22.Part1, 2: day22.Part2},