aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-02-26 02:09:04 +0100
committerAbigail <abigail@abigail.be>2021-02-26 02:09:04 +0100
commit21170ada7e43fe9a452206db2d9092ec00b3e5e1 (patch)
treed97a12bd6c6054998881ee4377800222f2042a8d
parent1ee5a3b374b07f10366df37b11cc9b6abe754fdd (diff)
downloadperlweeklychallenge-club-21170ada7e43fe9a452206db2d9092ec00b3e5e1.tar.gz
perlweeklychallenge-club-21170ada7e43fe9a452206db2d9092ec00b3e5e1.tar.bz2
perlweeklychallenge-club-21170ada7e43fe9a452206db2d9092ec00b3e5e1.zip
Lua solution for week 101, part 1
-rw-r--r--challenge-101/abigail/README.md1
-rw-r--r--challenge-101/abigail/lua/ch-1.lua126
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-101/abigail/README.md b/challenge-101/abigail/README.md
index 2eaa42f92a..8da1360200 100644
--- a/challenge-101/abigail/README.md
+++ b/challenge-101/abigail/README.md
@@ -65,6 +65,7 @@ or
* [AWK](awk/ch-1.awk)
* [Bash](bash/ch-1.sh)
* [C](c/ch-1.c)
+* [Lua](lua/ch-1.lua)
* [Perl](perl/ch-1.pl)
### Blog
diff --git a/challenge-101/abigail/lua/ch-1.lua b/challenge-101/abigail/lua/ch-1.lua
new file mode 100644
index 0000000000..360d1455ae
--- /dev/null
+++ b/challenge-101/abigail/lua/ch-1.lua
@@ -0,0 +1,126 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-1.lua < input-file
+--
+
+local RIGHT = 0
+local UP = 1
+local LEFT = 2
+local DOWN = 3
+
+--
+-- Read in the input: a single line, with the elemens separated by white space
+--
+local line = io . read ()
+local elements = {}
+for element in line : gmatch ("%S+") do
+ elements [#elements + 1] = element
+end
+
+--
+-- Calculate the optimal width and height
+--
+local height = math . floor (math . sqrt (#elements))
+while #elements % height > 0 do
+ height = height - 1
+end
+local width = #elements / height
+
+--
+-- Fill the matrix, starting from the bottom left
+--
+local min_x = 1
+local max_x = height
+local min_y = 1
+local max_y = width
+local x = max_x
+local y = min_y
+local direction = RIGHT
+
+--
+-- Initialize the matrix
+--
+local matrix = {}
+for x = 1, height do
+ matrix [x] = {}
+end
+
+for i = 1, #elements do
+ matrix [x] [y] = elements [i]
+ local turn = 0
+
+ if direction == RIGHT
+ then if y >= max_y
+ then turn = 1
+ x = x - 1
+ max_x = max_x - 1
+ else y = y + 1
+ end
+ end
+
+ if direction == UP
+ then if x <= min_x
+ then turn = 1
+ y = y - 1
+ max_y = max_y - 1
+ else x = x - 1
+ end
+ end
+
+ if direction == LEFT
+ then if y <= min_y
+ then turn = 1
+ x = x + 1
+ min_x = min_x - 1
+ else y = y - 1
+ end
+ end
+
+ if direction == DOWN
+ then if x >= max_x
+ then turn = 1
+ y = y + 1
+ min_y = min_y + 1
+ else x = x + 1
+ end
+ end
+
+ if turn > 0
+ then direction = (direction + 1) % 4
+ end
+
+end
+
+--
+-- Find the maximum widths in each column
+--
+local widths = {}
+for y = 1, width
+do local max = 0
+ for x = 1, height
+ do if max < #matrix [x] [y]
+ then max = #matrix [x] [y]
+ end
+ end
+ widths [y] = max
+end
+
+
+--
+-- Pretty print the matrix
+--
+for x = 1, height
+do for y = 1, width
+ do if y > 1
+ then io . write (" ")
+ end
+ local format = "%" .. widths [y] .. "s"
+ io . write (string . format (format, matrix [x] [y]))
+ end
+ io . write ("\n")
+end