day20
This commit is contained in:
parent
9878e51904
commit
712cab8120
@ -42,10 +42,9 @@ func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||
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!
|
||||
// Future optimization path?
|
||||
// 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)
|
||||
// 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])
|
||||
|
||||
56
day20/day.go
Normal file
56
day20/day.go
Normal file
@ -0,0 +1,56 @@
|
||||
package day20
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"tools"
|
||||
)
|
||||
|
||||
func getBlacklistMap(inp []string) (map[int]int, []int) {
|
||||
ips := make(map[int]int)
|
||||
var lips []int
|
||||
for _, l := range inp {
|
||||
p := strings.Split(l, "-")
|
||||
i1, _ := strconv.Atoi(p[0])
|
||||
i2, _ := strconv.Atoi(p[1])
|
||||
ips[i1] = i2
|
||||
lips = append(lips, i1)
|
||||
}
|
||||
sort.Ints(lips)
|
||||
return ips, lips
|
||||
}
|
||||
|
||||
func Part1(puzzle tools.AoCPuzzle) interface{} {
|
||||
ips, lips := getBlacklistMap(puzzle.GetInputArray())
|
||||
|
||||
index := 0
|
||||
for _, lip := range lips {
|
||||
if lip > index {
|
||||
break
|
||||
}
|
||||
if index >= lip && index <= ips[lip] {
|
||||
index = ips[lip] + 1
|
||||
}
|
||||
}
|
||||
|
||||
return index
|
||||
}
|
||||
|
||||
func Part2(puzzle tools.AoCPuzzle) interface{} {
|
||||
ips, lips := getBlacklistMap(puzzle.GetInputArray())
|
||||
|
||||
cips := make(map[int]int)
|
||||
var minIp, maxIp = 0, 0
|
||||
for _, lip := range lips {
|
||||
if lip >= minIp && lip <= maxIp+1 {
|
||||
maxIp = tools.Max(ips[lip], maxIp)
|
||||
} else {
|
||||
minIp = lip
|
||||
maxIp = ips[lip]
|
||||
}
|
||||
cips[minIp] = maxIp
|
||||
}
|
||||
|
||||
return len(cips) - 1
|
||||
}
|
||||
3
inputs/20_test
Normal file
3
inputs/20_test
Normal file
@ -0,0 +1,3 @@
|
||||
5-8
|
||||
0-2
|
||||
4-7
|
||||
3
main.go
3
main.go
@ -19,6 +19,7 @@ import (
|
||||
"aoc2016/day17"
|
||||
"aoc2016/day18"
|
||||
"aoc2016/day19"
|
||||
"aoc2016/day20"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@ -55,7 +56,7 @@ func initDayFunctions() {
|
||||
17: {1: day17.Part1, 2: day17.Part2},
|
||||
18: {1: day18.Part1, 2: day18.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},
|
||||
// 22: {1: day22.Part1, 2: day22.Part2},
|
||||
// 23: {1: day23.Part1, 2: day23.Part2},
|
||||
|
||||
Loading…
Reference in New Issue
Block a user