diff --git a/.gitignore b/.gitignore index 0cea664..0dd8231 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ */*/input +*/*/output .*.swp diff --git a/12023/13/13.lua b/12023/13/13.lua new file mode 100644 index 0000000..ef5fb18 --- /dev/null +++ b/12023/13/13.lua @@ -0,0 +1,120 @@ +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) diff --git a/12023/13/test b/12023/13/test new file mode 100644 index 0000000..3b6b5cc --- /dev/null +++ b/12023/13/test @@ -0,0 +1,15 @@ +#.##..##. +..#.##.#. +##......# +##......# +..#.##.#. +..##..##. +#.#.##.#. + +#...##..# +#....#..# +..##..### +#####.##. +#####.##. +..##..### +#....#..# diff --git a/12023/13/test1 b/12023/13/test1 new file mode 100644 index 0000000..4337eef --- /dev/null +++ b/12023/13/test1 @@ -0,0 +1,13 @@ +##.###..#.###.### +.###.##......#.## +..#..##..##.##.## +..#..##..##.##.## +.###.##......#.## +##.###..#.###.### +..#.#.#..#..###.. +.#.#.#.#.#.#####. +.#.#.#.#.#.#####. +..#.#.#..#..###.. +######..#.###.### +.###.##......#.## +..#..##..##.##.## \ No newline at end of file