--io.input("test") io.input("input") local key = 1 -- part 1 local key = 811589153 -- part 2 local mixed = {} local i = 0 for line in io.lines() do local num = tonumber(line)*key table.insert( mixed, { num = num, pos=i} ) i = i + 1 end local n = #mixed function wrap(x) return x%n end local pos0 = {} local it = 1 local nit = 10 local sums = {} for it = 1,nit do for i, d in ipairs(mixed) do if d.num == 0 then goto nextnum end local ppos = d.num+d.pos local inc = 0 if ppos >= n or ppos <0 then inc = ppos//(n-1) end local newpos = wrap( ppos + inc ) for j, t in ipairs(mixed) do if j==i then goto continue end if newpos < d.pos and t.pos>=newpos and t.pos d.pos and t.pos<=newpos and t.pos>d.pos then t.pos = t.pos - 1 end ::continue:: end d.pos = newpos ::nextnum:: end -- find position of 0 for i, d in ipairs(mixed) do if d.num == 0 then pos0[it] = d.pos break end end print(it, "pos0", pos0[it]) sums[it] = 0 end for j = 1, nit do for i, d in ipairs(mixed) do if d.pos == wrap(pos0[j] + 1000) or d.pos == wrap(pos0[j]+2000) or d.pos == wrap(pos0[j]+3000) then sums[j] = sums[j] + d.num end end end print("part 1", sums[1]) print("part 2", sums[10]) --[[ -- print list table.sort(mixed, function (a,b) return a.pos < b.pos end) local s = "" for i, d in ipairs(mixed) do s = s .. tostring(d.num) if i < #mixed then s = s .. ", " end end print(s) --]]