From b36105c5952150075a968648ed1bb686f14fb44f Mon Sep 17 00:00:00 2001 From: sejo Date: Wed, 13 Dec 2023 14:42:25 +0100 Subject: [PATCH] day 13: cleaned up a bit --- 12023/13/13.lua | 67 +++++++++++++++---------------------------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/12023/13/13.lua b/12023/13/13.lua index 2f3d24b..67a8745 100644 --- a/12023/13/13.lua +++ b/12023/13/13.lua @@ -1,28 +1,19 @@ io.input("input") -rows = {} -cols = {} -len = 0 -ngroup = 1 sum1 = 0 sum2 = 0 +function hex(n) return string.format("%04x",n) end + function reset() + len = 0 r = 0 vindexes = {} hindexes = {} rows = {} + cols = {} end -function difference(a,b) --string based - local diffs = {} - for i=1,#a do - if a:sub(i,i)~=b:sub(i,i) then - table.insert(diffs,i) - end - end - return diffs -end function one_difference(a,b) --numeric local diff = a~b -- is power of 2? @@ -45,7 +36,6 @@ end function new_reflection(nums,N,ignore) local vdiff = list_of_one_diff(nums) - local result = nil for _, d in ipairs(vdiff) do local i,j,diff = table.unpack(d) local newnums = {} @@ -54,7 +44,7 @@ function new_reflection(nums,N,ignore) local newvalue = newnums[j] newnums[i] = newvalue - print(i,j,string.format("%04x",diff),string.format("%04x",nums[i]),string.format("%04x",nums[j]),string.format("%04x",newvalue)) + --print(i,j,hex(diff),hex(nums[i]),hex(nums[j]),hex(newvalue)) -- calculate new indexes local indexes = {} @@ -66,15 +56,13 @@ function new_reflection(nums,N,ignore) print("attempt reflection") local r = reflection(indexes, newnums, N,ignore) - if r then result = r end + if r then return r end end - return result + return nil 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 @@ -89,11 +77,9 @@ end function reflection(indexes, nums, N, ignore) print(N) - local result = nil local consecutives = {} for num, ix in pairs(indexes) do - print(string.format("%04x",num), table.concat(ix,",")) - if #ix==4 then print("HERE") end + print(hex(num), table.concat(ix,",")) local cs = two_consecutives(ix) if cs then for _, c in ipairs(cs) do @@ -101,27 +87,27 @@ function reflection(indexes, nums, N, ignore) end end end - print("consecutives") if #consecutives>0 then + print("list of consecutives") for _, c in ipairs(consecutives) do local a,b = table.unpack(c) + if a==ignore then goto continue end 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]) + print("testing",x,y,hex(nums[x]),hex(nums[y])) if nums[x]~=nums[y] then reflected = false end x,y = x-1, y+1 end print(reflected, a) - if reflected and (not ignore or ignore~=a)then - result = a - end + if reflected then return a end + ::continue:: end end - return result + return nil end function end_of_group() @@ -133,14 +119,15 @@ function end_of_group() print("vindexes") local vr = reflection(vindexes, cols, len) + if vr then + print("VR",vr) + sum1 = sum1 + vr + end print("hindexes") local hr = reflection(hindexes, rows, r) - - if vr then - sum1 = sum1 + vr - end if hr then + print("HR",hr) sum1 = sum1 + 100*hr end @@ -150,24 +137,16 @@ function end_of_group() local nvr = new_reflection(cols, len, vr) if nvr then print("NVR",nvr) + sum2 = sum2 + nvr end print("hdifferences") local nhr = new_reflection(rows, r, hr) if nhr then print("NHR",nhr) - end - - if nvr then - sum2 = sum2 + nvr - end - if nhr then sum2 = sum2 + 100*nhr end - --table.move(vdiff,1,#vdiff,1,tdiff) - - ngroup = ngroup + 1 -- reset reset() print() @@ -178,26 +157,20 @@ for line in io.lines() do if line:find("[%#%.]") then if r==0 then len = #line --- for i=1,len do cols[i]="" end for i=1,len do cols[i]=0 end - print("G",ngroup) end print(line) r = r+1 - --local hnum = line local hnum = 0 for i=1,len do local char = line:sub(i,i) - --cols[i] = cols[i] .. char 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