adventofcode/2021/day20/main.go

56 lines
1.2 KiB
Go

package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
enhancement, inputImage := inp[0], h.StringSliceToCoordByteMap(inp[2:])
fmt.Println("# Part 1")
run(inputImage, enhancement, 2)
fmt.Println()
fmt.Println("# Part 2")
run(inputImage, enhancement, 50)
}
func run(image h.CoordByteMap, enhancement string, iters int) {
bg := byte('.')
for i := 0; i < iters; i++ {
image = enhance(image, enhancement, bg)
if bg == '#' {
bg = enhancement[511]
} else {
bg = enhancement[0]
}
}
fmt.Println("Lit pixels:", image.Count('#'))
}
func enhance(image h.CoordByteMap, enhancement string, bg byte) h.CoordByteMap {
pxToInt := func(c h.Coordinate) int {
var num int
for bldY := c.Y - 1; bldY <= c.Y+1; bldY++ {
for bldX := c.X - 1; bldX <= c.X+1; bldX++ {
num = num << 1
v := image.Opt(h.Coordinate{X: bldX, Y: bldY}, bg)
if v == '#' {
num |= 1
}
}
}
return num
}
next := h.NewCoordByteMap()
for y := image.TLY - 1; y <= image.BRY+1; y++ {
for x := image.TLX - 1; x <= image.BRX+1; x++ {
c := h.Coordinate{X: x, Y: y}
nv := enhancement[pxToInt(c)]
next.Put(c, nv)
}
}
return next
}