-
Notifications
You must be signed in to change notification settings - Fork 0
/
04p2.go
44 lines (35 loc) · 1.11 KB
/
04p2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"strconv"
"strings"
"aoc2022/utils"
)
func D04P2() {
lines := utils.ReadLines("./inputs/04.txt")
totalOverlaps := 0
var assignmentPairs []map[string]int
for _, line := range lines {
assignmentPair := make(map[string]int)
assignments := strings.Split(line, ",")
for index, assignment := range assignments {
assignmentRange := strings.Split(assignment, "-")
assignmentPair["elf"+strconv.Itoa(index+1)+"RangeStart"], _ = strconv.Atoi(
assignmentRange[0],
)
assignmentPair["elf"+strconv.Itoa(index+1)+"RangeEnd"], _ = strconv.Atoi(
assignmentRange[1],
)
}
assignmentPairs = append(assignmentPairs, assignmentPair)
}
for _, assignmentPair := range assignmentPairs {
if assignmentPair["elf1RangeStart"] <= assignmentPair["elf2RangeEnd"] &&
assignmentPair["elf1RangeEnd"] >= assignmentPair["elf2RangeStart"] {
totalOverlaps++
} else if assignmentPair["elf2RangeStart"] <= assignmentPair["elf1RangeEnd"] && assignmentPair["elf2RangeEnd"] >= assignmentPair["elf1RangeStart"] {
totalOverlaps++
}
}
fmt.Println("Total Overlaps:", totalOverlaps)
}