Compare commits

...

3 Commits

Author SHA1 Message Date
sejo b36105c595 day 13: cleaned up a bit 2023-12-13 14:42:25 +01:00
sejo cb6abe73cb day 13, part 2! 2023-12-13 14:21:03 +01:00
sejo 7f19c4cb8a day 13, part 1! 2023-12-13 07:48:46 +01:00
4 changed files with 210 additions and 0 deletions

1
.gitignore vendored
View File

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

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

@ -0,0 +1,181 @@
io.input("input")
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 one_difference(a,b) --numeric
local diff = a~b
-- is power of 2?
local log = math.log(diff,2)
return (diff>0 and log==math.floor(log)) and diff or nil
end
function list_of_one_diff(t)
local diffs = {}
for i=1,#t-1 do
for j=i+1,#t do
local diff = one_difference(t[i],t[j])
if diff then
table.insert(diffs,table.pack(i,j,diff))
end
end
end
return diffs
end
function new_reflection(nums,N,ignore)
local vdiff = list_of_one_diff(nums)
for _, d in ipairs(vdiff) do
local i,j,diff = table.unpack(d)
local newnums = {}
table.move(nums,1,#nums,1,newnums)
-- make them equal
local newvalue = newnums[j]
newnums[i] = newvalue
--print(i,j,hex(diff),hex(nums[i]),hex(nums[j]),hex(newvalue))
-- calculate new indexes
local indexes = {}
for j=1,N do
local num = newnums[j]
if not indexes[num] then indexes[num]={} end
table.insert(indexes[num], j)
end
print("attempt reflection")
local r = reflection(indexes, newnums, N,ignore)
if r then return r end
end
return nil
end
function two_consecutives(t)
if #t==1 then return nil
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, ignore)
print(N)
local consecutives = {}
for num, ix in pairs(indexes) do
print(hex(num), table.concat(ix,","))
local cs = two_consecutives(ix)
if cs then
for _, c in ipairs(cs) do
table.insert(consecutives, c)
end
end
end
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,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 then return a end
::continue::
end
end
return nil
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)
end
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 hr then
print("HR",hr)
sum1 = sum1 + 100*hr
end
-- part 2
print("\nPART 2")
print("vdifferences")
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)
sum2 = sum2 + 100*nhr
end
-- 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
end
print(line)
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)
else
end_of_group()
end
end
end_of_group()
print("part 1", sum1)
print("part 2", sum2)

15
12023/13/test Normal file
View File

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

13
12023/13/test1 Normal file
View File

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