-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.go
209 lines (191 loc) · 4.17 KB
/
day20.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"fmt"
"math"
"os"
"strings"
)
type point struct {
x, y int
}
type image map[point]*bool
type enhancementAlgorithm map[int]bool
func main() {
input, err := os.ReadFile("input.txt")
if err != nil {
os.Exit(1)
}
fmt.Printf("Part 1: %v\n", SolveDay20Part1(string(input)))
fmt.Printf("Part 2: %v\n", SolveDay20Part2(string(input)))
}
//SolveDay20Part1 returns the number of lit pixels after enhance 2 times
func SolveDay20Part1(input string) int {
algorithm, img := parseInput(input)
img = img.increase(3)
img = img.enhance(algorithm)
img = img.enhance(algorithm)
img = img.decrease(1)
return img.countLit()
}
//SolveDay20Part2 returns the number of lit pixels after enhance 50 times
func SolveDay20Part2(input string) int {
algorithm, img := parseInput(input)
for i := 0; i < 50; i++ {
if i%2 == 0 {
img = img.increase(4)
}
img = img.enhance(algorithm)
img = img.decrease(1)
}
return img.countLit()
}
//enhance apply the enhancementAlgorithm to an image and returns the new image
func (i image) enhance(algorithm enhancementAlgorithm) image {
newImage := make(image)
for pixel := range i {
x, y := pixel.x, pixel.y
p := make([]bool, 9)
p[0] = i.getPixel(point{x: x - 1, y: y - 1})
p[1] = i.getPixel(point{x: x, y: y - 1})
p[2] = i.getPixel(point{x: x + 1, y: y - 1})
p[3] = i.getPixel(point{x: x - 1, y: y})
p[4] = i.getPixel(point{x: x, y: y})
p[5] = i.getPixel(point{x: x + 1, y: y})
p[6] = i.getPixel(point{x: x - 1, y: y + 1})
p[7] = i.getPixel(point{x: x, y: y + 1})
p[8] = i.getPixel(point{x: x + 1, y: y + 1})
var bits int
for b := range p {
bits = bits << 1
if p[b] {
bits++
}
}
newPixel := algorithm[bits]
newImage[pixel] = &newPixel
}
return newImage
}
//getPixel returns the status of a pixel (default false)
func (i image) getPixel(p point) bool {
if i[p] != nil {
return *i[p]
}
return false
}
//increase the image by x pixel on each site
func (i image) increase(by int) image {
minX, minY, maxX, maxY := i.getSize()
for y := minY - by; y <= maxY+by; y++ {
for x := minX - by; x <= maxX+by; x++ {
if i[point{x: x, y: y}] == nil {
f := false
i[point{x: x, y: y}] = &f
}
}
}
return i
}
//decrease the image by x pixel on each site
func (i image) decrease(by int) image {
minX, minY, maxX, maxY := i.getSize()
newImg := make(image)
for y := minY + by; y <= maxY-by; y++ {
for x := minX + by; x <= maxX-by; x++ {
newImg[point{x: x, y: y}] = i[point{x: x, y: y}]
}
}
return newImg
}
//getSize returns the size of the image (minX, minY, maxX, maxY)
func (i image) getSize() (int, int, int, int) {
var maxX, maxY int
minX := math.MaxInt
minY := math.MaxInt
for p := range i {
if p.x > maxX {
maxX = p.x
}
if p.y > maxY {
maxY = p.y
}
if p.x < minX {
minX = p.x
}
if p.y < minY {
minY = p.y
}
}
return minX, minY, maxX, maxY
}
//print the image for debugging
func (i image) print() {
var maxX, maxY int
minX := math.MaxInt
minY := math.MaxInt
for p := range i {
if p.x > maxX {
maxX = p.x
}
if p.y > maxY {
maxY = p.y
}
if p.x < minX {
minX = p.x
}
if p.y < minY {
minY = p.y
}
}
for y := minY; y <= maxY; y++ {
for x := minX; x <= maxX; x++ {
if i[point{x: x, y: y}] != nil && *i[point{x: x, y: y}] {
fmt.Print("#")
} else {
fmt.Print(".")
}
}
fmt.Println()
}
}
//countLit returns the number of lit pixels in the image
func (i image) countLit() int {
var count int
for p := range i {
if i[p] != nil && *i[p] {
count++
}
}
return count
}
//parseInput returns the enhancementAlgorithm and image for an input
func parseInput(input string) (enhancementAlgorithm, image) {
in := strings.Split(input, "\n\n")
if len(in) != 2 {
panic("wrong input")
}
enhancement := make(enhancementAlgorithm)
for i, r := range in[0] {
switch r {
case '.':
enhancement[i] = false
case '#':
enhancement[i] = true
}
}
img := make(image)
imgLines := strings.Split(in[1], "\n")
for y := range imgLines {
for x, r := range imgLines[y] {
t := true
f := false
switch r {
case '.':
img[point{x: x, y: y}] = &f
case '#':
img[point{x: x, y: y}] = &t
}
}
}
return enhancement, img
}