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