Created
January 2, 2018 21:01
-
-
Save SwadicalRag/3ca1e0fd053704d2fc0c8df1a9e62f3e to your computer and use it in GitHub Desktop.
CodinGame: There is no Spoon - Episode 1
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
-- Don't let the machines win. You are humanity's last hope... | |
local function debug(fmt,...) | |
io.stderr:write(string.format(fmt,...)) | |
end | |
local function outputCoords(x1,y1,x2,y2,x3,y3) | |
io.write(string.format("%d %d %d %d %d %d\n",x1,y1,x2,y2,x3,y3)) | |
end | |
local function outputInvalid(fmt,...) | |
if fmt then debug(fmt,...) end | |
outputCoords(-1,-1,-1,-1,-1,-1) | |
end | |
-- assume header data is always correct because we prioritise speed :/ | |
local width = tonumber(io.read()) -- the number of cells on the X axis | |
local height = tonumber(io.read()) -- the number of cells on the Y axis | |
local data = {} | |
local function getNode(w,h) | |
repeat | |
if data[w] then | |
if data[w][h] then | |
-- debug("getNode(%d,%d) = %q\n",w,h,data[w][h]) | |
return data[w][h] | |
end | |
end | |
if w >= width then return false end | |
if h >= height then return false end | |
coroutine.yield() | |
until false | |
end | |
local done = false | |
local function scan() | |
for h=0,height-1 do | |
for w=0,width-1 do | |
local currentNode = getNode(w,h) | |
local x2,y2 = -1,-1 | |
local x3,y3 = -1,-1 | |
if currentNode then | |
-- sweet, we are a node | |
for offset=1,(width - w) do | |
if getNode(w + offset,h) then | |
x2,y2 = w + offset,h | |
break | |
end | |
end | |
for offset=1,(height - h) do | |
if getNode(w,h + offset) then | |
x3,y3 = w,h + offset | |
break | |
end | |
end | |
outputCoords(w,h,x2,y2,x3,y3) | |
end | |
end | |
end | |
done = true | |
end | |
scan = coroutine.wrap(scan) | |
local function safeResume() | |
if not done then return scan() end | |
end | |
for h=0,height-1 do | |
line = io.read() -- width characters, each either 0 or . | |
for w=0,width-1 do | |
local datum = line:sub(w + 1,w + 1) | |
if not datum or (datum == "") then | |
-- uh oh? | |
debug("couldn't read column data for row %d past column %d",h,w) | |
break | |
end | |
if (datum == ".") or (datum == "0") then | |
data[w] = data[w] or {} | |
data[w][h] = datum ~= "." | |
safeResume() | |
else | |
debug("column data is neither '.' nor '0'??? (%q)",datum) | |
end | |
end | |
end | |
safeResume() | |
xpcall(main,function(e) | |
outputInvalid("fatal: %s",debug.traceback(e)) | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment