generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
60 lines (47 loc) · 979 Bytes
/
solution.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
package day11p1
import (
"io"
"aoc/utils"
"github.com/devries/combs"
)
var expansionFactor = 2
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
galaxyRows := make(map[int]bool)
galaxyColumns := make(map[int]bool)
galaxies := []utils.Point{}
// Parse input
for j, ln := range lines {
for i, r := range ln {
if r == '#' {
p := utils.Point{X: i, Y: j}
galaxies = append(galaxies, p)
galaxyRows[j] = true
galaxyColumns[i] = true
}
}
}
sum := 0
// go through all combinations
for combo := range combs.Combinations(2, galaxies) {
d := 0
// count x distance
for i := min(combo[0].X, combo[1].X); i < max(combo[0].X, combo[1].X); i++ {
if galaxyColumns[i] {
d++
} else {
d += expansionFactor
}
}
// count y distance
for j := min(combo[0].Y, combo[1].Y); j < max(combo[0].Y, combo[1].Y); j++ {
if galaxyRows[j] {
d++
} else {
d += expansionFactor
}
}
sum += d
}
return sum
}