day 13, part 1!

This commit is contained in:
sejo 2023-12-13 07:48:46 +01:00
parent 029b023901
commit 7f19c4cb8a
4 changed files with 149 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*/*/input
*/*/output
.*.swp

120
12023/13/13.lua Normal file
View File

@ -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)

15
12023/13/test Normal file
View File

@ -0,0 +1,15 @@
#.##..##.
..#.##.#.
##......#
##......#
..#.##.#.
..##..##.
#.#.##.#.
#...##..#
#....#..#
..##..###
#####.##.
#####.##.
..##..###
#....#..#

13
12023/13/test1 Normal file
View File

@ -0,0 +1,13 @@
##.###..#.###.###
.###.##......#.##
..#..##..##.##.##
..#..##..##.##.##
.###.##......#.##
##.###..#.###.###
..#.#.#..#..###..
.#.#.#.#.#.#####.
.#.#.#.#.#.#####.
..#.#.#..#..###..
######..#.###.###
.###.##......#.##
..#..##..##.##.##