diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-09-30 22:56:56 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-09-30 23:00:37 -0700 |
| commit | 9f221c40fce21616bca718b77baede972affd2a9 (patch) | |
| tree | 6ee30ca4750cc5e3da32f787929cad5b454a1381 /challenge-080/tyler-wardhaugh/lua/ch-2.lua | |
| parent | bacd815752b6e6e4c8b0646be9d32ebc53ed478a (diff) | |
| download | perlweeklychallenge-club-9f221c40fce21616bca718b77baede972affd2a9.tar.gz perlweeklychallenge-club-9f221c40fce21616bca718b77baede972affd2a9.tar.bz2 perlweeklychallenge-club-9f221c40fce21616bca718b77baede972affd2a9.zip | |
Ch80: add Lua solutions
Diffstat (limited to 'challenge-080/tyler-wardhaugh/lua/ch-2.lua')
| -rwxr-xr-x | challenge-080/tyler-wardhaugh/lua/ch-2.lua | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-080/tyler-wardhaugh/lua/ch-2.lua b/challenge-080/tyler-wardhaugh/lua/ch-2.lua new file mode 100755 index 0000000000..54d355d037 --- /dev/null +++ b/challenge-080/tyler-wardhaugh/lua/ch-2.lua @@ -0,0 +1,25 @@ +#!/usr/bin/env lua + +local t2 = {} + +function t2.count_candies(coll) + local count = #coll + local maybe_inc = function(i, j) + if (coll[i] > coll[j]) then count = count + 1 end + end + + -- sweep left-to-right + for i=1,#coll-1 do maybe_inc(i, i+1) end + -- sweep right-to-left + for i=#coll,2,-1 do maybe_inc(i, i-1) end + + return count +end + +function t2.run(arg) + local N = {} + for _, v in ipairs(arg) do table.insert(N, tonumber(v)) end + print(t2.count_candies(N)) +end + +return t2 |
