aboutsummaryrefslogtreecommitdiff
path: root/challenge-106/abigail/lua
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-03-29 15:50:40 +0200
committerAbigail <abigail@abigail.be>2021-03-29 15:50:40 +0200
commit65daa19f792a11fa58f41976422039fbc0bc83c7 (patch)
treebec6c94b743e9287b7b31b2ea494f389ec70c1fb /challenge-106/abigail/lua
parenta5a44874e956b3b5337f18ac7c36384b01175745 (diff)
downloadperlweeklychallenge-club-65daa19f792a11fa58f41976422039fbc0bc83c7.tar.gz
perlweeklychallenge-club-65daa19f792a11fa58f41976422039fbc0bc83c7.tar.bz2
perlweeklychallenge-club-65daa19f792a11fa58f41976422039fbc0bc83c7.zip
Lua solution for week 106, part 1
Diffstat (limited to 'challenge-106/abigail/lua')
-rw-r--r--challenge-106/abigail/lua/ch-1.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-106/abigail/lua/ch-1.lua b/challenge-106/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..29c6aceed4
--- /dev/null
+++ b/challenge-106/abigail/lua/ch-1.lua
@@ -0,0 +1,41 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+for line in io . lines () do
+ --
+ -- Extract the numbers from the line of input
+ -- Note that gmatch() doesn't return an array or list;
+ -- we have to iterate over it, and construct an array
+ --
+ local numbers = {}
+ for i in line : gmatch ("(%S+)") do
+ numbers [#numbers + 1] = i
+ end
+
+ --
+ -- Sort it; the default sort is numerical
+ --
+ table . sort (numbers)
+
+ --
+ -- Find the max difference
+ --
+ local max = 0
+ for i, n in ipairs (numbers) do
+ if (i > 1 and (numbers [i] - numbers [i - 1]) > max)
+ then max = numbers [i] - numbers [i - 1]
+ end
+ end
+
+ --
+ -- And print it
+ --
+ print (max)
+end