diff options
| author | Abigail <abigail@abigail.be> | 2021-07-28 18:13:24 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-07-28 18:13:24 +0200 |
| commit | a42addb6a561d57e470cfed7828109bbfba040fb (patch) | |
| tree | c18527120e6a1f8a71e04910bc03ccc7d1e5fffa | |
| parent | bd9797e1d52dc5b0853bc5400f04f477ffd6a4cb (diff) | |
| download | perlweeklychallenge-club-a42addb6a561d57e470cfed7828109bbfba040fb.tar.gz perlweeklychallenge-club-a42addb6a561d57e470cfed7828109bbfba040fb.tar.bz2 perlweeklychallenge-club-a42addb6a561d57e470cfed7828109bbfba040fb.zip | |
Use methods to factor out calculations.
| -rw-r--r-- | challenge-123/abigail/awk/ch-2.awk | 14 | ||||
| -rw-r--r-- | challenge-123/abigail/bash/ch-2.sh | 14 | ||||
| -rw-r--r-- | challenge-123/abigail/bc/ch-2.bc | 20 | ||||
| -rw-r--r-- | challenge-123/abigail/c/ch-2.c | 16 | ||||
| -rw-r--r-- | challenge-123/abigail/go/ch-2.go | 17 | ||||
| -rw-r--r-- | challenge-123/abigail/java/ch-2.java | 17 | ||||
| -rw-r--r-- | challenge-123/abigail/lua/ch-2.lua | 14 | ||||
| -rw-r--r-- | challenge-123/abigail/node/ch-2.js | 15 | ||||
| -rw-r--r-- | challenge-123/abigail/pascal/ch-2.p | 17 | ||||
| -rw-r--r-- | challenge-123/abigail/perl/ch-2.pl | 17 | ||||
| -rw-r--r-- | challenge-123/abigail/python/ch-2.py | 12 | ||||
| -rw-r--r-- | challenge-123/abigail/ruby/ch-2.rb | 16 | ||||
| -rw-r--r-- | challenge-123/abigail/tcl/ch-2.tcl | 15 |
13 files changed, 106 insertions, 98 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/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-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 |
