@@ -31,21 +31,40 @@ func main() {
31
31
}
32
32
33
33
fmt .Printf ("Part 1: %d\n " , p1 )
34
+
35
+ file .Seek (0 , 0 )
36
+ p2 , err := CountPartialOverlappingSectionAssignment (file )
37
+ if err != nil {
38
+ log .Fatal (err )
39
+ }
40
+ fmt .Printf ("Part 2: %d\n " , p2 )
41
+
34
42
}
35
43
36
- func MapSections (i []string ) (ax , ay , bx , by int ) {
37
- ax , _ = strconv .Atoi (i [0 ])
38
- ay , _ = strconv .Atoi (i [1 ])
39
- bx , _ = strconv .Atoi (i [2 ])
40
- by , _ = strconv .Atoi (i [3 ])
44
+ func MapSections (i []string ) (x1 , x2 , y1 , y2 int ) {
45
+ x1 , _ = strconv .Atoi (i [0 ])
46
+ x2 , _ = strconv .Atoi (i [1 ])
47
+ y1 , _ = strconv .Atoi (i [2 ])
48
+ y2 , _ = strconv .Atoi (i [3 ])
41
49
return
42
50
}
43
51
44
52
func CountOverlappingSectionAssignment (input * os.File ) (overlap int , err error ) {
45
53
err = IterateLines (input , func (s string ) {
46
- var ax , ay , bx , by int = MapSections (re .FindAllString (s , - 1 ))
54
+ var x1 , x2 , y1 , y2 int = MapSections (re .FindAllString (s , - 1 ))
47
55
// check if A contains B
48
- if ax <= bx && ay >= by || bx <= ax && by >= ay {
56
+ if x1 <= y1 && x2 >= y2 || y1 <= x1 && y2 >= x2 {
57
+ overlap ++
58
+ }
59
+ })
60
+ return
61
+ }
62
+
63
+ func CountPartialOverlappingSectionAssignment (input * os.File ) (overlap int , err error ) {
64
+ err = IterateLines (input , func (s string ) {
65
+ var x1 , x2 , y1 , y2 int = MapSections (re .FindAllString (s , - 1 ))
66
+ // for any overlap
67
+ if x1 <= y2 && y1 <= x2 {
49
68
overlap ++
50
69
}
51
70
})
0 commit comments