84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
|
)
|
|
|
|
func main() {
|
|
inp := h.StdinToStringSlice()
|
|
part1(inp)
|
|
part2(inp)
|
|
}
|
|
|
|
func part1(inp []string) {
|
|
fmt.Println("# Part 1")
|
|
var ret int
|
|
for _, pair := range inp {
|
|
elves := strings.Split(pair, ",")
|
|
e1S, e1E := getRange(elves[0])
|
|
e2S, e2E := getRange(elves[1])
|
|
if rangesFullyOverlap(e1S, e1E, e2S, e2E) {
|
|
ret++
|
|
}
|
|
}
|
|
fmt.Printf("Fully Overlapping Ranges: %d\n", ret)
|
|
}
|
|
|
|
func part2(inp []string) {
|
|
fmt.Println("# Part 2")
|
|
var ret int
|
|
for _, pair := range inp {
|
|
elves := strings.Split(pair, ",")
|
|
e1S, e1E := getRange(elves[0])
|
|
e2S, e2E := getRange(elves[1])
|
|
if rangesOverlap(e1S, e1E, e2S, e2E) {
|
|
ret++
|
|
}
|
|
}
|
|
fmt.Printf("Overlapping Ranges: %d\n", ret)
|
|
}
|
|
|
|
func partXAllOverlaps(inp []string) {
|
|
fmt.Println("# Part x")
|
|
var overlaps int
|
|
sections := make(map[int]int)
|
|
for _, pair := range inp {
|
|
elves := strings.Split(pair, ",")
|
|
for elfIdx, elf := range elves {
|
|
var overlapped bool
|
|
i1, i2 := getRange(elf)
|
|
for i := i1; i <= i2; i++ {
|
|
if _, ok := sections[i]; ok {
|
|
overlapped = true
|
|
} else {
|
|
sections[i] = elfIdx
|
|
}
|
|
}
|
|
if overlapped {
|
|
overlaps++
|
|
}
|
|
}
|
|
}
|
|
fmt.Printf("Overlapping Assignments: %d\n", overlaps)
|
|
}
|
|
|
|
func rangesFullyOverlap(e1start, e1end, e2start, e2end int) bool {
|
|
return (e1start >= e2start && e1end <= e2end) ||
|
|
(e2start >= e1start && e2end <= e1end)
|
|
}
|
|
func rangesOverlap(e1start, e1end, e2start, e2end int) bool {
|
|
ret := (e1start >= e2start && e1start <= e2end) ||
|
|
(e1end >= e2start && e1end <= e2end) ||
|
|
(e2start >= e1start && e2start <= e1end) ||
|
|
(e2end >= e1start && e2end <= e1end)
|
|
return ret
|
|
}
|
|
|
|
func getRange(elf string) (int, int) {
|
|
idxsS := strings.Split(elf, "-")
|
|
return h.Atoi(idxsS[0]), h.Atoi(idxsS[1])
|
|
}
|