diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2022-02-07 22:37:05 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2022-02-07 22:37:05 +0100 |
| commit | f1bc02f8fcc156205b2a8ccabaf0063f32ab8106 (patch) | |
| tree | c1de78dd3162abbb01e3d729d006d0325cd75d99 | |
| parent | e270077b999c4129874da382d77addc76460aa17 (diff) | |
| download | perlweeklychallenge-club-f1bc02f8fcc156205b2a8ccabaf0063f32ab8106.tar.gz perlweeklychallenge-club-f1bc02f8fcc156205b2a8ccabaf0063f32ab8106.tar.bz2 perlweeklychallenge-club-f1bc02f8fcc156205b2a8ccabaf0063f32ab8106.zip | |
Week 151: Lua solutions
| -rw-r--r-- | challenge-151/abigail/lua/ch-1.lua | 45 | ||||
| -rw-r--r-- | challenge-151/abigail/lua/ch-2.lua | 33 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-151/abigail/lua/ch-1.lua b/challenge-151/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..1d8664060f --- /dev/null +++ b/challenge-151/abigail/lua/ch-1.lua @@ -0,0 +1,45 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + local tree = {} + local d = 1 + local i = 1 + tree [d] = {} + for token in line : gmatch ("(%S+)") do + if token == "|" then + d = d + 1 + i = 1 + tree [d] = {} + goto end_loop + end + if token == "*" then + i = i + 1 + goto end_loop + end + + tree [d] [i] = 1 + i = i + 1 + + ::end_loop:: + end + + for d, row in ipairs (tree) do + for i, _ in pairs (row) do + if not tree [d + 1] or + not tree [d + 1] [2 * i - 1] and not tree [d + 1] [2 * i] then + print (d) + goto end_main + end + end + end + + ::end_main:: +end diff --git a/challenge-151/abigail/lua/ch-2.lua b/challenge-151/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..98df0aae1d --- /dev/null +++ b/challenge-151/abigail/lua/ch-2.lua @@ -0,0 +1,33 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-151 +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +for line in io . lines () do + local houses = {} + local best = {} + for val in line : gmatch ("%d+") do + houses [#houses + 1] = val + end + + for i = #houses, 1, -1 do + if 2 >= #houses then + best [i] = houses [i] + elseif i == #houses then + best [i] = houses [i] + elseif i == 0 then + best [i] = houses [i] + best [i + 2] + elseif i == #houses - 1 then + best [i] = math . max (houses [i], best [i + 1]) + else + best [i] = math . max (houses [i] + best [i + 2], + best [i + 1]) + end + end + print (best [1]) +end |
