aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-02-27 23:19:25 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-02-27 23:19:25 +0000
commit8b92a0d0ccdba50a286fb8d043e25189ade7bb82 (patch)
tree865a9eb956b7fe50c7a785ff40a81441cbe4ba17
parent541a825c91802e0a5bb3bff382b410731aa534f4 (diff)
downloadperlweeklychallenge-club-8b92a0d0ccdba50a286fb8d043e25189ade7bb82.tar.gz
perlweeklychallenge-club-8b92a0d0ccdba50a286fb8d043e25189ade7bb82.tar.bz2
perlweeklychallenge-club-8b92a0d0ccdba50a286fb8d043e25189ade7bb82.zip
Add Lua solution to challenge 101
-rw-r--r--challenge-101/paulo-custodio/lua/ch-1.lua108
-rw-r--r--challenge-101/paulo-custodio/lua/ch-2.lua44
2 files changed, 152 insertions, 0 deletions
diff --git a/challenge-101/paulo-custodio/lua/ch-1.lua b/challenge-101/paulo-custodio/lua/ch-1.lua
new file mode 100644
index 0000000000..168993f879
--- /dev/null
+++ b/challenge-101/paulo-custodio/lua/ch-1.lua
@@ -0,0 +1,108 @@
+#!/usr/bin/env lua
+
+--[[
+You are given an array @A of items (integers say, but they can be anything).
+
+Your task is to pack that array into an MxN matrix spirally counterclockwise,
+as tightly as possible.
+
+‘Tightly’ means the absolute value |M-N| of the difference has to be as small
+as possible.
+--]]
+
+-- find out m,n
+function smallest_rect(n)
+ local low = 1
+ local high = n
+ for i=1, math.floor(math.sqrt(n)) do
+ if math.floor(math.fmod(n, i)) == 0 then
+ low, high = i, math.floor(n/i)
+ end
+ end
+ return low, high
+end
+
+-- find max width of elements, convert to int
+function max_width(numbers)
+ local num_width = 1
+ for i=1, #numbers do
+ if #numbers[i] < num_width then
+ num_width = #numbers[i]
+ numbers[i] = tonumber(numbers[i])
+ end
+ end
+ return num_width
+end
+
+-- build empty rectangle
+function build_empty_rectangle(m, n)
+ local rect = {}
+ for r=1, m do
+ rect[r] = {}
+ for c=1, n do
+ rect[r][c] = ""
+ end
+ end
+ return rect
+end
+
+-- build spiral rectangle
+function spiral(numbers)
+ local m, n = smallest_rect(#numbers)
+ local num_width = max_width(numbers)
+ local rect = build_empty_rectangle(m, n)
+ local r, c = m, 1
+ local i = 1
+ while i <= #numbers do
+ -- go East
+ while c <= n do
+ if rect[r][c] ~= "" then break end
+ rect[r][c] = string.format("%"..tostring(num_width+1).."d",
+ numbers[i])
+ i = i + 1
+ c = c + 1
+ end
+ c = c - 1
+ r = r - 1
+ -- go North
+ while r >= 1 do
+ if rect[r][c] ~= "" then break end
+ rect[r][c] = string.format("%"..tostring(num_width+1).."d",
+ numbers[i])
+ i = i + 1
+ r = r - 1
+ end
+ r = r + 1
+ c = c - 1
+ -- go West
+ while c >= 1 do
+ if rect[r][c] ~= "" then break end
+ rect[r][c] = string.format("%"..tostring(num_width+1).."d",
+ numbers[i])
+ i = i + 1
+ c = c - 1
+ end
+ c = c + 1
+ r = r + 1
+ -- go South
+ while r <= m do
+ if rect[r][c] ~= "" then break end
+ rect[r][c] = string.format("%"..tostring(num_width+1).."d",
+ numbers[i])
+ i = i + 1
+ r = r + 1
+ end
+ r = r - 1
+ c = c + 1
+ end
+
+ -- print result
+ for r=1, m do
+ for c=1, n do
+ io.write(rect[r][c])
+ end
+ io.write("\n")
+ end
+end
+
+spiral(arg)
diff --git a/challenge-101/paulo-custodio/lua/ch-2.lua b/challenge-101/paulo-custodio/lua/ch-2.lua
new file mode 100644
index 0000000000..a748e1bd02
--- /dev/null
+++ b/challenge-101/paulo-custodio/lua/ch-2.lua
@@ -0,0 +1,44 @@
+#!/usr/bin/env lua
+
+--[[
+TASK #2 › Origin-containing Triangle
+Submitted by: Stuart Little
+You are given three points in the plane, as a list of six co-ordinates:
+A=(x1,y1), B=(x2,y2) and C=(x3,y3).
+
+Write a script to find out if the triangle formed by the given three
+co-ordinates contain origin (0,0).
+
+Print 1 if found otherwise 0.
+--]]
+
+function sign(x1,y1,x2,y2,x3,y3)
+ return (x1 - x3) * (y2 - y3) - (x2 - x3) * (y1 - y3)
+end
+
+function point_in_triangle(xp,yp, x1,y1,x2,y2,x3,y3)
+ local d1 = sign(xp,yp, x1,y1, x2,y2)
+ local d2 = sign(xp,yp, x2,y2, x3,y3)
+ local d3 = sign(xp,yp, x3,y3, x1,y1)
+
+ local has_neg
+ if (d1 < 0) or (d2 < 0) or (d3 < 0) then
+ has_neg = true
+ else
+ has_neg = false
+ end
+
+ local has_neg = (d1 < 0) or (d2 < 0) or (d3 < 0)
+ local has_pos = (d1 > 0) or (d2 > 0) or (d3 > 0)
+
+ if has_neg and has_pos then
+ return 0
+ else
+ return 1
+ end
+end
+
+io.write(point_in_triangle(0,0,
+ tonumber(arg[1]), tonumber(arg[2]),
+ tonumber(arg[3]), tonumber(arg[4]),
+ tonumber(arg[5]), tonumber(arg[6])), "\n")