diff options
Diffstat (limited to 'challenge-101/abigail/lua')
| -rw-r--r-- | challenge-101/abigail/lua/ch-1.lua | 126 | ||||
| -rw-r--r-- | challenge-101/abigail/lua/ch-2.lua | 52 |
2 files changed, 178 insertions, 0 deletions
diff --git a/challenge-101/abigail/lua/ch-1.lua b/challenge-101/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..23262855b3 --- /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 diff --git a/challenge-101/abigail/lua/ch-2.lua b/challenge-101/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..d7ecaacfb9 --- /dev/null +++ b/challenge-101/abigail/lua/ch-2.lua @@ -0,0 +1,52 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +-- +-- See ../README.md +-- + +-- +-- This determines on which side of the line through (x1, y1) and +-- (x2, y2) the origin lies. If > 0, then the origin lies to the left +-- of the line, if < 0, the origin lies to the right of the line, if +-- = 0, the origin lies on the line. +-- +function side (x1, y1, x2, y2) + return (y2 - y1) * x2 - (x2 - x1) * y2 +end + +for line in io . lines () do + -- + -- Parse input + -- + local _, _, x1, y1, x2, y2, x3, y3 = + line : find ("(%S+) (%S+) (%S+) (%S+) (%S+) (%S+)") + + -- + -- Determine where the origin is relative to the three lines + -- through the vertices of the triangle. Note we have to go + -- in a specific order through the points. (Either clock wise, + -- or counter clockwise, as long as we're consistent). + -- + local s1 = side (x2, y2, x3, y3) + local s2 = side (x3, y3, x1, y1) + local s3 = side (x1, y1, x2, y2) + + -- + -- If the origin either lies to the left (or on) each of the + -- lines, or to the right (or on) each of the lines, the origin + -- lies inside the triangle. If not, it does not. + -- + if (s1 <= 0 and s2 <= 0 and s3 <= 0) or + (s1 >= 0 and s2 >= 0 and s3 >= 0) + then print (1) + else print (0) + end +end |
