Code for the Advent of Code event
aoc
advent-of-code
1local grid = require("grid")
2
3grid.init(false)
4
5local ops = {
6 ["on"] = function() return true end,
7 ["off"] = function() return false end,
8 ["toggle"] = function(cell) return not cell end
9}
10
11local function count()
12 return grid.accum(0, function(accum, cell) return accum + (cell and 1 or 0) end)
13end
14
15local input = io.open("input.txt", "r")
16
17for line in input:lines("l") do
18 local op, startx, starty, endx, endy = line:match("(%w+) (%d+),(%d+) through (%d+),(%d+)")
19
20 startx = tonumber(startx)
21 starty = tonumber(starty)
22 endx = tonumber(endx)
23 endy = tonumber(endy)
24
25 grid.apply(ops[op], startx, endx, starty, endy)
26end
27
28input:close()
29
30print(("%d lights are lit"):format(count()))