@@ -31,21 +31,40 @@ func main() {
3131 }
3232
3333 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+
3442}
3543
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 ])
4149 return
4250}
4351
4452func CountOverlappingSectionAssignment (input * os.File ) (overlap int , err error ) {
4553 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 ))
4755 // 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 {
4968 overlap ++
5069 }
5170 })
0 commit comments