-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.go
186 lines (176 loc) · 4.63 KB
/
day09.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"aoc2021/util"
"fmt"
"image"
"image/color"
"image/color/palette"
"image/gif"
"os"
"sort"
"time"
)
func main() {
lines := util.GetLines("day09\\09.in")
start := time.Now()
day09a(lines)
duration := time.Since(start)
day09b(lines)
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
//makeGif(lines)
}
func day09a(lines []string) {
sum := 0
height := len(lines)
width := len(lines[0])
for i, line := range lines {
for j, r := range line {
if i > 0 && rune(lines[i-1][j]) <= r {
continue
}
if i < height-1 && rune(lines[i+1][j]) <= r {
continue
}
if j < width-1 && rune(lines[i][j+1]) <= r {
continue
}
if j > 0 && rune(lines[i][j-1]) <= r {
continue
}
sum += int(r-'0') + 1
}
}
fmt.Printf("Solution for part A: %d\n", sum)
}
type point struct {
x, y int
}
func day09b(lines []string) {
height := len(lines)
width := len(lines[0])
// mapping keeps track of scanned points and their assigned basin
mapping := make([][]int, height)
for i := range mapping {
mapping[i] = make([]int, width)
}
// running list of basins with their respective points
basins := map[int][]point{}
for i, line := range lines {
for j, r := range line {
if r == '9' {
mapping[i][j] = -1
continue
}
// check previous element
p := point{i, j}
// map to same basin as previous element in row
if j > 0 && rune(lines[i][j-1]) != '9' {
mapping[i][j] = mapping[i][j-1]
basins[mapping[i][j-1]] = append(basins[mapping[i][j-1]], p)
// map to new basin
} else {
mapping[i][j] = len(basins)
basins[len(basins)] = []point{p}
}
// if above is already mapped, take mapping and transform all connected points as well
if i > 0 && rune(lines[i-1][j]) != '9' {
oldMapping := mapping[i][j]
newMapping := mapping[i-1][j]
if oldMapping == newMapping {
continue
}
for _, v := range basins[oldMapping] {
mapping[v.x][v.y] = newMapping
basins[newMapping] = append(basins[newMapping], v)
}
basins[oldMapping] = nil
}
}
}
sizes := []int{}
for _, basin := range basins {
sizes = append(sizes, len(basin))
}
sort.Ints(sizes)
l := len(sizes)
result := sizes[l-1] * sizes[l-2] * sizes[l-3]
fmt.Printf("Solution for part B: %d\n", result)
}
func createPalette(numColors int) []color.Color {
palette := palette.Plan9
return palette
}
func makeGif(lines []string) {
height := len(lines)
width := len(lines[0])
scale := 4
palette := createPalette(2000)
rect := image.Rect(0, 0, width*scale, height*scale)
timings := []int{}
frames := []*image.Paletted{}
// mapping keeps track of scanned points and their assigned basin
mapping := make([][]int, height)
for i := range mapping {
mapping[i] = make([]int, width)
}
// running list of basins with their respective points
basins := map[int][]point{}
for i, line := range lines {
for j, r := range line {
newImg := image.NewPaletted(rect, palette)
if r == '9' {
mapping[i][j] = 0
} else {
// check previous element
p := point{i, j}
// map to same basin as previous element in row
if j > 0 && rune(lines[i][j-1]) != '9' {
mapping[i][j] = mapping[i][j-1]
basins[mapping[i][j-1]] = append(basins[mapping[i][j-1]], p)
// map to new basin
} else {
mapping[i][j] = len(basins)
basins[len(basins)] = []point{p}
}
// if above is already mapped, take mapping and transform all connected points as well
if i > 0 && rune(lines[i-1][j]) != '9' {
oldMapping := mapping[i][j]
newMapping := mapping[i-1][j]
if oldMapping == newMapping {
continue
}
for _, v := range basins[oldMapping] {
mapping[v.x][v.y] = newMapping
basins[newMapping] = append(basins[newMapping], v)
}
basins[oldMapping] = nil
}
}
for m := range lines {
for n := range lines[0] {
tileColor := uint8((mapping[m][n] + 1) % 256)
for t := 0; t < scale; t++ {
for k := 0; k < scale; k++ {
if m < len(lines)-1 && n < len(lines[0])-1 {
newImg.SetColorIndex(n*scale+k, m*scale+t, tileColor)
}
}
}
}
}
frames = append(frames, newImg)
timings = append(timings, 0)
}
}
anim := gif.GIF{Delay: timings, Image: frames, LoopCount: 0}
out, err := os.Create("9b_scanning.gif")
if err != nil {
panic(err)
}
defer out.Close()
fmt.Println(len(lines[0]) * len(lines))
fmt.Println(len(frames))
e := gif.EncodeAll(out, &anim)
fmt.Println(e)
}