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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
// Use min and max to check if fully contained, since we have sorted sections
type section struct {
min int
max int
}
func sectionFromString(sectionRange string) section {
minmax := strings.Split(sectionRange, "-")
min, errMin := strconv.Atoi(minmax[0])
max, errMax := strconv.Atoi(minmax[1])
if errMin != nil || errMax != nil {
log.Fatal("Got unexpected input", sectionRange)
}
return section{
min: min,
max: max,
}
}
func (s section) isInside(o section) bool {
if o.min <= s.min && s.max <= o.max {
return true
}
return false
}
// Use negative cases because it is easier to think about when ranges don't overlap
func (s section) isOverlapping(o section) bool {
if s.max < o.min {
return false
}
if o.max < s.min {
return false
}
return true
}
func main() {
f, err := os.Open("day4.txt")
if err != nil {
log.Fatal("Could not open input file")
}
// part 1
scanner := bufio.NewScanner(f)
runningSum := 0
for scanner.Scan() {
sectionStrings := strings.Split(scanner.Text(), ",")
sectionA, sectionB := sectionFromString(sectionStrings[0]), sectionFromString(sectionStrings[1])
if sectionA.isInside(sectionB) || sectionB.isInside(sectionA) {
runningSum += 1
}
}
fmt.Println("Part 1:", runningSum)
f.Seek(0, 0)
scanner = bufio.NewScanner(f)
// Part 2
runningSum = 0
for scanner.Scan() {
sectionStrings := strings.Split(scanner.Text(), ",")
sectionA, sectionB := sectionFromString(sectionStrings[0]), sectionFromString(sectionStrings[1])
if sectionA.isOverlapping(sectionB) {
runningSum += 1
}
}
fmt.Println("Part 2:", runningSum)
}
|