diff options
| author | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-15 00:04:11 +1100 |
|---|---|---|
| committer | Myoungjin JEON <jeongoon@gmail.com> | 2020-10-15 00:04:11 +1100 |
| commit | dd71532a73981ec613c8c61d99fa4bbd3f3fd79c (patch) | |
| tree | 70b1b5c1613f51461bb27f6b4aa49ba8b5efa78e /challenge-081/tyler-wardhaugh/lua/ch-1.lua | |
| parent | 99053d27c19ab6baee10484e8de730527333ddde (diff) | |
| parent | a86e289ff2bb02e7c579be9175c92f2667168a28 (diff) | |
| download | perlweeklychallenge-club-dd71532a73981ec613c8c61d99fa4bbd3f3fd79c.tar.gz perlweeklychallenge-club-dd71532a73981ec613c8c61d99fa4bbd3f3fd79c.tar.bz2 perlweeklychallenge-club-dd71532a73981ec613c8c61d99fa4bbd3f3fd79c.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-081/tyler-wardhaugh/lua/ch-1.lua')
| -rwxr-xr-x | challenge-081/tyler-wardhaugh/lua/ch-1.lua | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-081/tyler-wardhaugh/lua/ch-1.lua b/challenge-081/tyler-wardhaugh/lua/ch-1.lua new file mode 100755 index 0000000000..8203f05c74 --- /dev/null +++ b/challenge-081/tyler-wardhaugh/lua/ch-1.lua @@ -0,0 +1,42 @@ +#!/usr/bin/env lua + +local t1 = {} + +do + local lpeg = require'lpeg' + local P = lpeg.P + function t1.make_pat(s) + return P(s) + end +end + +function t1.common_base_string(s1, s2) + -- ensure s1 is smaller or equal in length to s2 + if s1:len() > s2:len() then + s1, s2 = s2, s1 + end + + local s2_len = s2:len() + local results, substring, part = {} + for i = 1,s2_len do + -- skip attempt if s2's length not evenly divisible by i + if s2_len % i ~= 0 then goto continue end + + substring = s2:sub(1, s2_len // i) + part = t1.make_pat(substring) + if (part^i):match(s2) and (part^1 * -1):match(s1) then + table.insert(results, substring) + end + ::continue:: + end + + return results +end + +function t1.run(args) + local S1, S2 = table.unpack(args, 1, 2) + local bases = t1.common_base_string(S1, S2) + print(table.concat(bases, " ")) +end + +return t1 |
