-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11-1.go
149 lines (132 loc) · 2.57 KB
/
day11-1.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"slices"
)
type day11Map [][]byte
type day11Pos struct {
y, x int
}
func day11part1(filename string) (string, error) {
u, err := day11ReadMap(filename)
if err != nil {
return "", err
}
u = day11ExpandMapVertical(u)
day11ExpandMapHorizontal(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 1
})
}
return fmt.Sprint(total), nil
}
func day11PrintMap(u day11Map) {
for _, vy := range u {
for _, vx := range vy {
fmt.Print(string(vx))
}
fmt.Println()
}
}
func day11ReadMap(filename string) (day11Map, error) {
var u day11Map
if err := forLineError(filename, func(line string) error {
u = append(u, []byte(line))
return nil
}); err != nil {
return u, err
}
return u, nil
}
func day11ExpandMapVertical(u day11Map) day11Map {
var res day11Map
for _, r := range u {
res = append(res, r)
if !slices.Contains(r, '#') {
res = append(res, r)
}
}
return res
}
func day11ExpandMapHorizontal(u day11Map) {
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, v := range u {
u[y] = slices.Insert(v, x, '.')
}
x += 2
}
}
func day11FindGalaxies(u day11Map) []day11Pos {
var res []day11Pos
for y, vy := range u {
for x, vx := range vy {
if vx == '#' {
res = append(res, day11Pos{y, x})
}
}
}
return res
}
func day11AllPairs(g []day11Pos) [][]day11Pos {
var res [][]day11Pos
for i, v := range g {
for j := i + 1; j < len(g); j++ {
var p []day11Pos
p = append(p, v)
p = append(p, g[j])
res = append(res, p)
}
}
return res
}
func (u day11Map) isValidPos(p day11Pos) bool {
return p.y >= 0 && p.y < len(u) && p.x >= 0 && p.x < len(u[p.y])
}
type day11Node struct {
p day11Pos
l int
}
func day11PathLen(u day11Map, from, to day11Pos, weight func(day11Pos) int) int {
rowMove := []int{-1, 0, 0, 1}
colMove := []int{0, -1, 1, 0}
visited := make(map[day11Pos]bool)
visited[from] = true
q := make([]day11Node, 0)
q = append(q, day11Node{p: from, l: 0})
for {
if len(q) == 0 {
break
}
currNode := q[0]
pt := currNode.p
if pt.y == to.y && pt.x == to.x {
return currNode.l
}
q = q[1:]
for i := 0; i < 4; i++ {
p := day11Pos{pt.y + colMove[i], pt.x + rowMove[i]}
if u.isValidPos(p) && !visited[p] {
visited[p] = true
q = append(q, day11Node{p, currNode.l + weight(pt)})
}
}
}
return -1
}