diff options
| author | Abigail <abigail@abigail.be> | 2021-02-24 13:53:35 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-02-24 13:53:35 +0100 |
| commit | 4dbfa232abe9797f56c3ba7427dbae9fb6846943 (patch) | |
| tree | 9413d3d6511ada0970090f1a6693d92632086449 /challenge-101/abigail | |
| parent | 0dabcd579411c6d1544225d972efe81354416895 (diff) | |
| download | perlweeklychallenge-club-4dbfa232abe9797f56c3ba7427dbae9fb6846943.tar.gz perlweeklychallenge-club-4dbfa232abe9797f56c3ba7427dbae9fb6846943.tar.bz2 perlweeklychallenge-club-4dbfa232abe9797f56c3ba7427dbae9fb6846943.zip | |
Lua solution for week 101, part 2
Diffstat (limited to 'challenge-101/abigail')
| -rw-r--r-- | challenge-101/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-101/abigail/lua/ch-2.lua | 52 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-101/abigail/README.md b/challenge-101/abigail/README.md index 450cdc1526..e2b747cc38 100644 --- a/challenge-101/abigail/README.md +++ b/challenge-101/abigail/README.md @@ -102,6 +102,7 @@ Output: 1 because (0,0) is on the edge connecting B and C. * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) +* [Lua](lua/ch-2.lua) * [Perl](perl/ch-2.pl) ### Blog 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 |
