-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3_part2.go
68 lines (59 loc) · 1.18 KB
/
day3_part2.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
package day3_part2
import (
"strings"
"GoAdventOfCode/util"
)
type Area struct {
X1 int
Y1 int
X2 int
Y2 int
}
func getAnswer(lines []string) string {
m := make(map[string]int)
list := make(map[string]int)
for i, line1 := range lines {
id1, area1 := getArea(line1)
list[id1] = 1
for j, line2 := range lines {
if j > i {
id2, area2 := getArea(line2)
ok := doAreasOverlap(area1, area2)
//fmt.Printf("checking: %v and %v = %t\n", id1, id2, ok)
if ok {
m[id1]++
m[id2]++
}
}
}
}
for k := range list {
_, ok := m[k]
if !ok {
return k
}
}
return ""
}
func getArea(line string) (string, Area) {
parts := strings.Fields(line)
id := parts[0]
p1 := strings.TrimSuffix(parts[2], ":")
parts1 := strings.Split(p1, ",")
parts2 := strings.Split(parts[3], "x")
sx := util.ConvertStringToInt(parts1[0])
sy := util.ConvertStringToInt(parts1[1])
area := Area{
X1: sx,
Y1: sy,
X2: sx + util.ConvertStringToInt(parts2[0]) - 1,
Y2: sy + util.ConvertStringToInt(parts2[1]) - 1,
}
return id, area
}
func doAreasOverlap(a Area, b Area) bool {
if a.X1 <= b.X2 && a.X2 >= b.X1 && a.Y1 <= b.Y2 && a.Y2 >= b.Y1 {
return true
}
return false
}