aboutsummaryrefslogtreecommitdiff
path: root/challenge-075/abigail/lua
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-07-08 20:28:28 +0200
committerAbigail <abigail@abigail.be>2021-07-09 01:35:21 +0200
commit1f9fd3507cfe52a7e394838835f2dd9958100db0 (patch)
tree9dc9e48dcc8e73253c0d58011aaa5b7353669a33 /challenge-075/abigail/lua
parent3fde608ec96122d4f73f1579d2f6256dcb796c28 (diff)
downloadperlweeklychallenge-club-1f9fd3507cfe52a7e394838835f2dd9958100db0.tar.gz
perlweeklychallenge-club-1f9fd3507cfe52a7e394838835f2dd9958100db0.tar.bz2
perlweeklychallenge-club-1f9fd3507cfe52a7e394838835f2dd9958100db0.zip
Lua solutions for week 075
Diffstat (limited to 'challenge-075/abigail/lua')
-rw-r--r--challenge-075/abigail/lua/ch-1.lua43
-rw-r--r--challenge-075/abigail/lua/ch-2.lua49
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-075/abigail/lua/ch-1.lua b/challenge-075/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..6d46469469
--- /dev/null
+++ b/challenge-075/abigail/lua/ch-1.lua
@@ -0,0 +1,43 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+function possibilities (target, coins, from, to)
+ if target == 0 then
+ return 1
+ end
+ if target < 0 or from > to then
+ return 0
+ end
+
+ local sum = 0
+
+ for i = 0, math . floor (target / coins [from]) do
+ sum = sum + possibilities (target - i * coins [from],
+ coins, from + 1, to)
+ end
+
+ return sum
+end
+
+
+for line in io . lines () do
+ local target
+ local coins = {}
+ local i
+ for i in line : gmatch ("%d+") do
+ if target == nil then
+ target = tonumber (i)
+ else
+ table . insert (coins, tonumber (i))
+ end
+ end
+ print (possibilities (target, coins, 1, #coins))
+
+end
diff --git a/challenge-075/abigail/lua/ch-2.lua b/challenge-075/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..0765ff81b1
--- /dev/null
+++ b/challenge-075/abigail/lua/ch-2.lua
@@ -0,0 +1,49 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+for line in io . lines () do
+ local heights = {}
+ local h, i
+ local max_height = 0
+ for h in line : gmatch ("%d+") do
+ h = tonumber (h)
+ table . insert (heights, h)
+ if max_height < h then
+ max_height = h
+ end
+ end
+
+ local max_area = 0
+ for h = 1, max_height do
+ local max = 0
+ local cur = 0
+ for i = 1, #heights do
+ if heights [i] >= h then
+ cur = cur + 1
+ else
+ if max < cur then
+ max = cur
+ end
+ cur = 0
+ end
+ end
+
+ if max < cur then
+ max = cur
+ end
+
+ local area = max * h
+ if max_area < area then
+ max_area = area
+ end
+ end
+
+ print (max_area)
+end