aboutsummaryrefslogtreecommitdiff
path: root/challenge-109/abigail/lua
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-04-26 09:15:20 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-04-26 09:15:20 +0100
commit03f28cae3ddea3b08a671dd3f20f3d32777aa4db (patch)
tree7aea1f86e706cf8233d89d704ac2342a0c63d059 /challenge-109/abigail/lua
parent46b8aecc9397c6211a1e97a7f0638833726294a2 (diff)
parent1ff197d81f941c3dd35d77bec8a0326807e8d2b1 (diff)
downloadperlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.gz
perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.tar.bz2
perlweeklychallenge-club-03f28cae3ddea3b08a671dd3f20f3d32777aa4db.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-109/abigail/lua')
-rw-r--r--challenge-109/abigail/lua/ch-1.lua43
-rw-r--r--challenge-109/abigail/lua/ch-2.lua131
2 files changed, 174 insertions, 0 deletions
diff --git a/challenge-109/abigail/lua/ch-1.lua b/challenge-109/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..74c3b7d914
--- /dev/null
+++ b/challenge-109/abigail/lua/ch-1.lua
@@ -0,0 +1,43 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua [plain | compute]
+--
+
+local PLAIN = 0
+local COMPUTE = 1
+
+local COUNT = 20
+
+function divisor_sum (n)
+ local sum = 0
+ for i = 2, n / 2 do
+ if n % i == 0
+ then sum = sum + i
+ end
+ end
+ return (sum)
+end
+
+local type = PLAIN
+if #arg >= 1 and arg [1] == "compute"
+then type = COMPUTE
+end
+
+if type == PLAIN
+then print ("0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21")
+end
+
+if type == COMPUTE
+then for n = 1, COUNT do
+ if n > 1
+ then io . write (", ")
+ end
+ io . write (divisor_sum (n))
+ end
+ io . write ("\n")
+end
diff --git a/challenge-109/abigail/lua/ch-2.lua b/challenge-109/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..2b40532bfc
--- /dev/null
+++ b/challenge-109/abigail/lua/ch-2.lua
@@ -0,0 +1,131 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+local SIZE = 7
+
+for line in io . lines () do
+ local numbers = {}
+ for n in line : gmatch ("-?%d+") do
+ numbers [#numbers + 1] = tonumber (n)
+ end
+
+ --
+ -- Initialize the a differences table.
+ --
+ local differences = {}
+ for n_i, n in pairs (numbers) do
+ differences [n] = {}
+ end
+
+ --
+ -- Find all the differences; store the indices of the
+ -- pairs of numbers giving the difference. Ignore a difference
+ -- which is not present in the numbers array.
+ --
+ for i = 1, SIZE do
+ for j = i + 1, SIZE do
+ local diff = numbers [i] - numbers [j]
+ if differences [ diff] ~= nil
+ then table . insert (differences [ diff], {i, j})
+ end
+ if differences [-diff] ~= nil
+ then table . insert (differences [-diff], {j, i})
+ end
+ end
+ end
+
+ --
+ -- For each possible value d, find the pairs of differences
+ -- equal to d, such that all five numbers are difference.
+ --
+
+ for d_i = 1, SIZE do
+ local d = numbers [d_i]
+ local diffs = differences [d]
+
+ for x = 1, #diffs do
+ --
+ -- Ignore any difference involving d_i
+ --
+ if diffs [x] [1] == d_i or diffs [x] [2] == d_i
+ then goto end_x
+ end
+ for y = x + 1, #diffs do
+ --
+ -- Second difference cannot involve the number at d_i,
+ -- and the indices involved in the second difference
+ -- must be different from the first difference.
+ --
+ if diffs [y] [1] == d_i or diffs [y] [2] == d_i or
+ diffs [x] [1] == diffs [y] [1] or
+ diffs [x] [1] == diffs [y] [2] or
+ diffs [x] [2] == diffs [y] [1] or
+ diffs [x] [2] == diffs [y] [2]
+ then goto end_y
+ end
+
+ --
+ -- W.l.o.g. we can now assume diffs [x] are
+ -- the indices for a and c, and diffs [y]
+ -- are the indices for g and e.
+ --
+ local a_i = diffs [x] [1]
+ local c_i = diffs [x] [2]
+ local g_i = diffs [y] [1]
+ local e_i = diffs [y] [2]
+
+ --
+ -- Try the remaining indices for b_i and f_i
+ --
+ for b_i = 1, SIZE do
+ if b_i == a_i or b_i == c_i or b_i == d_i or
+ b_i == e_i or b_i == g_i
+ then goto end_bi
+ end
+ for f_i = 1, SIZE do
+ if f_i == a_i or f_i == b_i or f_i == c_i or
+ f_i == d_i or f_i == e_i or f_i == g_i
+ then goto end_fi
+ end
+ --
+ -- Do we have a winner?
+ --
+ local target = numbers [a_i] + numbers [b_i]
+ if target == numbers [b_i] + numbers [c_i] +
+ numbers [d_i] and
+ target == numbers [d_i] + numbers [e_i] +
+ numbers [f_i] and
+ target == numbers [f_i] + numbers [g_i]
+ then --
+ -- We have a winner. Print it, and its reverse
+ --
+ io . write (
+ string . format ("%d %d %d %d %d %d %d\n",
+ numbers [a_i], numbers [b_i],
+ numbers [c_i], numbers [d_i],
+ numbers [e_i], numbers [f_i],
+ numbers [g_i]))
+ io . write (
+ string . format ("%d %d %d %d %d %d %d\n",
+ numbers [g_i], numbers [f_i],
+ numbers [e_i], numbers [d_i],
+ numbers [c_i], numbers [b_i],
+ numbers [a_i]))
+ end
+ ::end_fi::
+ end
+ ::end_bi::
+ end
+ ::end_y::
+ end
+ ::end_x::
+ end
+ end
+end