day 14, parts 1 and 2!
This commit is contained in:
parent
b36105c595
commit
46dde0d268
|
@ -0,0 +1,169 @@
|
||||||
|
io.input("input")
|
||||||
|
|
||||||
|
r = 0
|
||||||
|
cols = {}
|
||||||
|
for line in io.lines() do
|
||||||
|
if r==0 then
|
||||||
|
len = #line
|
||||||
|
for i=1,len do
|
||||||
|
cols[i] = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
r = r+1
|
||||||
|
|
||||||
|
for i=1,len do
|
||||||
|
local c = line:sub(i,i)
|
||||||
|
cols[i][r] = c
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function tilt(t)
|
||||||
|
local cols = {}
|
||||||
|
table.move(t,1,#t,1,cols)
|
||||||
|
for i=1,len do
|
||||||
|
--print("\ncol"..i,table.concat(cols[i]))
|
||||||
|
local o,f=1,1
|
||||||
|
local done = false
|
||||||
|
while o<=len-1 do
|
||||||
|
--print("o",o)
|
||||||
|
local cs,cr = 0,0
|
||||||
|
local curr = cols[i][o]
|
||||||
|
if curr~="." then goto fixed end
|
||||||
|
f=o+1
|
||||||
|
done = false
|
||||||
|
repeat --search for next rock
|
||||||
|
--print("f",f)
|
||||||
|
local nxt = cols[i][f]
|
||||||
|
if nxt=="O" then -- swap?
|
||||||
|
cols[i][o] = "O"
|
||||||
|
cols[i][f] = "."
|
||||||
|
--print("swapped",o,f)
|
||||||
|
elseif nxt=="#" then --skip
|
||||||
|
o=f
|
||||||
|
--print("skipped to",f)
|
||||||
|
end
|
||||||
|
|
||||||
|
f=f+1
|
||||||
|
until f>len or nxt~="."
|
||||||
|
if f>len then break end
|
||||||
|
::fixed::
|
||||||
|
o=o+1
|
||||||
|
end
|
||||||
|
--print("->",table.concat(cols[i]))
|
||||||
|
end
|
||||||
|
return cols
|
||||||
|
end
|
||||||
|
|
||||||
|
function get_load(t)
|
||||||
|
local sum = 0
|
||||||
|
for i=1,len do
|
||||||
|
for j=1,len do
|
||||||
|
local c = t[j][i]
|
||||||
|
if c=="O" then
|
||||||
|
local res = len-i+1
|
||||||
|
sum = sum + res
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return sum
|
||||||
|
end
|
||||||
|
|
||||||
|
function print_map(t)
|
||||||
|
for i=1,len do
|
||||||
|
local s = ""
|
||||||
|
for j=1,len do
|
||||||
|
s = s .. t[i][j]
|
||||||
|
end
|
||||||
|
print(s)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function encode(t)
|
||||||
|
local s = ""
|
||||||
|
for i,c in ipairs(t) do
|
||||||
|
s = s .. table.concat(c)
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
newcols = tilt(cols)
|
||||||
|
sum1 = get_load(newcols)
|
||||||
|
print("part 1",sum1)
|
||||||
|
|
||||||
|
|
||||||
|
trans = {}
|
||||||
|
table.move(cols,1,len,1,trans)
|
||||||
|
history = {}
|
||||||
|
loads = {}
|
||||||
|
repeated = 0
|
||||||
|
exit = 0
|
||||||
|
for n=1,1000000 do
|
||||||
|
--print("<N ^W")
|
||||||
|
newcols = tilt(trans)
|
||||||
|
|
||||||
|
-- WEST
|
||||||
|
--print("<W ^N")
|
||||||
|
for i=1,len do
|
||||||
|
trans[i] = {}
|
||||||
|
for j=1,len do
|
||||||
|
trans[i][j] = newcols[j][i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
newcols = tilt(trans)
|
||||||
|
|
||||||
|
--SOUTH
|
||||||
|
--print("<S ^E")
|
||||||
|
for i=1,len do
|
||||||
|
trans[i] = {}
|
||||||
|
for j=1,len do
|
||||||
|
trans[i][j] = newcols[len-j+1][len-i+1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
newcols = tilt(trans)
|
||||||
|
|
||||||
|
-- EAST
|
||||||
|
--print("<E ^S")
|
||||||
|
for i=1,len do
|
||||||
|
trans[i] = {}
|
||||||
|
for j=1,len do
|
||||||
|
trans[i][j] = newcols[j][i]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
newcols = tilt(trans)
|
||||||
|
|
||||||
|
--NORTH
|
||||||
|
--print("<N ^W")
|
||||||
|
for i=1,len do
|
||||||
|
trans[i] = {}
|
||||||
|
for j=1,len do
|
||||||
|
trans[i][j] = newcols[len-j+1][len-i+1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local curr_load = get_load(trans)
|
||||||
|
--[[
|
||||||
|
if n%10==1 then
|
||||||
|
print("N",n)
|
||||||
|
--print_map(trans)
|
||||||
|
print(curr_load)
|
||||||
|
print()
|
||||||
|
end
|
||||||
|
--]]
|
||||||
|
local s = encode(trans)
|
||||||
|
if not history[s] then
|
||||||
|
history[s] = n
|
||||||
|
loads[n] = curr_load
|
||||||
|
else
|
||||||
|
exit=n
|
||||||
|
repeated = history[s]
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
period = exit-repeated
|
||||||
|
target = 1000000000
|
||||||
|
rtarget = target-repeated
|
||||||
|
modulo = rtarget%period
|
||||||
|
resultn = repeated + modulo
|
||||||
|
--print(repeated, exit, period, rtarget%period)
|
||||||
|
print("part 2", loads[resultn])
|
|
@ -0,0 +1,10 @@
|
||||||
|
O....#....
|
||||||
|
O.OO#....#
|
||||||
|
.....##...
|
||||||
|
OO.#O....O
|
||||||
|
.O.....O#.
|
||||||
|
O.#..O.#.#
|
||||||
|
..O..#O..O
|
||||||
|
.......O..
|
||||||
|
#....###..
|
||||||
|
#OO..#....
|
Loading…
Reference in New Issue