57 lines
988 B
Go
57 lines
988 B
Go
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
|
|
}
|