AoC/2021/day20.swift

192 lines
12 KiB
Swift

@main
struct Day20: Puzzle {
func run() {
var image = Image(input)
for i in 0..<50 {
image = image.run(map: map)
if i == 1 {
print("Part 1:", image.countLitPixels())
}
}
print("Part 2:", image.countLitPixels())
}
}
struct Image {
var width: Int
var height: Int
var pixels: [Bool]
var border: Bool
init(_ string: String) {
pixels = []
width = 0
height = 1
border = false
for char in string {
if char.isNewline {
width = 0
height += 1
continue
}
pixels.append(char == "#")
width += 1
}
}
init(width: Int, height: Int, border: Bool) {
self.width = width
self.height = height
self.border = border
self.pixels = Array(repeating: false, count: width * height)
}
subscript(x: Int, y: Int) -> Bool {
get {
guard 0..<width ~= x && 0..<height ~= y else {
return border
}
return pixels[x + width * y]
}
set {
precondition(0..<width ~= x && 0..<height ~= y)
pixels[x + width * y] = newValue
}
}
var borderIndex: Int { border ? 7 : 0 }
func lookupIndex(x: Int, y: Int) -> Int {
var index = 0
for i in -1...1 {
for j in -1...1 {
index = index << 1 | (self[x + j, y + i] ? 1 : 0)
}
}
return index
}
func run(map: [Bool]) -> Image {
var outputImage = Image(width: width + 2, height: height + 2, border: map[borderIndex])
for y in -1...height {
for x in -1...width {
outputImage[x + 1, y + 1] = map[lookupIndex(x: x, y: y)]
}
}
return outputImage
}
func countLitPixels() -> Int {
pixels.lazy.filter { $0 }.count
}
}
let map = "#####.#.###.###.#.#.####.#####.####.#..####.##.####...##......#.##.##.#.#..#.##.###.#.#.#.##.#..#.####.#.#.##.#..........###.##...#.#...#.#.###..#...##.####.##...###.....##..##..##.#......#.#.##.#.#.##.#.......###.##.#.###.##..##.......##....#.##....#..###.######.##...##.##...#.#.##...##.##.#....##.####..#..#..##.##.#.#..#....#######.###.##...#.####..#.#.#.##...##..##.#.#.#.##.#.......###..######..###..##......###..###.#.#.#.......#.....##.#.##..#.##.#.##.####..#.##....##.#.#..#.####.##.#.#.##...#..##.####."
.map { $0 == "#" }
let input = """
.###..###..#.####.#....#..##.###.........#.#.#..#.#...##...#.#........##..#..#.##....#.##.#...###.##
.##......#.#..###.######.#.#.######...#..##...#......###..###..####..####..##..#.##.#.#.#....##..##.
.##.##..###..#...###....#...##..#..######...#.#.##.#..####.#..#..###.######.#.#..#..##...##.#.#.#.#.
..#..##..####...###.#...###.#.####...#####...#####.#.#########.##..####...#....##....##..#.#..#...##
.#.#..#.###....###.#.#..###.#..#####.#.###...####.##.###.#..####.###...#...#.##.#..#..#...##....####
######....#..##.#..#.##.#.#.##.#.#####.#..###.#####..#######..##..#..#........##.##..#...#..##..#.##
.#.##########.##...#.###..##...#....#.##..##.###..#####...#..##.#.....##..####.#.#.##..#...#..#.#.#.
.##..#.#..#..##.###..#.#.##..#.#...########.##..#....##...#..#.####.#.#..####..#.#..##.#.......##..#
##..##...###.##..#.#.#.###.#..####.##.....#.###.##.##......#.......#.#####.#..##.#.##.#..##.###.##.#
..#..###.....##..####.......####.###.#..##.##.#######.####..#.######.###.#.#..###..###.##.###.##..#.
.##.#..#..#.####.#.#.#..#....#...##.....##..##..##.##.#..##...#..#...##.###.##.##........#.#..#..###
....#.######.#..#......####.##....##.#..#..##...#...#...#.##...##..###.......##.....#..#..#..#####..
###..##.....##.##.###.##..#.#.###..####.#.#....#.##.#.....#######.##.....#..#####.##..#..#####.##.##
#.#.#..#.#.#..........##.##......#.###...#.###.##.######..#...#...#.......###...#..#.##...#.#.##..##
##...##.###.#.##..##....#.#.#..##.###.##.#...#.#..##..#..##...#..##...##..#.####..#..##.#.#...##....
...#..###.####.#.####.#.#.#....#.##.##..#####..#####.#.####.##..##.###....###..#....#..##.#.#..#..#.
..#.#.###...###...#####.####..#.#...###.###..#.##....#.#.#.....#..#...#..#.##..####......##.#.###...
#.##.....##...#.#...#####.#.##.##..##.#.##....#.##.....#.###..#..##....###.....#####..####..#..##...
#...##....##..#..####...#...###...##.##..##..#....###.#....#########.##..##.#.#.######.###..####.##.
##.#.##.#.#..#......##.#..#...##..#.#..##.###..##.......##..#.#...#.#..#.#.#....##..######.##...#...
.#..##..#....##..#.##..##...#..##...#....#.#.##..#...##.##.#..#.#.#.##.#..#.#.......#.###..######..#
#..##.#.##.#..##...####.##.#...####...##.###..#..#.#.#..###..##.#....#####.###..#...#..#..####...##.
#.####....######.....#.##..####....###.##....###.####...##.#....####.#..##..#..##..#.##.####.#.....#
...#.#.###...####..#..#.#..#.##...###.#.#.##.#...#.#.#.##.##...#.#####.#....#..##.#...#.##....##....
#..##..#.##..#.#.#..#..###.##.....####..##..##..#######...##.#.....##..#...########..#.#.#.....##...
.#...##...##..##.#..##......#.##......#.###.##.##...##....####.##.#.....#.#.#.#.##.#.##.#..#..##..#.
.#..#..###.#.#.##..#..#...#..##..#.##...##..#...######..##.#.######........###.#..##..##..######.##.
#####..##.####.....###.###.########.###...#########..#.#.###.#..#####..#..###.#####.#.....#..##..##.
........#....###..####.#.###...#.###.........#.......#..#.#..###.##.#...#.###.##..#...##.##.##.#.###
.##....##.#..##.##...####..########.#####..#.#.#...#.##..#.##..######.###.#..##.#..#...###..#.##.###
#..#.##.##..#.#.#...##....#.#..#..###...###..#..#..##.#.....#..#..#.#.####...#.####.#.#.#.#.#.....#.
###..##.##..##.###.#.#....#..........##.#....#.#.##......#.##.#...#####.##....#.#.#....#.###.#.####.
###.....##..#....#..##.##....#..##.##.#...#..#.#####.####.#..##...#...##.###..##.####..##...##.####.
#.#..#.#.#.#..#..#.###..###.##.###.#....#..###.#.###.....##..#.#.##.#....#.#.#.#.#...#.##.######.##.
##....###...#...#.######.#.#.#....#..#.###.#.##..##...#.....##.#...##.#.#####.##.#.##########.####..
...#.#.#..###.#....##.#....##.#..##...#.##....#...#####.#.....##..#.##.##.....#.##.#.#..##.##.#..#.#
###.##.########.##..##.#...###.##.##.#.#...##..###...##.##.##.#..#...#.#.##.....##.....#..##.#.#.###
#.###.#......####..###.....#.##.###..###....###.....#..#.#.##.##....#.#....#.######.#...##...######.
#.#####.###..#.#..........#.######.#####.####.##.##....###..#.###.#.......#....##.#.######..#.###.#.
#.#.##..#...##...#..#.#...#.##..#.####.#.......####.##..#.##..##.#....#.#.....#..#..#...#..#.##.#.#.
####.#.##..##....####.#.##.#.##..#.#.#.#####.####.#.##..##.#.####.#..##..#.###.#...#.##...#.#..###..
#.###...#.#...##.####...###..#..##.#.##.#.#.######...##.##.#.####..#.###..#.##...##..##.#.#.#.####.#
....#.##..##..####......###.#.....#....######.#...##..#..##.###...##.#####..#.##...#...##.#.#####.##
..##...#..##.......#.#.###.#...#####........###...#....#...##.##...###....##.##.#..####....##..##.#.
.#..###.##......##..#...##......##.#.##..##..###.#.#.#.##...#...#.#.....#.#.#.......#..###..####.###
...#..#..##...#..##.##.#####.#.##.#.#...##.####.###...###.#.##.##.##..#.##..#..##.#.###....###..####
#......#...#.#.#.#.#..#...#....###.##.##...######....#.###.##..##.##..#.#..#.####.#.####.......##.#.
#.###.#..####....##.##.###.##.#.#######..##.##.#.#..#...#.#..####.##..#.##.#....#....#.##.#.#.##...#
##...###..##.#.##...##..####.#.#.#####.#.#.##..........#....#...#..##.###....###.#.#.###.###....#.#.
###.#.##..#....#.#.#.##.######..####.#...##.##...###..###..##.........##.##.#.###...##.#####...###.#
..###..#.##..######.#....#...##.#.....#..######.....#.####.#..###..#..##.##...##..##....##..###..##.
#.#.#############.#..##.#.#..#####.#.#.....#....####.#....#..##..###.#...#.##...###.##...##...#..##.
##..##........##.#.##.###..#.#.##..#...#.##.###.#.###########..#.#.#.##..#....#....#.#..#...##...#..
#.#.#...##...#.#.##.#..##.#.####.##.#....##..#..#.....#...######.#...##...####.#....#.........#.#...
##..#.#.##...#....#.##..##.##.###.#.....#.########.##.....#....######..##......#.#...##..##.#.####..
.##.....###..######..#...#......#..#....##.##...#.##...#.#.###...##...#.#.#..#.#..#.#..#.##..###.###
#..#....#...###.#..#########..##.#.......#.###..###...#....#..##.########.#....#.#..###.##.#......#.
###..#..##....####...###.....#....###...#.#..#...#..#.#####...#.####.......#.##.#.....##.##...##.###
..###..#.###..#.##.......###..##...##....#.#.##.#.....###..###.#..####.#####.#..#.#...###.#.####.#..
#.##.#..##.####.#####.#.#.#...######.#.#.#....#.#.#.#...#.##.......###...#.#######...##..###.#.###.#
..##.#....#####.#..##.#..#.#.#.#.#####.#.###...#..##.#.#.####.#.#....##..###.#.##....##..#####.###..
.....#####.#..#.#.....##.#..###.#.##..#..#..#...#.#....#.##.#.#..#...####...#.#..#.##.##.##.#.#...#.
##.##.######....#....#..##.##.##..###..##.#..####.#.##.#...##.#.##.###.######..#####.....#.##.##.###
.#.##.#...#..#######.####.....###.#.#.#...###....#.#.#...#..#.#.....###...###.#...#.#.....##..#...##
.#####..##.####.##.###.##.##..##..####...#.#.###....#.#.....#...#####...###.##.####....#.###.##.####
.#...#....###.########.##..##.##.##...###..#...#..###..#..#.#.###.#...#.#..##..###.#.....#...####.#.
#.#.#..#.##..###.#.###.##.##...##.#.##.#.#.##......#.....###.#.####..##.#.##..#...###...#..##..#...#
.##....######.#.##....#.#.#...#....###...##.##.#.#..##.#....#.##.###.#.####..##...###.#..#.....#.###
...#.####......#.##...#...####......#..#######.#.#.##..##....####.##....##.##..#.##..###...####.#.#.
####..####..##..#...#.#.....#..#..#.......#.##.#.....#.#.##.#..#####...##.#....##........#.........#
.#.#.#......##..#.###.##....#.#..######.##.#.#..#...#...#....###.....###.###....##.#.##..#.#..##.#.#
.##..###....#.#.#.#..#...##.#.##..#.#.##..#.#####.#...#..#...#####.###..##..#.....#.##..##.####.#.#.
..#..##.####....##.##.#...##.##.#..##.###.#..#.###...#...##.#.##.##.#.#..##.##.##..#.....#..####.##.
.#..#..#.#.#.###...###.##.##..##..##....##...#####.#.#...###.##.#.#....##.#..#...#.#..##...#.####...
#####......##....#.##..##.##..#..#.##..#.##.#.######..##..##...#..#.#..#.###..####.#.###..#.#..###.#
.####..#....#.##....#.##.#...#.###..###...##.##.##.#..#..##..#.....#.#########..#...#..####.##....##
####.###..##.#.#.##.#.####.###.###.#...#####.....#.#....#..####.#.#...####.##....#..##.#....#.#.###.
##.##...##...#.#####..#...#.#.##.#.##.#....#..#..####.#.#####.#...#...#..##.##.###..###.#....####..#
.##.####..###..#.####.#.....###....#...#..#...###.....#.####.#....##.####..#.###..#.###...#....##...
#....#...###.#...#.##.#.....#.##.###.....#.##...#...##.##...###.##...##..#####...#....#....#..###.#.
#.#...##.#.#..##..##..###..##...####.#.#..##..##.##..##.#.###.##...###.#.##.##.##..##..###..###..#.#
.##..###...#.####..#####.#.###....###.#..##..####.##..#.##.....###...#...#..##.###.#.#######.....#..
.####....##..##......##.#......#.##..##.##.###..###.......###....####.#.##..#.####..##.#.#.#.##.#..#
##...#####.#.....##..###...###.#...##..#####.#..###...#####..###......#...#.###.##..##...#.###...###
..###.#...###..#.##.##.##.#.##.....##.###..##..##.##..###....###..#.......###.##...##....#..##..##.#
#..#...##.###.#...#.##...#.##.###.##...##.#...##.#.#.#..#....###.#.......#.##.#..###.#.##.##..####.#
##...####.......#..##.#.#.#..#.##....#.##.##.#.####..##....###.####.#.###..#.##..##......#.#.#..####
###...#..##.#.......##..#.#...##...#...##..##.#.#..#.#.#.####.##.##.###..#.##.####..####....####.#..
#..###.##.....###..##.##.###.###.#.#....##.#######....#####.##.#....###.##..#...#..#.##.#.#.#.#..#..
.#.....#..###..##.#..##.##.###.##..#....###...##........#....####..#.##########...#....###.#...##.##
#.....#.#.#####.#.######.#....##..#...#.##.#..##..#####...######.#.####...#.####.###..#.##....##...#
.#.#...#.#..#.##.#..####..#..#.....##.#....#.#######.#.#..#.#.#.#.###...##..####..##.##.##..#.###...
.#..#.##.#..##.#.##....##..###..#........#.##.#.##.#..##.#.###.#.#..#...#..####.##...#.....#.###...#
..#....##..#..#.##.##.#..###......#.#..#.####..##.#.######..#######..##.##.#..##....#.##.....#.###..
.#....#..#....#...#.###...#.#...##..#..##.######..#..###.##..###.#.##...#.#..#.###.#..###.##...#..#.
##...##.####.##..####.#.#....#....#.###.##.###.##.##..#.#.#.....##...#.#..#####.##..#.#.....#.#.####
.....#.#.##..######....#...#.##..#...#..#...#.##..##..##.....#...##..#.#.####......#.#.##..#.#.#..#.
#.##...##.##..###..#....#...####..##....#.#....#..#.#.....######.....##....#..###.......##......#.#.
#.##.....##.#.#...#.#######.###..#..#...##...#.#.####.#########...#..#....#.....##.#.#.###.##..#.#..
.#.......#.##.....#####.#..#...###.##...#.#..#.###.#.#.#.#########.##.###...####...##.##...###.#.#.#
"""