cleaned day 3
This commit is contained in:
parent
cee5afd945
commit
f19f7e3686
114
src/3p2.lua
114
src/3p2.lua
|
@ -7,24 +7,13 @@ for s in inp:gmatch("[^\r\n]+") do
|
|||
table.insert(lines, s)
|
||||
end
|
||||
|
||||
local function issym(a, second_iter)
|
||||
if second_iter then
|
||||
return a == '*'
|
||||
end
|
||||
|
||||
if a == '.' then
|
||||
return false
|
||||
end
|
||||
|
||||
if tonumber(a) == nil then
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
function issym(a)
|
||||
return a == '*'
|
||||
end
|
||||
|
||||
tracked = {}
|
||||
local function iter(ux, uy, ym, only_tracked, stracked)
|
||||
changed = 0
|
||||
function iter(ux, uy, ym)
|
||||
if ux > 0 and uy > 0 and not(tonumber(string.sub(lines[ux],uy,uy)) == nil) then
|
||||
local fullnum = ""-- = string.sub(lines[ux],uy,uy)
|
||||
local start_x = 0;
|
||||
|
@ -35,25 +24,11 @@ local function iter(ux, uy, ym, only_tracked, stracked)
|
|||
end
|
||||
end
|
||||
|
||||
for i,_ in pairs(stracked) do
|
||||
if(stracked[i].x == ux and stracked[i].y == start_x) then
|
||||
return 1;
|
||||
end
|
||||
end
|
||||
table.insert(stracked, {x=ux,y=start_x});
|
||||
|
||||
local istracked = false
|
||||
for i,_ in pairs(tracked) do
|
||||
if(tracked[i].x == ux and tracked[i].y == start_x) then
|
||||
istracked = true
|
||||
return 1;
|
||||
end
|
||||
end
|
||||
if istracked and not only_tracked then
|
||||
return 1;
|
||||
end
|
||||
if only_tracked and not istracked then
|
||||
return 1;
|
||||
end
|
||||
table.insert(tracked, {x=ux,y=start_x});
|
||||
|
||||
for i=start_x,string.len(lines[ux]) do
|
||||
|
@ -62,79 +37,40 @@ local function iter(ux, uy, ym, only_tracked, stracked)
|
|||
end
|
||||
fullnum = fullnum .. string.sub(lines[ux],i,i)
|
||||
end
|
||||
--out = out + fullnum
|
||||
changed = changed + 1
|
||||
return tonumber(fullnum);
|
||||
end
|
||||
return 1;
|
||||
end
|
||||
|
||||
function qcheck(x,y)
|
||||
if x == y then
|
||||
return 0;
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
function c_table(tab)
|
||||
t = {}
|
||||
for i,_ in pairs(tab) do
|
||||
table.insert(t, {x=tab[i].x,y=tab[i].y})
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function s_adj(x,y, second_iter)
|
||||
--local ttracked = c_table(tracked)
|
||||
stracked = {}
|
||||
function s_adj(x,y)
|
||||
local out = 1;
|
||||
local changes = 0;
|
||||
local old = 1;
|
||||
changed = 0
|
||||
tracked = {}
|
||||
out = out * iter(x+1,y,-1)
|
||||
out = out * iter(x-1,y,-1)
|
||||
out = out * iter(x, y -1, -1)
|
||||
out = out * iter(x, y+1, -1)
|
||||
|
||||
out = out * iter(x+1, y, -1, second_iter, stracked)
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
out = out * iter(x-1, y, -1, second_iter, stracked)
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
out = out * iter(x, y -1, -1, second_iter, stracked)
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
|
||||
out = out * iter(x, y+1, -1, second_iter, stracked)
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
out = out * iter(x + 1, y + 1, -1, second_iter, stracked)
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
out = out * iter(x - 1, y + 1, -1, second_iter, stracked);
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
out = out * iter(x - 1, y - 1, -1, second_iter, stracked);
|
||||
changes = changes + qcheck(out,old)
|
||||
old = out;
|
||||
out = out * iter(x + 1, y - 1, -1, second_iter, stracked);
|
||||
changes = changes + qcheck(out,old);
|
||||
|
||||
if changes == 2 and second_iter then
|
||||
--print(x .. "," .. y)
|
||||
--print(out)
|
||||
out = out * iter(x + 1, y + 1, -1)
|
||||
out = out * iter(x - 1, y + 1, -1);
|
||||
out = out * iter(x - 1, y - 1, -1);
|
||||
out = out * iter(x + 1, y - 1, -1);
|
||||
if changed == 2 then
|
||||
return out;
|
||||
end
|
||||
--tracked = c_table(ttracked)
|
||||
return 0;
|
||||
end
|
||||
|
||||
total = 0;
|
||||
for ll=1,2 do
|
||||
for i,_ in pairs(lines) do
|
||||
local ind = 1
|
||||
for s in lines[i]:gmatch"." do
|
||||
if(issym(s, ll == 2)) then
|
||||
--print(i.." "..ind)
|
||||
total = total + s_adj(i,ind, ll == 2);
|
||||
end
|
||||
ind = ind + 1
|
||||
for i,_ in pairs(lines) do
|
||||
local ind = 1
|
||||
for s in lines[i]:gmatch"." do
|
||||
if(issym(s)) then
|
||||
total = total + s_adj(i,ind);
|
||||
end
|
||||
ind = ind + 1
|
||||
end
|
||||
end
|
||||
|
||||
print(total)
|
||||
|
|
82
src/3p22.lua
82
src/3p22.lua
|
@ -1,82 +0,0 @@
|
|||
require "llib"
|
||||
|
||||
local inp = llib.io.readfile("3.input")
|
||||
|
||||
lines = {}
|
||||
for s in inp:gmatch("[^\r\n]+") do
|
||||
table.insert(lines, s)
|
||||
end
|
||||
|
||||
function issym(a)
|
||||
return a == '*'
|
||||
end
|
||||
|
||||
tracked = {}
|
||||
changed = 0
|
||||
function iter(ux, uy, ym)
|
||||
if ux > 0 and uy > 0 and not(tonumber(string.sub(lines[ux],uy,uy)) == nil) then
|
||||
local fullnum = ""-- = string.sub(lines[ux],uy,uy)
|
||||
local start_x = 0;
|
||||
for i=uy+ym,0,-1 do
|
||||
if tonumber(string.sub(lines[ux],i,i)) == nil then
|
||||
start_x = i+1;
|
||||
break;
|
||||
end
|
||||
end
|
||||
|
||||
for i,_ in pairs(tracked) do
|
||||
if(tracked[i].x == ux and tracked[i].y == start_x) then
|
||||
return 1;
|
||||
end
|
||||
end
|
||||
table.insert(tracked, {x=ux,y=start_x});
|
||||
|
||||
for i=start_x,string.len(lines[ux]) do
|
||||
if tonumber(string.sub(lines[ux],i,i)) == nil then
|
||||
break;
|
||||
end
|
||||
fullnum = fullnum .. string.sub(lines[ux],i,i)
|
||||
end
|
||||
changed = changed + 1
|
||||
return tonumber(fullnum);
|
||||
end
|
||||
return 1;
|
||||
end
|
||||
|
||||
function qcheck(x,y)
|
||||
if x == y then
|
||||
return 0;
|
||||
end
|
||||
return 1
|
||||
end
|
||||
function s_adj(x,y)
|
||||
local out = 1;
|
||||
changed = 0
|
||||
tracked = {}
|
||||
out = out * iter(x+1,y,-1)
|
||||
out = out * iter(x-1,y,-1)
|
||||
out = out * iter(x, y -1, -1)
|
||||
out = out * iter(x, y+1, -1)
|
||||
|
||||
out = out * iter(x + 1, y + 1, -1)
|
||||
out = out * iter(x - 1, y + 1, -1);
|
||||
out = out * iter(x - 1, y - 1, -1);
|
||||
out = out * iter(x + 1, y - 1, -1);
|
||||
if changed == 2 then
|
||||
return out;
|
||||
end
|
||||
return 0;
|
||||
end
|
||||
|
||||
total = 0;
|
||||
for i,_ in pairs(lines) do
|
||||
local ind = 1
|
||||
for s in lines[i]:gmatch"." do
|
||||
if(issym(s)) then
|
||||
total = total + s_adj(i,ind);
|
||||
end
|
||||
ind = ind + 1
|
||||
end
|
||||
end
|
||||
|
||||
print(total)
|
Loading…
Reference in New Issue