diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-15 21:02:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-15 21:02:03 +0100 |
| commit | 81031de3c1d8fe1c389a60f1c4514e17b4989ea7 (patch) | |
| tree | 2bc6455857f345a77c4ce4c03314684258947989 /challenge-100/stuart-little/lua/ch-2.lua | |
| parent | 3cf2f377eb630f019d81293e81885a8a3a1ba187 (diff) | |
| parent | 81465123f99e4ef5f088a4fb8fab6678723c8d49 (diff) | |
| download | perlweeklychallenge-club-81031de3c1d8fe1c389a60f1c4514e17b4989ea7.tar.gz perlweeklychallenge-club-81031de3c1d8fe1c389a60f1c4514e17b4989ea7.tar.bz2 perlweeklychallenge-club-81031de3c1d8fe1c389a60f1c4514e17b4989ea7.zip | |
Merge pull request #4526 from stuart-little/stuart-little_100_lua
1st commit on 100_lua
Diffstat (limited to 'challenge-100/stuart-little/lua/ch-2.lua')
| -rwxr-xr-x | challenge-100/stuart-little/lua/ch-2.lua | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-100/stuart-little/lua/ch-2.lua b/challenge-100/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..0f47095561 --- /dev/null +++ b/challenge-100/stuart-little/lua/ch-2.lua @@ -0,0 +1,39 @@ +#!/usr/bin/env lua + +--[[ +run <script> <file containing triangle> + +for instance, copy + + 1 + 2 4 + 6 4 9 + 5 1 7 2 + +to a text file and pass that file's path +--]] + +function clps(state,row) + local newS={} + for k,v in ipairs(row) do + local val=v+((state[k] and state[k-1] and math.min(tonumber(state[k]),tonumber(state[k-1]))) or state[k] or state[k-1] or 0) + table.insert(newS,val) + end + return newS +end + +local state={} +local tr={} +for l in io.lines(arg[1]) do + if l:find("%d") then + local row={} + for num in l:gmatch("-?%d+") do table.insert(row,num) end + table.insert(tr,row) + end +end + +for _,v in ipairs(tr) do + state=clps(state,v) +end + +print((#state > 0) and math.min(table.unpack(state)) or 0) |
