Compare commits
2 Commits
7b323d202b
...
ee8bf23e87
Author | SHA1 | Date |
---|---|---|
sejo | ee8bf23e87 | |
sejo | 8ec222dda7 |
|
@ -0,0 +1,9 @@
|
|||
...........
|
||||
.S-------7.
|
||||
.|F-----7|.
|
||||
.||.....||.
|
||||
.||.....||.
|
||||
.|L-7.F-J|.
|
||||
.|..|.|..|.
|
||||
.L--J.L--J.
|
||||
...........
|
|
@ -0,0 +1,10 @@
|
|||
.F----7F7F7F7F-7....
|
||||
.|F--7||||||||FJ....
|
||||
.||.FJ||||||||L7....
|
||||
FJL7L7LJLJ||LJ.L-7..
|
||||
L--J.L7...LJS7F-7L7.
|
||||
....F-J..F7FJ|L7L7L7
|
||||
....L7.F7||L7|.L7L7|
|
||||
.....|FJLJ|FJ|F7|.LJ
|
||||
....FJL-7.||.||||...
|
||||
....L---J.LJ.LJLJ...
|
|
@ -0,0 +1,10 @@
|
|||
FF7FSF7F7F7F7F7F---7
|
||||
L|LJ||||||||||||F--J
|
||||
FL-7LJLJ||||||LJL-77
|
||||
F--JF--7||LJLJ7F7FJ-
|
||||
L---JF-JLJ.||-FJLJJ7
|
||||
|F|F-JF---7F7-L7L|7|
|
||||
|FFJF7L7F-JF7|JL---7
|
||||
7-L-JL7||F7|L7F-7F7|
|
||||
L.L7LFJ|||||FJL7||LJ
|
||||
L7JLJL-JLJLJL--JLJ.L
|
|
@ -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)
|
|
@ -0,0 +1,10 @@
|
|||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
Loading…
Reference in New Issue