-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
225 lines (184 loc) · 3.8 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
)
func input() *os.File {
input, err := os.Open(path.Join("2021", "20", "input.txt"))
if err != nil {
panic(err)
}
return input
}
type pos struct {
x, y int
}
var outsideZero = true
func solve(r io.Reader) {
scanner := bufio.NewScanner(r)
scanner.Scan()
if scanner.Err() != nil {
panic(scanner.Err())
}
algo := strings.ReplaceAll(strings.ReplaceAll(scanner.Text(), ".", "0"), "#", "1")
picture := make(map[pos]rune)
y := 0
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
part := strings.ReplaceAll(strings.ReplaceAll(line, ".", "0"), "#", "1")
for x, p := range part {
picture[pos{x: x, y: y}] = p
}
y += 1
}
if scanner.Err() != nil {
panic(scanner.Err())
}
doSwap := false
switch algo[0] {
case '0':
// 3x3 off stays off, nothing to do
case '1':
switch algo[len(algo)-1] {
case '0':
// 3x3 all on turns off, we need to swap the out of bounds char
doSwap = true
case '1':
panic("infinite pixels lit according to this algo")
}
}
draw(picture)
for i := 0; i < 50; i++ {
//draw(picture)
enhance(algo, picture)
if doSwap {
outsideZero = !outsideZero
}
}
sum := 0
for _, v := range picture {
if v == '1' {
sum += 1
}
}
fmt.Println(sum)
}
func draw(picture map[pos]rune) {
minp, maxp := bounds(picture)
for y := minp.y; y <= maxp.y; y++ {
for x := minp.x; x <= maxp.x; x++ {
val := '.'
v := get(picture, pos{x: x, y: y})
if v == '1' {
val = '#'
}
fmt.Printf("%c", val)
}
fmt.Println()
}
fmt.Println()
}
func bounds(picture map[pos]rune) (pos, pos) {
minp, maxp := pos{x: 9999999, y: 999999}, pos{x: -9999999, y: -9999999}
for p := range picture {
if p.x < minp.x {
minp.x = p.x
}
if p.y < minp.y {
minp.y = p.y
}
if p.x > maxp.x {
maxp.x = p.x
}
if p.y > maxp.y {
maxp.y = p.y
}
}
return minp, maxp
}
func enhance(algo string, picture map[pos]rune) {
updates := make(map[pos]rune)
minp, maxp := bounds(picture)
for y := minp.y - 2; y <= maxp.y+2; y++ {
for x := minp.x - 2; x <= maxp.x+2; x++ {
p := pos{x: x, y: y}
val := combine(
get(picture, pos{
x: p.x - 1,
y: p.y - 1,
}),
get(picture, pos{
x: p.x,
y: p.y - 1,
}),
get(picture, pos{
x: p.x + 1,
y: p.y - 1,
}),
get(picture, pos{
x: p.x - 1,
y: p.y,
}),
get(picture, pos{
x: p.x,
y: p.y,
}),
get(picture, pos{
x: p.x + 1,
y: p.y,
}),
get(picture, pos{
x: p.x - 1,
y: p.y + 1,
}),
get(picture, pos{
x: p.x,
y: p.y + 1,
}),
get(picture, pos{
x: p.x + 1,
y: p.y + 1,
}),
)
intVal, err := strconv.ParseInt(val, 2, 32)
if err != nil {
panic(err)
}
v := rune(algo[intVal])
updates[p] = v
}
}
for p, v := range updates {
picture[p] = v
}
}
func combine(rs ...rune) string {
var s strings.Builder
for _, r := range rs {
s.WriteRune(r)
}
return s.String()
}
func get(picture map[pos]rune, p pos) rune {
v, ok := picture[p]
if ok {
//fmt.Printf("get, %+v = %v\n", p, v)
return v
}
if outsideZero {
return '0'
}
return '1'
}
func main() {
solve(strings.NewReader("..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..###..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#..#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#.....####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.......##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#\n\n#..#.\n#....\n##..#\n..#..\n..###"))
solve(input())
}