This commit is contained in:
sejo 2023-12-11 06:46:54 +01:00
parent 7b323d202b
commit 8ec222dda7
2 changed files with 84 additions and 0 deletions

74
12023/11/11.lua Normal file
View File

@ -0,0 +1,74 @@
io.input("input")
galaxies = {}
empty_rows = {}
empty_cols = {}
incs = {}
r = 1
for line in io.lines() do
empty_rows[r] = true
for c=1,#line do
local char = line:sub(c,c)
if char=="#" then
local g = table.pack(r, c)
table.insert(galaxies, g)
table.insert(incs, {0,0})
empty_rows[r] = nil
if empty_cols[c] then empty_cols[c] = nil end
--print(r,c)
else -- empty col?
if r==1 then
empty_cols[c] = true
end
end
end
r = r+1
end
print("# galaxies", #galaxies)
expansion = 1000000 -- 2 for part 1, 1000000 for part 2
-- expand empty rows
for r,_ in pairs(empty_rows) do
for i=1,#galaxies do
local gr, gc = table.unpack(galaxies[i])
local ir, ic = table.unpack(incs[i])
if gr>r then ir = ir+expansion-1 end
incs[i] = table.pack(ir,ic)
end
end
-- expand empty cols
for c,_ in pairs(empty_cols) do
for i=1,#galaxies do
local gr, gc = table.unpack(galaxies[i])
local ir, ic = table.unpack(incs[i])
if gc>c then ic = ic+expansion-1 end
incs[i] = table.pack(ir,ic)
end
end
for i=1,#galaxies do
local r,c = table.unpack(galaxies[i])
local ir, ic = table.unpack(incs[i])
galaxies[i] = table.pack(r+ir, c+ic)
--print(r+ir,c+ic)
end
sum1 = 0
for i=1,#galaxies do
local g1r, g1c = table.unpack(galaxies[i])
for j=i+1,#galaxies do
local g2r, g2c = table.unpack(galaxies[j])
local dist = math.abs(g2r-g1r)+math.abs(g2c-g1c)
sum1 = sum1 + dist
-- print(i,j, dist)
end
end
print("result", sum1)

10
12023/11/test Normal file
View File

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