From f1bc02f8fcc156205b2a8ccabaf0063f32ab8106 Mon Sep 17 00:00:00 2001 From: Abigail Date: Mon, 7 Feb 2022 22:37:05 +0100 Subject: Week 151: Lua solutions --- challenge-151/abigail/lua/ch-1.lua | 45 ++++++++++++++++++++++++++++++++++++++ challenge-151/abigail/lua/ch-2.lua | 33 ++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 challenge-151/abigail/lua/ch-1.lua create mode 100644 challenge-151/abigail/lua/ch-2.lua 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 -- cgit