diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-08-18 15:07:49 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-08-18 15:07:49 -0400 |
| commit | 710837b2a38b5fbd4404f8d9be06ef368af0d366 (patch) | |
| tree | 8796e6c6c7be4f7f935e68f3f0bff99bed04eb57 /challenge-126/stuart-little/lua/ch-2.lua | |
| parent | 637f7bbeceeaf4a26e1e6ecba8c6fcb308790bb3 (diff) | |
| parent | 73470591f10490d6385145990c49825266f80b2b (diff) | |
| download | perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.gz perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.tar.bz2 perlweeklychallenge-club-710837b2a38b5fbd4404f8d9be06ef368af0d366.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-126/stuart-little/lua/ch-2.lua')
| -rwxr-xr-x | challenge-126/stuart-little/lua/ch-2.lua | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-126/stuart-little/lua/ch-2.lua b/challenge-126/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..bdbfdf1451 --- /dev/null +++ b/challenge-126/stuart-little/lua/ch-2.lua @@ -0,0 +1,43 @@ +#!/usr/bin/env lua + +-- run <script> + +function nbrs(mat,i,j) + local t={} + for _,x in ipairs({-1,0,1}) do + for _,y in ipairs({-1,0,1}) do + if ((x ~= 0 or y ~= 0) and 1 <= i+x and i+x <= #mat and 1 <= j+y and j+y <= #(mat[1])) then table.insert(t,{i+x,j+y}) end + end + end + return t +end + +local inptStr=[[x * * * x * x x x x +* * * * * * * * * x +* * * * x * x * x * +* * * x x * * * * * +x * * * x * * * * x +]] + +local inpt={} +for ln in inptStr:gmatch("([^\n]+)") do + local lnTab = {} + for chr in ln:gmatch("([^%s]+)") do table.insert(lnTab,chr) end + table.insert(inpt,lnTab) +end + +for i=1,#inpt do + local ln="" + for j=1,#(inpt[1]) do + if inpt[i][j] == 'x' then + ln = ln .. 'x' .. ' ' + else + local nr=0 + for _,v in ipairs(nbrs(inpt,i,j)) do + if inpt[v[1]][v[2]] == 'x' then nr = nr + 1 end + end + ln = ln .. tostring(nr) .. " " + end + end + print(ln) +end |
