summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lua/day2.lua79
-rw-r--r--lua/punch.lua20
-rw-r--r--lua/scheduler.lua39
-rw-r--r--lua/strict.lua25
-rw-r--r--lua/util.lua16
5 files changed, 179 insertions, 0 deletions
diff --git a/lua/day2.lua b/lua/day2.lua
new file mode 100644
index 0000000..0526d78
--- /dev/null
+++ b/lua/day2.lua
@@ -0,0 +1,79 @@
+dofile("util.lua")
+
+ice_cream_scoops = {
+ "vanilla",
+ "chocolate";
+
+ sprinkles = true
+}
+
+greek_numbers = {
+ ena = "one",
+ dyo = "two",
+ tria = "three"
+}
+
+mt = {
+ __tostring = table_to_string
+}
+
+setmetatable(greek_numbers, mt)
+
+Villain = {
+ health = 100,
+
+ new = function(self, name)
+ obj = {
+ name = name,
+ health = self.health
+ }
+ setmetatable(obj, self)
+ self.__index = self
+ return obj
+ end,
+
+ take_hit = function(self)
+ self.health = self.health - 10
+ end
+}
+
+dietrich = Villain:new("Dietrich")
+
+SuperVillain = Villain:new()
+function SuperVillain:take_hit()
+ self.health = self.health - 5
+end
+
+joker = SuperVillain:new("Joker")
+
+function fibonacci()
+ local m = 1
+ local n = 1
+
+ while true do
+ coroutine.yield(m)
+ m, n = n, m+n
+ end
+end
+
+generator = coroutine.create(fibonacci)
+
+-- Easy
+function concatenate(a1, a2)
+ local new = {}
+ for i = 1, #a1 do
+ new[#new+1] = a1[i]
+ end
+ for i = 1, #a2 do
+ new[#new+1] = a2[i]
+ end
+ return new
+end
+
+l1 = {1,2,3}
+l2 = {4,5,6,7,8}
+
+-- Medium -- ????????????????????????????
+-- function _G.__add(self, v)
+-- return concatenate(self, v)
+-- end
diff --git a/lua/punch.lua b/lua/punch.lua
new file mode 100644
index 0000000..8f8b24f
--- /dev/null
+++ b/lua/punch.lua
@@ -0,0 +1,20 @@
+dofile("scheduler.lua")
+
+function punch()
+ for i = 1, 5 do
+ print('punch ' .. i)
+ wait(1.0)
+ end
+end
+
+function block()
+ for i = 1, 3 do
+ print('block ' .. i)
+ wait(2.0)
+ end
+end
+
+schedule(0.0, coroutine.create(punch))
+schedule(0.0, coroutine.create(block))
+
+run()
diff --git a/lua/scheduler.lua b/lua/scheduler.lua
new file mode 100644
index 0000000..e526efa
--- /dev/null
+++ b/lua/scheduler.lua
@@ -0,0 +1,39 @@
+pending = {}
+
+function schedule(time, action)
+ pending[#pending+1] = {
+ time = time,
+ action = action
+ }
+
+ sort_by_time(pending)
+end
+
+function sort_by_time(array)
+ table.sort(array, function(e1, e2) return e1.time < e2.time end)
+end
+
+function wait(seconds)
+ coroutine.yield(seconds)
+end
+
+function run()
+ while #pending > 0 do
+ while os.clock() < pending[1].time do end -- busy-wait
+
+ local item = remove_first(pending)
+ local _, seconds = coroutine.resume(item.action)
+
+ if seconds then
+ later = os.clock() + seconds
+ schedule(later, item.action)
+ end
+ end
+end
+
+function remove_first(array)
+ result = array[1]
+ array[1] = array[#array]
+ array[#array] = nil
+ return result
+end
diff --git a/lua/strict.lua b/lua/strict.lua
new file mode 100644
index 0000000..1c70442
--- /dev/null
+++ b/lua/strict.lua
@@ -0,0 +1,25 @@
+local _private = {}
+
+function strict_read(table, key)
+ if _private[key] then
+ return _private[key]
+ else
+ error("Invalid key: " .. key)
+ end
+end
+
+function strict_write(table, key, value)
+ if _private[key] then
+ error("Duplicate key: " .. key)
+ else
+ _private[key] = value
+ end
+end
+
+local mt = {
+ __index = strict_read,
+ __newindex = strict_write
+}
+
+treasure = {}
+setmetatable(treasure, mt)
diff --git a/lua/util.lua b/lua/util.lua
new file mode 100644
index 0000000..e68c615
--- /dev/null
+++ b/lua/util.lua
@@ -0,0 +1,16 @@
+function print_table(t)
+ for k, v in pairs(t) do
+ print(k .. ": " .. v)
+ end
+end
+
+function table_to_string(t)
+ local result = {}
+
+ for k, v in pairs(t) do
+ result[#result+1] = k .. ": " .. v
+ end
+
+ return table.concat(result, "\n")
+end
+