day 20
This commit is contained in:
parent
f4227e42ec
commit
fadc49c09d
3 changed files with 215 additions and 0 deletions
64
day20/part1.lua
Executable file
64
day20/part1.lua
Executable file
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env luajit
|
||||
require("utils")
|
||||
require("grid")
|
||||
|
||||
local f = io.open(arg[3])
|
||||
local line = f:read("*l")
|
||||
local enhancer = {}
|
||||
for char in chars(line) do
|
||||
if char == "#" then
|
||||
table.insert(enhancer, 1)
|
||||
else
|
||||
table.insert(enhancer, 0)
|
||||
end
|
||||
end
|
||||
f:read("*l")
|
||||
line = f:read("*l")
|
||||
local image = grid(1, 1, #line, #line, 0)
|
||||
local r, c = 0, 0
|
||||
while line ~= nil do
|
||||
r = r + 1
|
||||
c = 0
|
||||
for char in chars(line) do
|
||||
c = c + 1
|
||||
if char == "#" then
|
||||
image:set(r, c, 1)
|
||||
end
|
||||
end
|
||||
line = f:read("*l")
|
||||
end
|
||||
|
||||
function zoom(image, enhancer)
|
||||
local newdefault = enhancer[image.default * (#enhancer - 1) + 1]
|
||||
local newimage = grid(image.top - 1, image.left - 1,
|
||||
image.height + 2, image.width + 2, newdefault)
|
||||
for v, r, c in image:iter() do
|
||||
newimage:set(r, c, v)
|
||||
end
|
||||
return newimage
|
||||
end
|
||||
|
||||
function enhance(image, enhancer)
|
||||
local newimage = zoom(image, enhancer)
|
||||
for _, r, c in newimage:iter() do
|
||||
local index = 0
|
||||
for v, _, _ in image:neigh9(r, c) do
|
||||
index = index * 2 + v
|
||||
end
|
||||
newimage:set(r, c, enhancer[index + 1])
|
||||
end
|
||||
return newimage
|
||||
end
|
||||
|
||||
local function pprint(c)
|
||||
if c == 0
|
||||
then return "."
|
||||
else return "#"
|
||||
end
|
||||
end
|
||||
|
||||
local counter = 0
|
||||
for v in enhance(enhance(image, enhancer), enhancer):iter() do
|
||||
counter = counter + v
|
||||
end
|
||||
print(counter)
|
68
day20/part2.lua
Executable file
68
day20/part2.lua
Executable file
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env luajit
|
||||
require("utils")
|
||||
require("grid")
|
||||
|
||||
local f = io.open(arg[3])
|
||||
local line = f:read("*l")
|
||||
local enhancer = {}
|
||||
for char in chars(line) do
|
||||
if char == "#" then
|
||||
table.insert(enhancer, 1)
|
||||
else
|
||||
table.insert(enhancer, 0)
|
||||
end
|
||||
end
|
||||
f:read("*l")
|
||||
line = f:read("*l")
|
||||
local image = grid(1, 1, #line, #line, 0)
|
||||
local r, c = 0, 0
|
||||
while line ~= nil do
|
||||
r = r + 1
|
||||
c = 0
|
||||
for char in chars(line) do
|
||||
c = c + 1
|
||||
if char == "#" then
|
||||
image:set(r, c, 1)
|
||||
end
|
||||
end
|
||||
line = f:read("*l")
|
||||
end
|
||||
|
||||
function zoom(image, enhancer)
|
||||
local newdefault = enhancer[image.default * (#enhancer - 1) + 1]
|
||||
local newimage = grid(image.top - 1, image.left - 1,
|
||||
image.height + 2, image.width + 2, newdefault)
|
||||
for v, r, c in image:iter() do
|
||||
newimage:set(r, c, v)
|
||||
end
|
||||
return newimage
|
||||
end
|
||||
|
||||
function enhance(image, enhancer)
|
||||
local newimage = zoom(image, enhancer)
|
||||
for _, r, c in newimage:iter() do
|
||||
local index = 0
|
||||
for v, _, _ in image:neigh9(r, c) do
|
||||
index = index * 2 + v
|
||||
end
|
||||
newimage:set(r, c, enhancer[index + 1])
|
||||
end
|
||||
return newimage
|
||||
end
|
||||
|
||||
local function pprint(c)
|
||||
if c == 0
|
||||
then return "."
|
||||
else return "#"
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1, 50 do
|
||||
image = enhance(image, enhancer)
|
||||
end
|
||||
|
||||
local counter = 0
|
||||
for v in image:iter() do
|
||||
counter = counter + v
|
||||
end
|
||||
print(counter)
|
Loading…
Add table
Add a link
Reference in a new issue