From 1f9fd3507cfe52a7e394838835f2dd9958100db0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 8 Jul 2021 20:28:28 +0200 Subject: Lua solutions for week 075 --- challenge-075/abigail/lua/ch-1.lua | 43 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-075/abigail/lua/ch-1.lua (limited to 'challenge-075/abigail/lua/ch-1.lua') 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 -- cgit