-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.go
75 lines (64 loc) · 1.16 KB
/
day4.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
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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func get_range(pair string) (int, int) {
pas := strings.Split(pair, "-")
xa, _ := strconv.Atoi(pas[0])
ya, _ := strconv.Atoi(pas[1])
return xa, ya
}
func is_full_overlap(pa string, pb string) bool {
xa, ya := get_range(pa)
xb, yb := get_range(pb)
if xa <= xb && ya >= yb {
return true
}
if xb <= xa && yb >= ya {
return true
}
return false
}
func is_overlap(pa string, pb string) bool {
xa, ya := get_range(pa)
xb, yb := get_range(pb)
if xa <= xb && xb <= ya {
return true
}
if xa <= yb && yb <= ya {
return true
}
if xb <= xa && xa <= yb {
return true
}
if xb <= ya && ya <= yb {
return true
}
return false
}
func main() {
input, err := os.ReadFile("./input/input4.txt")
check(err)
lines := strings.Split(string(input), "\r\n")
full_overlaps := 0
overlaps := 0
for _, line := range lines {
pairs := strings.Split(line, ",")
if is_full_overlap(pairs[0], pairs[1]) {
full_overlaps++
}
if is_overlap(pairs[0], pairs[1]) {
overlaps++
}
}
fmt.Println("part 1: ", full_overlaps)
fmt.Println("part 2: ", overlaps)
}