Day 20 in Lua
4 files changed, 233 insertions(+), 0 deletions(-) M README.md A day20/Makefile A day20/day20.lua A day20/input.txt
M README.md => README.md +2 -0
@@ 23,6 23,7 @@ of the solutions 😄 * [Day 16][day16]: OCaml * [Day 17][day17]: Raku * [Day 18][day18]: Clojure * [Day 20][day20]: Lua [day1]: https://git.sr.ht/~srpablo/advent_of_code_2021/tree/main/item/day1/day1.factor @@ [day2]: https://git.sr.ht/~srpablo/advent_of_code_2021/tree/main/item/day2/day2.sml 42,4 43,5 @@ of the solutions 😄 [day16]: https://git.sr.ht/~srpablo/advent_of_code_2021/tree/main/item/day16/day16.ml [day17]: https://git.sr.ht/~srpablo/advent_of_code_2021/tree/main/item/day17/day17.raku [day18]: https://git.sr.ht/~srpablo/advent_of_code_2021/tree/main/item/day18/day18/src/day18/core.clj [day20]: https://git.sr.ht/~srpablo/advent_of_code_2021/tree/main/item/day20/day20.lua [aoc]: https://adventofcode.com/2021
A day20/Makefile => day20/Makefile +3 -0
A day20/day20.lua => day20/day20.lua +126 -0
@@ 0,0 1,126 @@ -- Game of life! -- -- To support infinite sequences, we'll represent the boards as tables (dictionaries) -- instead of tables (list of lists) ;) local function key(i, j) return tostring(i) .. "," .. tostring(j) end local function tableLength(t) local count = 0 for _ in pairs(t) do count = count + 1 end return count end local function parseInput(filename) local i = 1 local imageEnchancementLookup = {} local theField = {} for line in io.lines(filename) do if i == 1 then local imageEnchancementAlgorithm = line for j = 1, #imageEnchancementAlgorithm do local char = imageEnchancementAlgorithm:sub(j, j) imageEnchancementLookup[j] = (char == "#") end goto continue end if i == 2 then goto continue end for j = 1, #line do local c = line:sub(j,j) local rowIndex = i - 3 local colIndex = j - 1 if c == "#" then local thisKey = key(rowIndex, colIndex) theField[thisKey] = {rowIndex, colIndex} end end ::continue:: i = i + 1 end return imageEnchancementLookup, theField end local function getMinAndMax(field) local xMin, xMax, yMin, yMax = 10000, 0, 10000, 0 for _, table in pairs(field) do local x, y = table[1], table[2] if x < xMin then xMin = x elseif x > xMax then xMax = x elseif y < yMin then yMin = y elseif y > yMax then yMax = y end end return { xMin = (xMin - 1), xMax = (xMax + 1), yMin = (yMin - 1), yMax = (yMax + 1) } end local function digitForNeighbors(x, y, board, bounds, voidIsOn) local offsets = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}} local digit = 0 local powers = 1 -- Loop backwards to build up the binary digit. for i = #offsets, 1, -1 do local offset = offsets[i] local xOff = x + offset[1] local yOff = y + offset[2] local inXBounds = bounds.xMin < xOff and xOff < bounds.xMax local inYBounds = bounds.yMin < yOff and yOff < bounds.yMax local inBounds = inXBounds and inYBounds if (inBounds and board[key(xOff, yOff)]) or (not inBounds and voidIsOn) then digit = digit + powers end powers = powers * 2 end -- Need to increment it since we index on it, and Lua -- indexes start at 1 return (digit + 1) end local function runCycle(lookup, field, voidIsOn) local newField = {} local bounds = getMinAndMax(field) for x = bounds.xMin, bounds.xMax do for y = bounds.yMin, bounds.yMax do local lookupDigit = digitForNeighbors(x, y, field, bounds, voidIsOn) if lookup[lookupDigit] then newField[key(x,y)] = {x, y} end end end return newField end -- Rack'em local lookup, field = parseInput("input.txt") for i = 1, 50 do local voidIsOn = false if i % 2 == 0 and lookup[1] then voidIsOn = true end field = runCycle(lookup, field, voidIsOn) if i == 2 then print("Part 1: " .. tableLength(field)) end end print("Part 2: " .. tableLength(field))
A day20/input.txt => day20/input.txt +102 -0
@@ 0,0 1,102 @@ ###..##....#....##...#.#.#...######.#.#.#.###.####.....#.##...###......###.......#.#.#...##.##...#.#####..#.#.#.###..####..#.#..#..#...###.####..#.###.#...#.#.#.#...#.....#####..#.##.#.#.#.#...####.#.#....##..######..###.####......#..#..#####.##.#.#.#.#.####...###.####..#.#.#..##...#..#..#..##..##..###..#..#####.##...#.#.....##.#..##.#...####.#...#..##.#.#....#..#....##.#.#.###.#..#.###.###.######.##.######...#..##..##.#.###..#.###..#..#.########.###.#.#.#.#..##.#.#.###.#.###.#.#.###...#..#.##....##.##.##.. .....#...#.####...#####..##..#.##.######...###.##.#.#.#...#####.#.#........#.######.#.#.##..####..#. ##.#...##...######...##..####..####..#...##.#..##.#.##.#.....##.##.#.##...#####..#..#....#.###...### ..#.##...#..#.....#..##.##...###..##..##....##...#..####.#....#.#.##.#.#.#..#.#.##..##.##.###.##..## ......##.##.#.#.#....#..##.###.##.#.###..###.###....#...##.####.#.####.###..#..##.#.###....#.#..#.#. #.#...##.######.#..###..#.....##....####.#..###.##.#...#.#.#.#..####..#.#....#.#........#.#..#...##. .#.#####..##..###########.#..##.#..#####.#.#.###..#..##.##.#.#...#..##.#####...#.......#....#....### ..#..#.##.#..##...##.#.#...###.#...#..##.##..####.#.#........##.#....#..###...##.#.#####.#...#..#.#. .###...###.#..#.##.#.##..#.#####.#.#...#.###.#...###.#...#....##.#..#.#####..##.#.###.#.....#..#.#.. ###...##..###.##...#..##...##.##..##.#..#.####.#.#.##..##.#..#.......##.#....####.#.####..###..#...# .#.....#.#.####..#.##.##..#######.##.###.#...#..#.##..#....#..####.#.#....#.#..#.#....##..#####..#.# .#.#..#..##..#..##.#........#...###..###.#...#.#...##..###...#.####..#####.####..#..##.##.####...### ##.#....##.#.#..#.##.##.#.#...#.##.#.#....##.#.##.#.#####..#.#....#.###..#.#....###......####.###### ##..###.#.#.##.###...#.#.......#.#.##..##..####...#.###.##.##.#.#..###.##..##.#......##.###.....##.. .#########..##..##.##.#.##..#.#######.#.#..######..###...##....####.#......###.####.#.#..####.###..# ....###.####..#.#.....#...#......##.##.###.#.#.###..#.###..#..###....#.##.#.##.#.#..###..#..##..#.## #..#.#..#.#..##..##.####....##..#..#.####.#.#.#..##.##...#.###.##...###.#####..####.####..#..#####.# ..#.......##....#.##...#.##...####..##...#.##..#.....#.######..##.#.#..##.##..##..#...#####..#...#.. ....#.#.###.##..#..###..#.#..#.###.##....#..#.##.##..##.#.####..####....##..####.#.##.#.#.######..## #.##.####.#####..####..##..###.########.#.#.#...##..#..#.#..#..#..###.##...##....#.#.##.#####....##. #.#..#.#.#..#.#.#.....#..#..#.####.##########.###.#....##.#.###.#.##.....#....#..##.###.###.#...###. .##.###.######.....#####.......###.#######.#....#.##..##...#...###.##.##.#..####....##.#..#.##.#.#.# .####..#.##.#.#...#.....###.....##..###.#.#.#..####..##..#.#######.#.#.##.######.##...###...#.#..#.. .##.#..#.#.##..#..##.#..#...#...##...###...#.#.####.#...###...#.#.....####......#.######..####.#.### ##..#.###..###....##.####.###..#..#.##.####.#.#.##..###..#.#.#..#..######...####...#..##.##..##..#.# ##.#...#######.####.#.##.##.#.##...#.#...#.##.####.#.##.#..#.###......##.#.#.#...###.#...##..##.###. ..###.#.##.#.#....##.#.###....##.#....##.##.#...##.#.#...###..#.#........#####..#####...##..#.#...#. ##.....#...#.#...#....####..#.####.#.#......##....#...####..#.##..####..#.#.####.##.##.#.##.#####.#. ....######....#..###.###..#..#####.###....#.##...#...#.#..#.....#.#####.##...##..###.##.#..#.####.## .##..###.#.#.#####...###.##.###.#.##..##..#..#.##.##....##..##...##.####..#.####...##.##.##...#..#.. #.#..#####.#.#####...#.#..#...#.#.###...##.#.######....##....##.#..#.##.##.###..##.##.#####.#...##.# #..###.###.#.#####.#..#......#.#.##.###..##.#...###..####.#..#####...##.####.#...##.#.###.##...###.# ###.###..####.#.#....#...#..##...#.###..#..#####.###.#.#.#..#..#.##...###.#.#.####.#.###..##..##.##. .#.#..#.#.#..#....##.#....#...#..#.#....###.#.#......##..##..#..###......##.####.#########....##...# #.....#.....##..##..###.##.##..####....#..#.##.##.#..##.##.#..####..#....####.#.#...##.#.#.###.#.##. .#..##.###.##..#..##..#.#..####......#..###.##..##...##..#....###.#.#.....###.##.##..##....#.#.##... .#...##.....#####.###..#...#.#.#...........#.##.#.......#.####...###.##..#..##.#...#..#########...#. ########.####.##.......#.##.###.##.#.####.#..##.##.#.###..##.####..#..##.#..#...#.###......##.##.### .#.##...#.#######.###.#.######.#..###.#..##.#..##.###..##.#.##....####...#..##..#....#.#.##.#.##.#.# ...#.#.#..##..##.#..####....##....#..#.#.#.##.#..#.#..#...####......##.....#.#.#..##...#..#.#..#...# .#.#####.#.#.#####.#####....##..##....####..#.###.#...###.#.##.#.#..##.#......#.###..#####....#..##. .#.###..####..###.###...#...#.###.#.##.##.##...#.....##.##..##..######.#.##.#.######..#.#.#.......#. #.....##.#..#.....#.##.##..#.#.###..##.##.##.#..#.#...#.#...##.....#.##.#.####.#.##.##.#.##.#..#.#.. .#.#...#.##.##.#.#...#......##.#....#####.###.###.######..#.##.#..#.#..####..####.###..#.#....#..#.# .##.#..####.#...####..#..##.##.###..#..######..##.####....#..#.##..###..##.##.......#.##..##..###..# #....##.#.#.#...#.#..#...##.#.........##.#######..####.##..##.#####.##.#....##..#.####..###.####.#.. #####...#.###.####.##....##.####.#...###.##...#...#####..#...#.....###..#.#.#.##..#....#.#.#.#..###. ...#.#..####..##..##..#.###.......#..#.#..#...##....#...##.##.####..#...#.#...##.#..#.#.##...#.#.### ##.#.#...####.###..##...#...##.####.#..#..#.###.##..##..######.##.#.##.#.##.##.#...##...###.#.#.#.#. ....#....#.#.#.....#..###...#.####.#..#.###..#######..#.#...####...#...##...#.#..##.#.#..##.####.### ..##.#..#.....#.....#.##.#.#.##.#####.##....###.#.##.....##.#...###.#.#..#....#..#####.#.##.##.#.##. ###..##.#.#..##.##.##..##.....###.#.#..#.##....#...##..###.##.#...#.#.#.####.#..####...#.##.###..#.. #....#.##..##.#..##...#.#.##..#.#..#..##.#....#.##....####..........##.##.....######.##...#.......#. #..#.##.###..##.#..#..#....#.####.#.....###.###....##...##..#..##.#####..#.#..########..#...####.#.# ###.#..##....###.#..##..###.##.#..#....####....#.#.....#.#.#######.###.#.#.##.######....#.#.#....##. ...#.##.###.##...##.####..#.###.##..#.#.###..###..#..#....###.###....##.....#..##..#..#............. ..#...###...#####.###.###.##.....###....#....###.###...#.#.#.#.##.#..#..####.#....#...#.#..#.##.#### #####....#.....#...###..#.#....#..#.###..#.####........###.##.#........###.#.##...##..#..#.##.#....# .####.#..##.#.##.######.......#.#.#...#..#.#..#....#.##..#...#.#######....####.#..##.#..##..##.###.. .#.##...#.##...#..#..#..###..#.#.......#.##..#.#..#.###..##.....#.#.#######.....##..#.##.#.###.#.##. ..##.#...##.###.##.#.#......##..##...##...###.#.##..#...#.#.####...#..##.#..#.##..##....#.##....###. #..############.#.#..#.##.##..#...#.......##.##..#.#..#..###....####...###.#.###..####.#.#.#..#..##. .#..#...#.##.##.#.##..#.##.###.....#..##...#.##......#.....##.#.##.##....####...#....##..##.####.##. #.###...##..#.##.##.######.#######.##..####.#..#.###.#.##...#.......#.######....###.#....##.#.##.#.. #.##.........###....####.###.#.###.#....#.##.##.#.###...#.###..#.##.####.#######.###.#.##.##....#..# ##..#.##...##.#..###.#.##.#..##...#....###.###.#..#.##....#.###...#.#.#...#####..#....#..####.#.###. #.####.#..#.####.###.######.##..#.#...#.####..#...###.#...#.####.....###..#..#..#..###.#.##.###.#..# #.##.#.######.##.#.#.#.#.#....#...#...##.####.###.##.#..###....#........#...###.#.#..#.##..####..##. ...####.#.#.#.###........#####.###...#..#.#.......#....#..##.#.#.##.###...######..###.#...#.##.##.## .#..#.####.#..#.#....##..###.##..##.####..###....#.#..######...######.##.#.#.#.##.#....#.......##..# ...###..#####......#.###..#...#...#.##.###.##...#.....###########....##..##...#.#...#.....##.#..#.## #.........#.##.#.#....#......#..#.##.#..####...#..#.###.##.####...#..##.#.##.#.#.##.#.##...#.#.#.### .#.#..#....#......#.#.#.......#...#..#.#..##..#####.#.#.#####.....#..#####.#..#..##.#.#####..#.#...# ...#.#..#.##...#.#..#.##.##.##..#..#####.....#.###...##.#..#..#.....###.#.##..#.####.###.#..#..###.# .#.########.#.###..#....#.#####...######.#####.#..#.#####.#....#.....###..#..###.###.#..####..#....# ####.#.#.####.#.#..###.##.....#.####..##.#..#....#.####.###.##....#####.#.##.#.##.......#.#.#..#.#.. ######..##.#...#.#.#####..#....#..#...###..#.#..##....#..##.....######..###.##.#.#.####.#.#...#..### .##......########.#..#.####.....####.#.###....##..##..##..#.##.#..#.##.###....##.#.....#.#.#.##...#. ###.#...#...##...#.####.###.###.#..#..#..#.###...#..####..####.#.#...##..#############.#..#......#.# #.##.#...#.######.####...#.#.#.#.#.######.#..####..#....##..###..#..#.###..#....##.##.#...####.#.#.# .....#.###.#...###..##.###.#.###.#....###....#..##.#.##..##.#####..##.######.#..##.##..#.#.##.#..#.. ##..##.......#..####..##.#..#..#.##.#.#...#.....#.###...#.#....#.....#...#.#.#...##..#####.#.#.....# ..###...#.....##..#.#.##..#...######...#...##.#..#.####...#.#......##.####..#..#####.#.###...#..##.. ###.#.#.....#..#.#..##.###...##.###..#...###.#.#..####...##..#.###..###.#...##.####.......##.##..... ##.#.#.#.#.......####.##..###.###..######..##.#...#..###..##.#..###.##....###..###.##....####.##..## .#..#####..#..###......##.#..##.......###.#.#......##.##.##.####.#..#####.#####.#.....#..#..#.#..#.# ...##.#.####.#...##.####..##.######.####.###...##...#.#..#####.....##.##...#.########.#..##.#..#.### #.##..#.##........#.#.##..#####..##.....##.#.###..######.###.#.#.....##..#........#....#.##.##...... ##..#...#.####.#..####..###...#.#..#.....##.##.###...#.##.....#....#....####.#..####...#.###..#.#.#. ###.#.###...#.######....###..#...#.#..#....####.#.####..#.#.#.##.###..#..##..#.###.##.##....####...# #.##.#.#...##.....##.#..##....######....######.#.##.#...##.#.......#...##..##..##..###..#....####.#. ###.###..#.##.##.....#..#.#..###..###....#...####.#######.##.##.#..###.###.#.###.#.........####..##. ..###...####..#.##..####.##...##...#.####..#...#..#..####.##.#..#.....##..####.......#.#...##..##..# ###..#.#.##.#.#..###....##.###..##.#####....####...###.#.#..#.##.#...#..#######..#.#.#.##.###....### #....###...#....#...###.#.##.#....###...#####...#.#......#.##.##.#....###...#.#.#.#.#.#..###..#.#### ..###...#.######.#..#..##.#....#####..####..###.#...####..#.#.#....#..#..###.#.##.....######.#..#..# ##...#.##.#.##.###.#.#.##.###..###.####.####...#.###.####.##......#.#..#.###..#......#.#.....####### .#.#..#..#..#.##...####...#...#.....##.#.##..###.##.##.#.#.##..#.##...###...###...##..####.#.###.#.# ..#...##.#.#.#.#..#####.####.#...##...#.#..####.....#.##..#.....#.#####.##...###..###....#.##...#..# ##...#...##.###.#.###...#..##..##.##..#.....##.###..#...##....###.#.##.#.#...###....#.##.###.####... ###...##.###.##........####...#.......##..###.##..#..####....###.#.###...#...###.#.#.#.###........#.