121 lines
2.7 KiB
Lua
121 lines
2.7 KiB
Lua
io.input("input")
|
|
|
|
rows = {}
|
|
cols = {}
|
|
len = 0
|
|
ngroup = 1
|
|
sum1 = 0
|
|
|
|
function reset()
|
|
r = 0
|
|
vindexes = {}
|
|
hindexes = {}
|
|
rows = {}
|
|
end
|
|
|
|
function two_consecutives(t)
|
|
if #t==1 then return nil
|
|
elseif #t==2 then
|
|
if t[1]+1==t[2] then return {table.pack(t[1], t[2])} end
|
|
else
|
|
local consecutives = {}
|
|
for i=1,#t-1 do
|
|
if t[i]+1==t[i+1] then
|
|
table.insert(consecutives, table.pack(t[i], t[i+1]))
|
|
end
|
|
end
|
|
if #consecutives>0 then return consecutives end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
function reflection(indexes, nums, N)
|
|
print(N)
|
|
local consecutives = {}
|
|
for num, ix in pairs(indexes) do
|
|
print(num, table.concat(ix,","))
|
|
if #ix==4 then print("HERE") end
|
|
local cs = two_consecutives(ix)
|
|
if cs then
|
|
for _, c in ipairs(cs) do
|
|
table.insert(consecutives, c)
|
|
end
|
|
end
|
|
end
|
|
print("consecutives")
|
|
if #consecutives>0 then
|
|
for _, c in ipairs(consecutives) do
|
|
local a,b = table.unpack(c)
|
|
local x,y = a,b
|
|
print(a,b)
|
|
local reflected = true
|
|
while reflected and x>=1 and y<=N do
|
|
print("testing",x,y,nums[x],nums[y])
|
|
if nums[x]~=nums[y] then
|
|
reflected = false
|
|
end
|
|
x,y = x-1, y+1
|
|
end
|
|
print(reflected)
|
|
if reflected then return a end
|
|
end
|
|
else
|
|
return nil
|
|
end
|
|
end
|
|
|
|
function end_of_group()
|
|
for j=1,len do
|
|
local vnum = cols[j]
|
|
if not vindexes[vnum] then vindexes[vnum]={} end
|
|
table.insert(vindexes[vnum], j)
|
|
--print(cols[j])
|
|
end
|
|
|
|
print("vindexes")
|
|
local vr = reflection(vindexes, cols, len)
|
|
|
|
print("hindexes")
|
|
local hr = reflection(hindexes, rows, r)
|
|
|
|
if vr then
|
|
sum1 = sum1 + vr
|
|
end
|
|
if hr then
|
|
sum1 = sum1 + 100*hr
|
|
end
|
|
|
|
ngroup = ngroup + 1
|
|
-- reset
|
|
reset()
|
|
print()
|
|
end
|
|
|
|
reset()
|
|
for line in io.lines() do
|
|
if line:find("[%#%.]") then
|
|
if r==0 then
|
|
len = #line
|
|
for i=1,len do cols[i]=0 end
|
|
print("G",ngroup)
|
|
end
|
|
r = r+1
|
|
local hnum = 0
|
|
for i=1,len do
|
|
local char = line:sub(i,i)
|
|
local bit = char=="#" and 1 or 0
|
|
hnum = (hnum << 1) | bit
|
|
cols[i] = (cols[i]<<1) | bit
|
|
end
|
|
table.insert(rows, hnum)
|
|
if not hindexes[hnum] then hindexes[hnum]={} end
|
|
table.insert(hindexes[hnum],r)
|
|
--print(r,line,hnum)
|
|
else
|
|
end_of_group()
|
|
end
|
|
end
|
|
end_of_group()
|
|
|
|
print("part 1", sum1)
|