-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
100 lines (84 loc) · 1.69 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"io"
"sort"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
lines := lib.ReaderToStrings(input)
var points []Point
for _, line := range lines {
points = append(points, toPoint(line))
}
sort.Slice(points, func(i, j int) bool {
a := points[i]
b := points[j]
return a.distance(Point{}) < b.distance(Point{})
})
var constellations [][]Point
constellations = append(constellations, []Point{points[0]})
for i := 1; i < len(points); i++ {
point := points[i]
added := false
for id, constellation := range constellations {
belong := false
for _, cp := range constellation {
if point.sameConstellation(cp) {
belong = true
break
}
}
if belong {
constellations[id] = append(constellations[id], point)
added = true
break
}
}
if !added {
constellations = append(constellations, []Point{point})
}
}
res := len(constellations)
for i := 0; i < len(constellations); i++ {
for j := i + 1; j < len(constellations); j++ {
if canGroup(constellations[i], constellations[j]) {
res--
}
}
}
return res
}
func canGroup(c1, c2 []Point) bool {
for _, p := range c1 {
for _, p2 := range c2 {
if p.sameConstellation(p2) {
return true
}
}
}
return false
}
type Point struct {
a int
b int
c int
d int
}
func (p Point) sameConstellation(p2 Point) bool {
return p.distance(p2) <= 3
}
func (p Point) distance(p2 Point) int {
return lib.Abs(p.a-p2.a) +
lib.Abs(p.b-p2.b) +
lib.Abs(p.c-p2.c) +
lib.Abs(p.d-p2.d)
}
func toPoint(s string) Point {
del := lib.NewDelimiter(s, ",")
return Point{
a: del.GetInt(0),
b: del.GetInt(1),
c: del.GetInt(2),
d: del.GetInt(3),
}
}