Last active
December 7, 2017 04:39
-
-
Save SaphireLattice/cfd47c632546ae20c447d33c5e1042ab to your computer and use it in GitHub Desktop.
Advent of code - Day 6 - Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mem = {[1] = "0 5 10 0 11 14 13 4 11 8 8 7 1 4 12 11"} | |
--mem = {[0] = "0 2 7 0"} | |
function split(str) | |
local ret = {} | |
local i = 1 | |
for text in string.gmatch(str, "%S+") do | |
ret[i] = tonumber(text) | |
i = i + 1 | |
end | |
return ret | |
end | |
function step(str) | |
ret = split(str) | |
local max = 1 | |
for i=1, #ret do | |
if ret[i] > ret[max] then | |
max = i | |
end | |
end | |
local all = math.floor(ret[max] / #ret) | |
local remainder = ret[max] % #ret | |
ret[max] = 0 | |
for i=1, #ret do | |
local b = (i + max) % #ret | |
b = (b == 0 and #ret or b) | |
ret[b] = ret[b] + all + (i <= remainder and 1 or 0) | |
end | |
return table.concat(ret, " ") | |
end | |
found = -1 | |
while found == -1 do | |
local m = step(mem[#mem]) | |
print(#mem + 1, m) | |
for i=1, #mem do | |
if (mem[i] == m) then | |
found = i | |
end | |
end | |
mem[#mem + 1] = m | |
end | |
print("Start", "End", "Length") | |
print(found - 1, #mem - 1, #mem - found) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment