aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-14 12:45:14 +0100
committerGitHub <noreply@github.com>2021-07-14 12:45:14 +0100
commitaff8debbaa52ff4bb571794761e8b45e855a01f2 (patch)
tree13c01897e33fe65aa6e96ceff54b8f0ff1e00b6c
parent901b81596522cf6484b171dae81b92612a493124 (diff)
parent3d2be7b010a125678fd91406f01778c535420521 (diff)
downloadperlweeklychallenge-club-aff8debbaa52ff4bb571794761e8b45e855a01f2.tar.gz
perlweeklychallenge-club-aff8debbaa52ff4bb571794761e8b45e855a01f2.tar.bz2
perlweeklychallenge-club-aff8debbaa52ff4bb571794761e8b45e855a01f2.zip
Merge pull request #4517 from stuart-little/stuart-little_079_lua
1st commit on 079_lua
-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))