-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
67 lines (58 loc) · 1.3 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
package main
import (
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs(input io.Reader, expansion int) int {
lines := aoc.ReaderToStrings(input)
positions, rowsDistance, colsDistance := parse(lines, expansion)
res := 0
for i := 0; i < len(positions); i++ {
for j := i + 1; j < len(positions); j++ {
pos1 := positions[i]
pos2 := positions[j]
res += aoc.Abs(rowsDistance[pos1.Row]-rowsDistance[pos2.Row]) +
aoc.Abs(colsDistance[pos1.Col]-colsDistance[pos2.Col])
}
}
return res
}
func parse(lines []string, expansion int) ([]aoc.Position, []int, []int) {
var positions []aoc.Position
rowsDistance := make([]int, len(lines))
sum := 0
for row, line := range lines {
runes := []rune(line)
empty := true
for col, r := range runes {
if r == '#' {
positions = append(positions, aoc.Position{Row: row, Col: col})
empty = false
}
}
rowsDistance[row] = sum
if empty {
sum += expansion
} else {
sum++
}
}
colsDistance := make([]int, len(lines[0]))
sum = 0
for col := 0; col < len(lines[0]); col++ {
empty := true
for row := 0; row < len(lines); row++ {
if lines[row][col] == '#' {
empty = false
break
}
}
colsDistance[col] = sum
if empty {
sum += expansion
} else {
sum++
}
}
return positions, rowsDistance, colsDistance
}