-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11-2.go
61 lines (55 loc) · 933 Bytes
/
day11-2.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
package main
import (
"fmt"
"slices"
)
func day11part2(filename string) (string, error) {
u, err := day11ReadMap(filename)
if err != nil {
return "", err
}
day11ExpandMap(u)
day11PrintMap(u)
gal := day11FindGalaxies(u)
pairs := day11AllPairs(gal)
var total int
for _, p := range pairs {
total += day11PathLen(u, p[0], p[1], func(dp day11Pos) int {
return u.weight(dp, 1000000)
})
}
return fmt.Sprint(total), nil
}
func day11ExpandMap(u day11Map) {
for y, vy := range u {
if !slices.Contains(vy, '#') {
for x := range vy {
u[y][x] = '*'
}
}
}
x := 0
for {
if x == len(u[0]) {
break
}
colIsEmpty := true
for _, v := range u {
colIsEmpty = colIsEmpty && v[x] != '#'
}
if !colIsEmpty {
x += 1
continue
}
for y := range u {
u[y][x] = '*'
}
x += 1
}
}
func (u day11Map) weight(p day11Pos, exp int) int {
if u[p.y][p.x] == '*' {
return exp
}
return 1
}