forked from manwar/perlweeklychallenge-club
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ch-1.lua
41 lines (35 loc) · 782 Bytes
/
ch-1.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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