aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-079/stuart-little/lua/ch-1.lua13
-rwxr-xr-xchallenge-079/stuart-little/lua/ch-2.lua35
2 files changed, 48 insertions, 0 deletions
diff --git a/challenge-079/stuart-little/lua/ch-1.lua b/challenge-079/stuart-little/lua/ch-1.lua
new file mode 100755
index 0000000000..ed17d5faac
--- /dev/null
+++ b/challenge-079/stuart-little/lua/ch-1.lua
@@ -0,0 +1,13 @@
+#!/usr/bin/env lua
+
+-- run <script> <number>
+-- https://oeis.org/A000788
+
+function bitsUpTo(n)
+ if n <= 1 then return n end
+ local topBitPos = math.floor(math.log(n,2))
+ local rest = n ~ 2^topBitPos
+ return 1+topBitPos*2^(topBitPos-1) + rest + bitsUpTo(rest)
+end
+
+print(("%d"):format(bitsUpTo(tonumber(arg[1]))))
diff --git a/challenge-079/stuart-little/lua/ch-2.lua b/challenge-079/stuart-little/lua/ch-2.lua
new file mode 100755
index 0000000000..23247611bd
--- /dev/null
+++ b/challenge-079/stuart-little/lua/ch-2.lua
@@ -0,0 +1,35 @@
+#!/usr/bin/env lua
+
+-- run <script> <space-separated numbers>
+
+function histo(t)
+ local mx = math.max(table.unpack(t))
+ local h={}
+ for i=0,mx-1 do
+ local row={}
+ for k,v in ipairs(t) do
+ table.insert(row,v < mx-i and " " or "#")
+ end
+ table.insert(h,row)
+ end
+ return h
+end
+
+function rain(t)
+ local q=0
+ for k,v in ipairs(t) do
+ if k > 1 and k < #t then
+ local l = math.max(table.unpack(t,1,k-1))
+ local r = math.max(table.unpack(t,k+1))
+ local m = math.min(l,r)
+ q=q+math.max(0,m-v)
+ end
+ end
+ return q
+end
+
+local nums={}
+for _,nr in ipairs(arg) do table.insert(nums,tonumber(nr)) end
+for _,row in ipairs(histo(nums)) do print(table.unpack(row)) end
+print(("-"):rep(20))
+print(rain(nums))