-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (79 loc) · 1.58 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
package main
import (
"fmt"
"github.com/devkvlt/aoc"
)
type Pos struct{ x, y int }
var (
universe = aoc.Lines("input")
positions []Pos
emptyRows []int
emptyCols []int
)
func init() {
for row := 0; row < len(universe); row++ {
for col := 0; col < len(universe[0]); col++ {
if universe[row][col] == '#' {
positions = append(positions, Pos{col, row})
}
}
}
for row := 0; row < len(universe); row++ {
if isEmptyRow(row) {
emptyRows = append(emptyRows, row)
}
}
for col := 0; col < len(universe[0]); col++ {
if isEmptyCol(col) {
emptyCols = append(emptyCols, col)
}
}
}
func isEmptyCol(col int) bool {
for row := 0; row < len(universe); row++ {
if universe[row][col] == '#' {
return false
}
}
return true
}
func isEmptyRow(row int) bool {
for col := 0; col < len(universe[row]); col++ {
if universe[row][col] == '#' {
return false
}
}
return true
}
func dist(p1, p2 Pos, expansion int) int {
x1 := min(p1.x, p2.x)
x2 := max(p1.x, p2.x)
y1 := min(p1.y, p2.y)
y2 := max(p1.y, p2.y)
dx := 0
dy := 0
for _, emptyCol := range emptyCols {
if x1 < emptyCol && emptyCol < x2 {
dx += expansion - 1
}
}
for _, emptyRow := range emptyRows {
if y1 < emptyRow && emptyRow < y2 {
dy += expansion - 1
}
}
return x2 - x1 + dx + y2 - y1 + dy
}
func solveWith(expantion int) {
sum := 0
for i := 0; i < len(positions); i++ {
for j := i + 1; j < len(positions); j++ {
sum += dist(positions[i], positions[j], expantion)
}
}
fmt.Println(sum)
}
func main() {
solveWith(2) // 9686930
solveWith(1000000) // 630728425490
}