aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-08-01 23:18:03 +0100
committerGitHub <noreply@github.com>2021-08-01 23:18:03 +0100
commita3e732f66fb9a452ae71426387da96054cdd1e4f (patch)
tree784fcf7fa8816e905167d3d2d57935df16ad0046
parented823b76e3a2dd399df5a2cf9ef6434c166f197c (diff)
parentcd7bc7d98cfd02ca52521e6fa745da216159083e (diff)
downloadperlweeklychallenge-club-a3e732f66fb9a452ae71426387da96054cdd1e4f.tar.gz
perlweeklychallenge-club-a3e732f66fb9a452ae71426387da96054cdd1e4f.tar.bz2
perlweeklychallenge-club-a3e732f66fb9a452ae71426387da96054cdd1e4f.zip
Merge pull request #4642 from Abigail/abigail/week-123
Abigail/week 123
-rw-r--r--challenge-123/abigail/awk/ch-2.awk14
-rw-r--r--challenge-123/abigail/bash/ch-2.sh14
-rw-r--r--challenge-123/abigail/bc/ch-2.bc20
-rw-r--r--challenge-123/abigail/blog.txt1
-rw-r--r--challenge-123/abigail/blog1.txt1
-rw-r--r--challenge-123/abigail/c/ch-2.c16
-rw-r--r--challenge-123/abigail/go/ch-2.go17
-rw-r--r--challenge-123/abigail/java/ch-2.java17
-rw-r--r--challenge-123/abigail/lua/ch-2.lua14
-rw-r--r--challenge-123/abigail/node/ch-2.js15
-rw-r--r--challenge-123/abigail/pascal/ch-2.p17
-rw-r--r--challenge-123/abigail/perl/ch-1.pl20
-rw-r--r--challenge-123/abigail/perl/ch-2.pl17
-rw-r--r--challenge-123/abigail/python/ch-2.py12
-rw-r--r--challenge-123/abigail/ruby/ch-2.rb16
-rw-r--r--challenge-123/abigail/tcl/ch-2.tcl15
16 files changed, 121 insertions, 105 deletions
diff --git a/challenge-123/abigail/awk/ch-2.awk b/challenge-123/abigail/awk/ch-2.awk
index 27709d6fd0..96ac5314b1 100644
--- a/challenge-123/abigail/awk/ch-2.awk
+++ b/challenge-123/abigail/awk/ch-2.awk
@@ -8,13 +8,11 @@
# Run as: awk -f ch-2.awk < input-file
#
-{
- e1 = ($1 - $3) ^ 2 + ($2 - $4) ^ 2
- e2 = ($3 - $5) ^ 2 + ($4 - $6) ^ 2
- e3 = ($5 - $7) ^ 2 + ($6 - $8) ^ 2
- e4 = ($7 - $1) ^ 2 + ($8 - $2) ^ 2
- d1 = ($1 - $5) ^ 2 + ($2 - $6) ^ 2
- d2 = ($3 - $7) ^ 2 + ($4 - $8) ^ 2
+function dist (x1, y1, x2, y2) {(x1 - x2) ^ 2 + (y1 - y2) ^ 2}
- print ((e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2) ? 1 : 0)
+{
+ print (dist ($1, $2, $3, $4) == dist ($3, $4, $5, $6) &&
+ dist ($3, $4, $5, $6) == dist ($5, $6, $7, $8) &&
+ dist ($5, $6, $7, $8) == dist ($7, $8, $1, $2) &&
+ dist ($1, $2, $5, $6) == dist ($3, $4, $7, $8) ? 1 : 0)
}
diff --git a/challenge-123/abigail/bash/ch-2.sh b/challenge-123/abigail/bash/ch-2.sh
index fb12779c2e..68844b2f46 100644
--- a/challenge-123/abigail/bash/ch-2.sh
+++ b/challenge-123/abigail/bash/ch-2.sh
@@ -10,13 +10,15 @@
set -f
+function dist () {((dist = ($1 - $3) ** 2 + ($2 - $4) ** 2))}
+
while read x1 y1 x2 y2 x3 y3 x4 y4
-do ((e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2))
- ((e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2))
- ((e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2))
- ((e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2))
- ((d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2))
- ((d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2))
+do dist $x1 $y1 $x2 $y2; ((e1 = dist))
+ dist $x2 $y2 $x3 $y3; ((e2 = dist))
+ dist $x3 $y3 $x4 $y4; ((e3 = dist))
+ dist $x4 $y4 $x1 $y1; ((e4 = dist))
+ dist $x1 $y1 $x3 $y3; ((d1 = dist))
+ dist $x2 $y2 $x4 $y4; ((d2 = dist))
if ((e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2))
then echo 1
else echo 0
diff --git a/challenge-123/abigail/bc/ch-2.bc b/challenge-123/abigail/bc/ch-2.bc
index 2ea9169a08..70815c52d7 100644
--- a/challenge-123/abigail/bc/ch-2.bc
+++ b/challenge-123/abigail/bc/ch-2.bc
@@ -8,6 +8,8 @@
# Input should be terminated with a line starting with a 0
#
+define dist (a, b, c, d) {return ((a - c) ^ 2 + (b - d) ^ 2)}
+
while (1) {
a = read ()
if (a == 0) {break}
@@ -18,15 +20,13 @@ while (1) {
f = read ()
g = read ()
h = read ()
- i = (a - c) ^ 2 + (b - d) ^ 2
- j = (c - e) ^ 2 + (d - f) ^ 2
- k = (e - g) ^ 2 + (f - h) ^ 2
- l = (g - a) ^ 2 + (h - b) ^ 2
- m = (a - e) ^ 2 + (b - f) ^ 2
- n = (c - g) ^ 2 + (d - h) ^ 2
- o = 0
- if (i == j && j == k && k == l && m == n) {
- o = 1
+
+ if (dist (a, b, c, d) == dist (c, d, e, f) && \
+ dist (c, d, e, f) == dist (e, f, g, h) && \
+ dist (e, f, g, h) == dist (g, h, a, b) && \
+ dist (a, b, e, f) == dist (c, d, g, h)) {
+ 1
+ } else {
+ 0
}
- o
}
diff --git a/challenge-123/abigail/blog.txt b/challenge-123/abigail/blog.txt
new file mode 100644
index 0000000000..421517e534
--- /dev/null
+++ b/challenge-123/abigail/blog.txt
@@ -0,0 +1 @@
+https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-123-1.html
diff --git a/challenge-123/abigail/blog1.txt b/challenge-123/abigail/blog1.txt
new file mode 100644
index 0000000000..8f627b4c43
--- /dev/null
+++ b/challenge-123/abigail/blog1.txt
@@ -0,0 +1 @@
+https://abigail.github.io/HTML/Perl-Weekly-Challenge/week-123-2.html
diff --git a/challenge-123/abigail/c/ch-2.c b/challenge-123/abigail/c/ch-2.c
index 56f0b615d4..60a24fb06b 100644
--- a/challenge-123/abigail/c/ch-2.c
+++ b/challenge-123/abigail/c/ch-2.c
@@ -10,19 +10,19 @@
* Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
*/
+int dist (int x1, int y1, int x2, int y2) {
+ return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
+}
+
int main (void) {
int x1, y1, x2, y2, x3, y3, x4, y4;
while (scanf ("%d %d %d %d %d %d %d %d",
&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) == 8) {
- int e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
- int e2 = (x2 - x3) * (x2 - x4) + (y2 - y3) * (y2 - y3);
- int e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4);
- int e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1);
- int d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
- int d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4);
-
- printf ("%d\n", e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0);
+ printf ("%d\n", dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) &&
+ dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) &&
+ dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) &&
+ dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) ? 1 : 0);
}
return (0);
diff --git a/challenge-123/abigail/go/ch-2.go b/challenge-123/abigail/go/ch-2.go
index 37d275d5dc..6a287f6d73 100644
--- a/challenge-123/abigail/go/ch-2.go
+++ b/challenge-123/abigail/go/ch-2.go
@@ -12,22 +12,23 @@ import (
"fmt"
)
+func dist (x1 int, y1 int, x2 int, y2 int) int {
+ return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
+}
+
func main () {
- var x1, y1, x2, y2, x3, y3, x4, y4, e1, e2, e3, e4, d1, d2 int;
+ var x1, y1, x2, y2, x3, y3, x4, y4 int;
for {
var n, err = fmt . Scanf ("%d %d %d %d %d %d %d %d",
&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
if (err != nil || n != 8) {
break;
}
- e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
- e2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
- e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4);
- e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1);
- d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
- d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4);
- if (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2) {
+ if (dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) &&
+ dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) &&
+ dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) &&
+ dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4)) {
fmt . Printf ("%d\n", 1);
} else { // Seriously, go, else needs to be cuddled?
fmt . Printf ("%d\n", 0);
diff --git a/challenge-123/abigail/java/ch-2.java b/challenge-123/abigail/java/ch-2.java
index 8d13bcf8bf..24f7dbe9a9 100644
--- a/challenge-123/abigail/java/ch-2.java
+++ b/challenge-123/abigail/java/ch-2.java
@@ -9,6 +9,9 @@
import java.util.*;
public class ch2 {
+ static int dist (int x1, int y1, int x2, int y2) {
+ return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
+ }
public static void main (String [] args) {
Scanner scanner = new Scanner (System . in);
while (scanner . hasNext ()) {
@@ -21,15 +24,11 @@ public class ch2 {
int x4 = scanner . nextInt ();
int y4 = scanner . nextInt ();
- int e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
- int e2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
- int e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4);
- int e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1);
- int d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
- int d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4);
-
- System . out . println (e1 == e2 && e2 == e3 &&
- e3 == e4 && d1 == d2 ? 1 : 0);
+ System . out . println (
+ dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) &&
+ dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) &&
+ dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) &&
+ dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) ? 1 : 0);
}
}
}
diff --git a/challenge-123/abigail/lua/ch-2.lua b/challenge-123/abigail/lua/ch-2.lua
index b379f3078a..ddf8d86a14 100644
--- a/challenge-123/abigail/lua/ch-2.lua
+++ b/challenge-123/abigail/lua/ch-2.lua
@@ -13,16 +13,16 @@ for _ = 2, 8 do
pat = pat .. "%s+(-?%d+)";
end
+function dist (x1, y1, x2, y2)
+ return (x1 - x2) ^ 2 + (y1 - y2) ^ 2
+end
for line in io . lines () do
_, _, x1, y1, x2, y2, x3, y3, x4, y4 = line : find (pat)
- local e1 = (x1 - x2) ^ 2 + (y1 - y2) ^ 2
- local e2 = (x2 - x3) ^ 2 + (y2 - y3) ^ 2
- local e3 = (x3 - x4) ^ 2 + (y3 - y4) ^ 2
- local e4 = (x4 - x1) ^ 2 + (y4 - y1) ^ 2
- local d1 = (x1 - x3) ^ 2 + (y1 - y3) ^ 2
- local d2 = (x2 - x4) ^ 2 + (y2 - y4) ^ 2
- if e1 == e2 and e2 == e3 and e3 == e4 and d1 == d2 then
+ if dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) and
+ dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) and
+ dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) and
+ dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) then
print (1)
else
print (0)
diff --git a/challenge-123/abigail/node/ch-2.js b/challenge-123/abigail/node/ch-2.js
index 85ec5ce186..8e5fdb499e 100644
--- a/challenge-123/abigail/node/ch-2.js
+++ b/challenge-123/abigail/node/ch-2.js
@@ -8,15 +8,16 @@
// Run as: node ch-2.js < input-file
//
+function dist (x1, y1, x2, y2) {
+ return (x1 - x2) ** 2 + (y1 - y2) ** 2
+}
+
require ('readline')
. createInterface ({input: process . stdin})
. on ('line', line => {
let [x1, y1, x2, y2, x3, y3, x4, y4] = line . split (/ +/) . map (_ => +_)
- let e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2
- let e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2
- let e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2
- let e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2
- let d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2
- let d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2
- console . log (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0)
+ console . log (dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) &&
+ dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) &&
+ dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) &&
+ dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) ? 1 : 0)
})
diff --git a/challenge-123/abigail/pascal/ch-2.p b/challenge-123/abigail/pascal/ch-2.p
index d71fa1c2f0..3bd63c819a 100644
--- a/challenge-123/abigail/pascal/ch-2.p
+++ b/challenge-123/abigail/pascal/ch-2.p
@@ -8,20 +8,21 @@ Program IsSquare;
(* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *)
(* *)
+function dist (x1, y1, x2, y2: integer): integer;
+begin
+ dist := (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
+end;
+
var
x1, y1, x2, y2, x3, y3, x4, y4: integer;
- e1, e2, e3, e4, d1, d2: integer;
begin
while not eof () do begin
readln (x1, y1, x2, y2, x3, y3, x4, y4);
- e1 := (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
- e2 := (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3);
- e3 := (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4);
- e4 := (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1);
- d1 := (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3);
- d2 := (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4);
- if (e1 = e2) and (e2 = e3) and (e3 = e4) and (d1 = d2) then begin
+ if (dist (x1, y1, x2, y2) = dist (x2, y2, x3, y3)) and
+ (dist (x2, y2, x3, y3) = dist (x3, y3, x4, y4)) and
+ (dist (x3, y3, x4, y4) = dist (x4, y4, x1, y1)) and
+ (dist (x1, y1, x3, y3) = dist (x2, y2, x4, y4)) then begin
writeln (1);
end
else begin
diff --git a/challenge-123/abigail/perl/ch-1.pl b/challenge-123/abigail/perl/ch-1.pl
index 83fb0e30ec..37077af736 100644
--- a/challenge-123/abigail/perl/ch-1.pl
+++ b/challenge-123/abigail/perl/ch-1.pl
@@ -19,19 +19,21 @@ use experimental 'lexical_subs';
use List::Util qw [min];
my @ugly = (1);
-my @primes = (2, 3, 5);
-my %next = map {$_ => 0} @primes;
+my $next_2 = 0;
+my $next_3 = 0;
+my $next_5 = 0;
#
# We will maintain the following invariants:
#
-# Foreach $p in @primes:
-# $p * $ugly [$next {$p} - 1] <= $ugly [-1] < $p * $ugly [$next {$p}]
+# 2 * $ugly [$next_2 - 1] <= $ugly [-1] < 2 * $ugly [$next_2]
+# 3 * $ugly [$next_3 - 1] <= $ugly [-1] < 3 * $ugly [$next_2]
+# 5 * $ugly [$next_5 - 1] <= $ugly [-1] < 5 * $ugly [$next_2]
#
# And since every ugly number (except the first) is either twice an
# ugly number, three times an ugly number, or five times an ugly
# number, the next ugly number will be the minimum of
-# (2 * $ugly [$next {2}], 3 * $ugly [$next {3}], 5 * $ugly [$next {5}]).
+# (2 * $ugly [$next_2], 3 * $ugly [$next_3], 5 * $ugly [$next_5]).
#
# We will spend O(1) time per generated ugly number, so our
# program will run in O(N) time, using O(N) memory.
@@ -42,14 +44,18 @@ while (my $n = <>) {
#
# Calculate the next ugly number.
#
- push @ugly => min map {$_ * $ugly [$next {$_}]} @primes;
+ push @ugly => min 2 * $ugly [$next_2],
+ 3 * $ugly [$next_3],
+ 5 * $ugly [$next_5];
#
# Update pointers. It could be that more than one pointer needs
# updating. (This happens if the ugly number generated is
# divisible by 6, 10, 15, or 30). No pointer ever needs updating twice.
#
- $_ * $ugly [$next {$_}] <= $ugly [-1] and $next {$_} ++ for @primes;
+ $next_2 ++ if 2 * $ugly [$next_2] <= $ugly [-1];
+ $next_3 ++ if 3 * $ugly [$next_3] <= $ugly [-1];
+ $next_5 ++ if 5 * $ugly [$next_5] <= $ugly [-1];
}
say $ugly [-1];
}
diff --git a/challenge-123/abigail/perl/ch-2.pl b/challenge-123/abigail/perl/ch-2.pl
index 7803ed57b0..6662c53e32 100644
--- a/challenge-123/abigail/perl/ch-2.pl
+++ b/challenge-123/abigail/perl/ch-2.pl
@@ -22,12 +22,15 @@ use experimental 'lexical_subs';
# So we check whether all edges are equal, and whether both diagonals are equal.
#
+#
+# Given the coordinates of two points, return the square of
+# the distance between them.
+#
+sub dist ($x1, $y1, $x2, $y2) {($x1 - $x2) ** 2 + ($y1 - $y2) ** 2}
+
while (<>) {
- my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = split;
- say + ($x1 - $x2) ** 2 + ($y1 - $y2) ** 2 ==
- ($x2 - $x3) ** 2 + ($y2 - $y3) ** 2 ==
- ($x3 - $x4) ** 2 + ($y3 - $y4) ** 2 ==
- ($x4 - $x1) ** 2 + ($y4 - $y1) ** 2 &&
- ($x1 - $x3) ** 2 + ($y1 - $y3) ** 2 ==
- ($x2 - $x4) ** 2 + ($y2 - $y4) ** 2 ? 1 : 0
+ my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = split;
+ say dist ($x1, $y1, $x2, $y2) == dist ($x2, $y2, $x3, $y3) ==
+ dist ($x3, $y3, $x4, $y4) == dist ($x4, $y4, $x1, $x2) &&
+ dist ($x1, $y1, $x3, $y3) == dist ($x2, $y2, $x4, $y4) ? 1 : 0
}
diff --git a/challenge-123/abigail/python/ch-2.py b/challenge-123/abigail/python/ch-2.py
index e61a2cc0c5..437dc072ff 100644
--- a/challenge-123/abigail/python/ch-2.py
+++ b/challenge-123/abigail/python/ch-2.py
@@ -10,15 +10,15 @@
import fileinput
+def dist (x1, y1, x2, y2):
+ return (x1 - x2) ** 2 + (y1 - y2) ** 2
+
for line in fileinput . input ():
[x1, y1, x2, y2, x3, y3, x4, y4] = \
map (lambda x: int (x), line . split (' '))
- if (x1 - x2) ** 2 + (y1 - y2) ** 2 == \
- (x2 - x3) ** 2 + (y2 - y3) ** 2 == \
- (x3 - x4) ** 2 + (y3 - y4) ** 2 == \
- (x4 - x1) ** 2 + (y4 - y1) ** 2 and \
- (x1 - x3) ** 2 + (y1 - y3) ** 2 == \
- (x2 - x4) ** 2 + (y2 - y4) ** 2:
+ if dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) == \
+ dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) and \
+ dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4):
print (1)
else:
print (0)
diff --git a/challenge-123/abigail/ruby/ch-2.rb b/challenge-123/abigail/ruby/ch-2.rb
index 7e7f54b680..56fb532063 100644
--- a/challenge-123/abigail/ruby/ch-2.rb
+++ b/challenge-123/abigail/ruby/ch-2.rb
@@ -8,14 +8,16 @@
# Run as: ruby ch-2.rb < input-file
#
+def dist (x1, y1, x2, y2)
+ return (x1 - x2) ** 2 + (y1 - y2) ** 2
+end
+
ARGF . each_line do
|line|
x1, y1, x2, y2, x3, y3, x4, y4 = line . split(" ") . map {|n| n . to_i}
- e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2
- e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2
- e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2
- e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2
- d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2
- d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2
- puts (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0)
+
+ puts (dist(x1, y1, x2, y2) == dist(x2, y2, x3, y3) &&
+ dist(x2, y2, x3, y3) == dist(x3, y3, x4, y4) &&
+ dist(x3, y3, x4, y4) == dist(x4, y4, x1, y1) &&
+ dist(x1, y1, x3, y3) == dist(x2, y2, x4, y4) ? 1 : 0)
end
diff --git a/challenge-123/abigail/tcl/ch-2.tcl b/challenge-123/abigail/tcl/ch-2.tcl
index ee5ed1563e..2f0ad1e8a3 100644
--- a/challenge-123/abigail/tcl/ch-2.tcl
+++ b/challenge-123/abigail/tcl/ch-2.tcl
@@ -6,15 +6,16 @@
# Run as: tclsh ch-2.tcl < input-file
#
+proc dist {x1 y1 x2 y2} {
+ return [expr ($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2)]
+}
+
while {[gets stdin line] >= 0} {
lassign [split $line " "] x1 y1 x2 y2 x3 y3 x4 y4
- set e1 [expr ($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2)]
- set e2 [expr ($x2 - $x3) * ($x2 - $x3) + ($y2 - $y3) * ($y2 - $y3)]
- set e3 [expr ($x3 - $x4) * ($x3 - $x4) + ($y3 - $y4) * ($y3 - $y4)]
- set e4 [expr ($x4 - $x1) * ($x4 - $x1) + ($y4 - $y1) * ($y4 - $y1)]
- set d1 [expr ($x1 - $x3) * ($x1 - $x3) + ($y1 - $y3) * ($y1 - $y3)]
- set d2 [expr ($x2 - $x4) * ($x2 - $x4) + ($y2 - $y4) * ($y2 - $y4)]
- if {$e1 == $e2 && $e2 == $e3 && $e3 == $e4 && $d1 == $d2} {
+ if {[dist $x1 $y1 $x2 $y2] == [dist $x2 $y2 $x3 $y3] &&
+ [dist $x2 $y2 $x3 $y3] == [dist $x3 $y3 $x4 $y4] &&
+ [dist $x3 $y3 $x4 $y4] == [dist $x4 $y4 $x1 $y1] &&
+ [dist $x1 $y1 $x3 $y3] == [dist $x2 $y2 $x4 $y4]} {
puts 1
} else {
puts 0