Update main.lua
Rewritten for changes to standard.
This commit is contained in:
@@ -1,94 +1,110 @@
|
|||||||
|
-- it's like dancing on ice
|
||||||
|
local function class(factory,stuff)
|
||||||
|
local meta = stuff
|
||||||
|
for index,item in pairs(stuff) do
|
||||||
|
meta["__"..index] = item
|
||||||
|
end
|
||||||
|
return setmetatable(meta,{
|
||||||
|
__call=function(this,...)
|
||||||
|
setmetatable(factory(...),meta)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
end
|
||||||
|
local function iterate(thing)
|
||||||
|
local closure = pairs(thing)
|
||||||
|
return function(...)
|
||||||
|
local index,item in closure(...)
|
||||||
|
return item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local c = class(function()
|
||||||
|
return {i = {}}
|
||||||
|
end)
|
||||||
|
|
||||||
|
local n = class(function(c,v,a,b)
|
||||||
|
local new = {c = c, v = v, a = a, b = b, s = {}}
|
||||||
|
table.insert(a.s,new)
|
||||||
|
table.insert(c.instances,new)
|
||||||
|
return new
|
||||||
|
end)
|
||||||
local I = 1
|
local I = 1
|
||||||
|
local n = function(c,...)
|
||||||
local c = setmetatable({},{
|
N(c,I,...)
|
||||||
__call = function(c,class,data,...)
|
|
||||||
local new = {}
|
|
||||||
-- todo: use a better data structure; recycle nexus
|
|
||||||
new.implies = {} -- Table of all dependency nexus of this class: it.items[0] == c
|
|
||||||
setmetatable(new,c)
|
|
||||||
return new
|
|
||||||
end
|
|
||||||
})
|
|
||||||
c.d = c() -- depend
|
|
||||||
c.s = c() -- solve
|
|
||||||
c.c = c() -- commit
|
|
||||||
|
|
||||||
c.h = c() -- cookie heuristic
|
|
||||||
|
|
||||||
local function h(it)
|
|
||||||
-- Find and return the data value of the first heuristic unit super-nexus of this object
|
|
||||||
for item,index in pairs(it.index) do
|
|
||||||
if item.class = c.h then
|
|
||||||
return item.data
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function s(it)
|
|
||||||
-- todo: use some sort of heuristic here like in A*
|
|
||||||
local options = {it}
|
|
||||||
while true do
|
|
||||||
-- order
|
|
||||||
table.sort(branches,function(a,b) return h(a) > h(b) end)
|
|
||||||
for depends in pairs(it.class.implies) do
|
|
||||||
depends.
|
|
||||||
end
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local n = setmetatable({},{
|
|
||||||
__call = function(n,class,data,...)
|
|
||||||
local new = {}
|
|
||||||
setmetatable(new,n)
|
|
||||||
new.class = class
|
|
||||||
table.insert(class.instances,class)
|
|
||||||
new.data = data
|
|
||||||
new.items = {...}
|
|
||||||
for index,item in pairs(new.items) do
|
|
||||||
item.index[new] = index
|
|
||||||
end
|
|
||||||
new.index = {}
|
|
||||||
s(new)
|
|
||||||
return new
|
|
||||||
end
|
|
||||||
})
|
|
||||||
|
|
||||||
c.after = c()
|
|
||||||
c.inp = {}
|
|
||||||
c.out = {}
|
|
||||||
c.text = {}
|
|
||||||
c.cookie = c()
|
c.cookie = c()
|
||||||
for char in ("abcdefghijklmnopqrstuvwxyz "):match(".") do
|
c.before = c()
|
||||||
c.inp[char] = c()
|
c.after = c()
|
||||||
c.out[char] = c()
|
c.out = c()
|
||||||
|
c.in = c()
|
||||||
|
c.so = c()
|
||||||
|
c[";"] = c()
|
||||||
|
for i in string.byte("a"),string.byte("z") do c[string.char(i)] = c() end
|
||||||
|
do
|
||||||
|
-- !a; !b; !next(a,b) <- !before(b,a) -- if b is after a then a is before b
|
||||||
|
local a = n();
|
||||||
|
local b = n();
|
||||||
|
N(c.so,N(c.next,a,b),N(c.before,b,a))
|
||||||
end
|
end
|
||||||
|
|
||||||
local previous = n(c(),I)
|
local function s(r)
|
||||||
while true do
|
local objects = {r}
|
||||||
local input = io.read()
|
local time = 0
|
||||||
for command in input:gmatch([^@]*) do
|
while true do
|
||||||
local name,text = command:match("(%a*)%s*(.*)")
|
for o in iterate(objects) do
|
||||||
if name == "cookie" then
|
for similar in iterate(o.c.i) do
|
||||||
local cookie = n(c.cookie,I)
|
for _depends in iterate(similar.s) do
|
||||||
n(c.after,I,previous,cookie)
|
if _depends.c == c.so then
|
||||||
previous = cookie
|
for linked in iterate(_depends.b.s) do
|
||||||
else
|
local fail = false
|
||||||
local mode
|
for depends in iterate(linked.s) do
|
||||||
if name == "inp" or name == "" then
|
if depends.c == c.so and depends.b == linked then
|
||||||
mode = c.inp
|
for instanced in iterate(o.s) do
|
||||||
elseif name == "out" then
|
if depends.c == instanced.c then
|
||||||
mode = c.out
|
|
||||||
else
|
end
|
||||||
error(string.format("Unknown command '@%s'",name))
|
end
|
||||||
end
|
end
|
||||||
for char in text:match(".") do
|
end
|
||||||
if mode[char] then
|
if not fail then
|
||||||
local object = n(mode[char],I)
|
|
||||||
n(c.after,I,previous,object)
|
end
|
||||||
previous = object
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
for linked in iterate(o.s) do
|
||||||
|
table.insert(objects,linked)
|
||||||
|
end
|
||||||
|
time = time + 1
|
||||||
|
if time > 100 then return end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local input = io.read()
|
||||||
|
local command,rest = input:match("%a*.+")
|
||||||
|
local previous = N(c())
|
||||||
|
local function write(text,author)
|
||||||
|
for char in text:match(".") do
|
||||||
|
if author[char] then
|
||||||
|
local it = n(author[char])
|
||||||
|
N(c.in,it)
|
||||||
|
N(c.after,previous,it)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
(({
|
||||||
|
["in"] = function()
|
||||||
|
write(rest,c.in)
|
||||||
|
end,
|
||||||
|
["out"] = function()
|
||||||
|
write(rest,c.out)
|
||||||
|
end,
|
||||||
|
["cookie"] = function()
|
||||||
|
write()
|
||||||
|
end,
|
||||||
|
})[command] or function() write(command..rest,c.in) end)()
|
||||||
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user