From c8a480818f9bb1c05569ee87e50fb83708526913 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Mon, 13 Dec 2021 09:32:03 +0100 Subject: [PATCH] day 13 --- day13/part1.lua | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ day13/part2.lua | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100755 day13/part1.lua create mode 100755 day13/part2.lua diff --git a/day13/part1.lua b/day13/part1.lua new file mode 100755 index 0000000..c7a6213 --- /dev/null +++ b/day13/part1.lua @@ -0,0 +1,59 @@ +#!/usr/bin/env luajit +require("utils") +require("set") + +local file = io.open(arg[3]) +local coords = {} +local maxr, maxc = 0, 0 +local line = file:read("*l") +while line ~= "" do + local c, r = unpack(split(line, ",")) + r, c = tonumber(r), tonumber(c) + if r > maxr then maxr = r end + if c > maxc then maxc = c end + table.insert(coords, { r, c }) + line = file:read("*l") +end + +local width, height = maxc + 1, maxr + 1 +foreach(coords, function(p) return p[1] * width + p[2] end) +coords = set(coords) + +local folds = {} +line = file:read("*l") +while line ~= nil do + local dir, v = unpack(split(line, "=")) + table.insert(folds, { dir == "fold along y", tonumber(v) }) + line = file:read("*l") +end + + +for i, fold in pairs(folds) do + local dir, v = unpack(fold) + local ncoords = set() + for p in coords:iter() do + local r, c = math.floor(p / width), p % width + if dir and r > v then + r = 2 * v - r + elseif not dir and c > v then + c = 2 * v - c + end + ncoords:add(r * width + c) + end + coords = ncoords + break +end + +for r = 0, height - 1 do + for c = 0, width - 1 do + if coords:has(r * width + c) then + io.write("#") + else + io.write(".") + end + end + io.write("\n") +end +io.write("\n") + +print(coords.size) diff --git a/day13/part2.lua b/day13/part2.lua new file mode 100755 index 0000000..1613e44 --- /dev/null +++ b/day13/part2.lua @@ -0,0 +1,50 @@ +#!/usr/bin/env luajit +require("utils") +require("set") + +local file = io.open(arg[3]) +local coords = {} +local line = file:read("*l") +while line ~= "" do + local c, r = unpack(split(line, ",")) + r, c = tonumber(r), tonumber(c) + table.insert(coords, { r, c }) + line = file:read("*l") +end + +local folds = {} +line = file:read("*l") +while line ~= nil do + local dir, v = unpack(split(line, "=")) + v = tonumber(v) + if dir == "fold along y" then + for i, p in pairs(coords) do + if p[1] > v then p[1] = 2 * v - p[1] end + end + else + for i, p in pairs(coords) do + if p[2] > v then p[2] = 2 * v - p[2] end + end + end + line = file:read("*l") +end + +local width, height = 0, 0 +for i, p in pairs(coords) do + if p[1] + 1 > height then height = p[1] + 1 end + if p[2] + 1 > width then width = p[2] + 1 end +end +foreach(coords, function(p) return p[1] * width + p[2] end) +coords = set(coords) + +for r = 0, height - 1 do + for c = 0, width - 1 do + if coords:has(r * width + c) then + io.write("#") + else + io.write(".") + end + end + io.write("\n") +end +io.write("\n")