diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-16 10:32:33 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-16 10:32:33 +0000 |
| commit | 7c480291e4a71e4320fc98553de21c9eb47058c8 (patch) | |
| tree | 88ec6352c250c5653550dec54fb15984523b022d | |
| parent | 348df89f110c0cc8654bc7ef97e1960e96c57ef2 (diff) | |
| parent | d6bba8467dbed369a17a8c8924d4cf51de6563b6 (diff) | |
| download | perlweeklychallenge-club-7c480291e4a71e4320fc98553de21c9eb47058c8.tar.gz perlweeklychallenge-club-7c480291e4a71e4320fc98553de21c9eb47058c8.tar.bz2 perlweeklychallenge-club-7c480291e4a71e4320fc98553de21c9eb47058c8.zip | |
Merge pull request #5658 from Abigail/abigail/week-152
Week 152: Solutions in 8 languages.
24 files changed, 529 insertions, 6 deletions
diff --git a/challenge-152/abigail/README.md b/challenge-152/abigail/README.md index 7c068e4d4a..9b1fd5d762 100644 --- a/challenge-152/abigail/README.md +++ b/challenge-152/abigail/README.md @@ -16,15 +16,9 @@ * [AWK](awk/ch-2.awk) * [Bash](bash/ch-2.sh) -* [bc](bc/ch-2.bc) * [C](c/ch-2.c) -* [Go](go/ch-2.go) -* [Java](java/ch-2.java) * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) -* [Pascal](pascal/ch-2.p) * [Perl](perl/ch-2.pl) * [Python](python/ch-2.py) -* [R](r/ch-2.r) * [Ruby](ruby/ch-2.rb) -* [Tcl](tcl/ch-2.tcl) diff --git a/challenge-152/abigail/awk/ch-1.awk b/challenge-152/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..76ebeb1aff --- /dev/null +++ b/challenge-152/abigail/awk/ch-1.awk @@ -0,0 +1,30 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: awk -f ch-1.awk < input-file +# + +{ + minsum = $1 + + min = 0 + n = 2 + m = n + for (i = 2; i <= NF; i ++) { + if (m == n || $i < min) { + min = $i + } + if (-- m == 0) { + minsum += min + m = ++ n + min = 0 + } + } + + print minsum +} + diff --git a/challenge-152/abigail/awk/ch-2.awk b/challenge-152/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..9af83cb5a7 --- /dev/null +++ b/challenge-152/abigail/awk/ch-2.awk @@ -0,0 +1,23 @@ +#!/usr/bin/awk + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +function min (a, b) {return a < b ? a : b} +function max (a, b) {return a < b ? b : a} + +{ + print ((max($1, $3) - min($1, $3)) * \ + (max($2, $4) - min($2, $4)) + \ + (max($5, $7) - min($5, $7)) * \ + (max($6, $8) - min($6, $8)) - \ + max(0, min(max($1, $3), max($5, $7)) - \ + max(min($1, $3), min($5, $7))) * \ + max(0, min(max($2, $4), max($6, $8)) - \ + max(min($2, $4), min($6, $8)))) +} diff --git a/challenge-152/abigail/bash/ch-1.sh b/challenge-152/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..941876c840 --- /dev/null +++ b/challenge-152/abigail/bash/ch-1.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +while read -a F +do ((minsum = 0)) + ((n = 1)) + ((m = n)) + ((min = 0)) + for num in ${F[@]} + do if ((n == m || num < min)) + then ((min = num)) + fi + + if ((-- m == 0)) + then ((minsum += min)) + ((m = ++ n)) + ((min = 0)) + fi + done + + echo $minsum + +done diff --git a/challenge-152/abigail/bash/ch-2.sh b/challenge-152/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..cdbc6b2d22 --- /dev/null +++ b/challenge-152/abigail/bash/ch-2.sh @@ -0,0 +1,36 @@ +#!/bin/sh + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +while read a_x1 a_y1 a_x2 a_y2 b_x1 b_y1 b_x2 b_y2 +do + ((a_x_min = a_x1 < a_x2 ? a_x1 : a_x2)) + ((a_x_max = a_x1 < a_x2 ? a_x2 : a_x1)) + ((a_y_min = a_y1 < a_y2 ? a_y1 : a_y2)) + ((a_y_max = a_y1 < a_y2 ? a_y2 : a_y1)) + ((b_x_min = b_x1 < b_x2 ? b_x1 : b_x2)) + ((b_x_max = b_x1 < b_x2 ? b_x2 : b_x1)) + ((b_y_min = b_y1 < b_y2 ? b_y1 : b_y2)) + ((b_y_max = b_y1 < b_y2 ? b_y2 : b_y1)) + + ((a_area = (a_x_max - a_x_min) * (a_y_max - a_y_min))) + ((b_area = (b_x_max - b_x_min) * (b_y_max - b_y_min))) + + ((x_overlap = (a_x_max < b_x_max ? a_x_max : b_x_max) - + (a_x_min < b_x_min ? b_x_min : a_x_min))) + ((y_overlap = (a_y_max < b_y_max ? a_y_max : b_y_max) - + (a_y_min < b_y_min ? b_y_min : a_y_min))) + + ((overlap = (x_overlap < 0 ? 0 : x_overlap) * + (y_overlap < 0 ? 0 : y_overlap))) + + echo $((a_area + b_area - overlap)) +done diff --git a/challenge-152/abigail/c/ch-1.c b/challenge-152/abigail/c/ch-1.c new file mode 100644 index 0000000000..cce0895298 --- /dev/null +++ b/challenge-152/abigail/c/ch-1.c @@ -0,0 +1,42 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 + */ + +/* + * Run as: cc -o ch-1.o ch-1.c; ./ch-1.o < input-file + */ + +int main (void) { + char * line = NULL; + size_t len = 0; + size_t str_len; + + while ((str_len = getline (&line, &len, stdin)) != -1) { + char * line_ptr = line; + int minsum = 0; + int n = 1; + int m = 1; + int num; + int skip; + int min = 0; + while (sscanf (line_ptr, "%d%n", &num, &skip) == 1) { + line_ptr += skip; + if (n == m || num < min) { + min = num; + } + if (!-- m) { + minsum += min; + m = ++ n; + min = 0; + } + } + printf ("%d\n", minsum); + } + free (line); + + return (0); +} diff --git a/challenge-152/abigail/c/ch-2.c b/challenge-152/abigail/c/ch-2.c new file mode 100644 index 0000000000..fab9dc2f1c --- /dev/null +++ b/challenge-152/abigail/c/ch-2.c @@ -0,0 +1,31 @@ +# include <stdlib.h> +# include <stdio.h> +# include <string.h> + +/* + * See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 + */ + +/* + * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file + */ + +int min (int a, int b) {return a < b ? a : b;} +int max (int a, int b) {return a < b ? b : a;} + +int main (void) { + int a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2; + + while (scanf ("%d%d%d%d%d%d%d%d", &a_x1, &a_y1, &a_x2, &a_y2, + &b_x1, &b_y1, &b_x2, &b_y2) == 8) { + printf ("%d\n", (max (a_x1, a_x2) - min (a_x1, a_x2)) * + (max (a_y1, a_y2) - min (a_y1, a_y2)) + + (max (b_x1, b_x2) - min (b_x1, b_x2)) * + (max (b_y1, b_y2) - min (b_y1, b_y2)) - + max (0, min (max (a_x1, a_x2), max (b_x1, b_x2)) - + max (min (a_x1, a_x2), min (b_x1, b_x2))) * + max (0, min (max (a_y1, a_y2), max (b_y1, b_y2)) - + max (min (a_y1, a_y2), min (b_y1, b_y2)))); + } + return (0); +} diff --git a/challenge-152/abigail/lua/ch-1.lua b/challenge-152/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..eb0579ee47 --- /dev/null +++ b/challenge-152/abigail/lua/ch-1.lua @@ -0,0 +1,30 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +for line in io . lines () do + local minsum = 0 + local n = 1 + local m = n + local min = 0 + for num in line : gmatch ("-?[0-9]+") do + num = tonumber (num) + if (m == n) or (num < min) then + min = num + end + m = m - 1 + if m == 0 then + minsum = minsum + min + n = n + 1 + m = n + min = 0 + end + end + print (minsum) +end diff --git a/challenge-152/abigail/lua/ch-2.lua b/challenge-152/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..d7a0f6f594 --- /dev/null +++ b/challenge-152/abigail/lua/ch-2.lua @@ -0,0 +1,29 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +local i_pat = "(-?%d+)" +local pattern = i_pat +for i = 2, 8 do + pattern = pattern .. "%s+" .. i_pat +end + +for line in io . lines () do + local _, _, a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2 = + line : find (pattern) + print ( (math . max (a_x1, a_x2) - math . min (a_x1, a_x2)) * + (math . max (a_y1, a_y2) - math . min (a_y1, a_y2)) + + (math . max (b_x1, b_x2) - math . min (b_x1, b_x2)) * + (math . max (b_y1, b_y2) - math . min (b_y1, b_y2)) - +math . max (0, math . min (math . max (a_x1, a_x2), math . max (b_x1, b_x2)) - + math . max (math . min (a_x1, a_x2), math . min (b_x1, b_x2))) * +math . max (0, math . min (math . max (a_y1, a_y2), math . max (b_y1, b_y2)) - + math . max (math . min (a_y1, a_y2), math . min (b_y1, b_y2)))) + +end diff --git a/challenge-152/abigail/node/ch-1.js b/challenge-152/abigail/node/ch-1.js new file mode 100644 index 0000000000..2a2a41279f --- /dev/null +++ b/challenge-152/abigail/node/ch-1.js @@ -0,0 +1,21 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +// + +// +// Run as: node ch-1.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let numbers = line . trim () . split (/ +/) . map (n => +n) + let minsum = 0 + let n = 1 + while (numbers . length) { + minsum += Math . min (... numbers . splice (0, n ++)) + } + console . log (minsum) +}) diff --git a/challenge-152/abigail/node/ch-2.js b/challenge-152/abigail/node/ch-2.js new file mode 100644 index 0000000000..c8b72f8786 --- /dev/null +++ b/challenge-152/abigail/node/ch-2.js @@ -0,0 +1,25 @@ +#!/usr/local/bin/node + +// +// See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +// + +// +// Run as: node ch-2.js < input-file +// + + require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', line => { + let [a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2] = + line . trim () . split (/ +/) . map (n => +n) + console . log ( (Math . max (a_x1, a_x2) - Math . min (a_x1, a_x2)) * + (Math . max (a_y1, a_y2) - Math . min (a_y1, a_y2)) + + (Math . max (b_x1, b_x2) - Math . min (b_x1, b_x2)) * + (Math . max (b_y1, b_y2) - Math . min (b_y1, b_y2)) - +Math . max (0, Math . min (Math . max (a_x1, a_x2), Math . max (b_x1, b_x2)) - + Math . max (Math . min (a_x1, a_x2), Math . min (b_x1, b_x2))) * +Math . max (0, Math . min (Math . max (a_y1, a_y2), Math . max (b_y1, b_y2)) - + Math . max (Math . min (a_y1, a_y2), Math . min (b_y1, b_y2)))) + +}) diff --git a/challenge-152/abigail/perl/ch-1.pl b/challenge-152/abigail/perl/ch-1.pl new file mode 100644 index 0000000000..fe23c8957f --- /dev/null +++ b/challenge-152/abigail/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: perl ch-1.pl < input-file +# +# We expect the entire triangle to be given as whitespace separated +# numbers, all on one line. Give it's a triangle, we don't need to +# know where the next row starts. +# Each new line is considered a different triangle. +# + +# +# At first, I thought "path" was used in the traditional sense: +# we were to view the triangle as a graph, each node having two +# edges downwards to the nearest nodes on the next row. +# But it looks like we can just take any node on each row, and +# they will all connect. Which makes the exercise trivial: we +# just sum the minimum value from each row. +# + +use List::Util qw [min]; + +while (<>) { + my ($n, $min_sum, @numbers) = (1, 0, split); + $min_sum += min splice @numbers, 0, $n ++ while @numbers; + say $min_sum; +} diff --git a/challenge-152/abigail/perl/ch-2.pl b/challenge-152/abigail/perl/ch-2.pl new file mode 100644 index 0000000000..346de9b235 --- /dev/null +++ b/challenge-152/abigail/perl/ch-2.pl @@ -0,0 +1,64 @@ +#!/opt/perl/bin/perl + +use 5.032; + +use strict; +use warnings; +no warnings 'syntax'; + +use experimental 'signatures'; +use experimental 'lexical_subs'; + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: perl ch-2.pl < input-file +# +# Each line of input is taken as a set of two rectangles, and consists +# of 8 numbers: first the corners of the first rectangle, than the +# corners of the second rectangle. +# +# We calculate the total area as the sum of the areas of each +# rectangle, minus the area of the overlap. +# The area of a rectangle is the product of the absolute difference of the +# x-coordinates and the absolute difference of the y-coordinates. +# The absolute difference of two numbers can be calculated by subtracting +# the minimum of the two from the maximum of the two. +# To calculate the area of the overlap, we multiply the overlap in the +# x-dimension, and the overlap in the y-dimension. To get the overlap +# in the x-dimension, we take the minimum of the max x-values of both +# rectangles, and subtract the maximim of the min x-values of both +# rectangles. If this value is less than 0, the overlap is 0. +# For the overlap in the y-dimension, we do the same with the y values. +# +# min (@F [0, 2]): Smallest x-value for first rectangle +# max (@F [0, 2]): Largest x-value for first rectangle +# min (@F [1, 3]): Smallest y-value for first rectangle +# max (@F [1, 3]): Largest y-value for first rectangle +# min (@F [4, 5]): Smallest x-value for second rectangle +# max (@F [4, 5]): Largest x-value for second rectangle +# min (@F [6, 7]): Smallest y-value for second rectangle +# max (@F [6, 7]): Largest y-value for second rectangle +# +# See https://math.stackexchange.com/questions/99565/ +# simplest-way-to-calculate-the-intersect-area-of-two-rectangles +# + +use List::Util qw [min max]; + +while (<>) {my @F = split; # Mimic -a + + say +(max (@F [0, 2]) - min (@F [0, 2])) * + (max (@F [1, 3]) - min (@F [1, 3])) + + (max (@F [4, 6]) - min (@F [4, 6])) * + (max (@F [5, 7]) - min (@F [5, 7])) - + max (0, min (max (@F [0, 2]), max (@F [4, 6])) - + max (min (@F [0, 2]), min (@F [4, 6]))) * + max (0, min (max (@F [1, 3]), max (@F [5, 7])) - + max (min (@F [1, 3]), min (@F [5, 7]))); + +} + +__END__ diff --git a/challenge-152/abigail/python/ch-1.py b/challenge-152/abigail/python/ch-1.py new file mode 100644 index 0000000000..c9062f3070 --- /dev/null +++ b/challenge-152/abigail/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: python ch-1.py < input-file +# + +import fileinput + +for line in fileinput . input (): + minsum = 0 + n = 1 + m = n + min = 0 + for num in line . strip () . split (" "): + num = int (num) + if n == m or num < min: + min = num + m = m - 1 + if m == 0: + n = n + 1 + m = n + minsum = minsum + min + min = 0 + print (minsum) diff --git a/challenge-152/abigail/python/ch-2.py b/challenge-152/abigail/python/ch-2.py new file mode 100644 index 0000000000..08c884caad --- /dev/null +++ b/challenge-152/abigail/python/ch-2.py @@ -0,0 +1,23 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +for line in fileinput . input (): + [a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2] = \ + [int (x) for x in line . split ()] + print ( (max (a_x1, a_x2) - min (a_x1, a_x2)) * \ + (max (a_y1, a_y2) - min (a_y1, a_y2)) + \ + (max (b_x1, b_x2) - min (b_x1, b_x2)) * \ + (max (b_y1, b_y2) - min (b_y1, b_y2)) - \ + max (0, min (max (a_x1, a_x2), max (b_x1, b_x2)) - \ + max (min (a_x1, a_x2), min (b_x1, b_x2))) * \ + max (0, min (max (a_y1, a_y2), max (b_y1, b_y2)) - \ + max (min (a_y1, a_y2), min (b_y1, b_y2)))) diff --git a/challenge-152/abigail/ruby/ch-1.rb b/challenge-152/abigail/ruby/ch-1.rb new file mode 100644 index 0000000000..7e8132e448 --- /dev/null +++ b/challenge-152/abigail/ruby/ch-1.rb @@ -0,0 +1,30 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: ruby ch-1.rb < input-file +# + +ARGF . each_line do |line| + minsum = 0 + n = 1 + m = n + min = 0 + line . split . map do |num| + num = num . to_i + if n == m or num < min then + min = num + end + m -= 1 + if m == 0 then + minsum += min + n += 1 + m = n + min = 0 + end + end + puts (minsum) +end diff --git a/challenge-152/abigail/ruby/ch-2.rb b/challenge-152/abigail/ruby/ch-2.rb new file mode 100644 index 0000000000..6dea375d17 --- /dev/null +++ b/challenge-152/abigail/ruby/ch-2.rb @@ -0,0 +1,24 @@ +#!/usr/bin/ruby + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152 +# + +# +# Run as: ruby ch-2.rb < input-file +# + +ARGF . each_line do |line| + a_x1, a_y1, a_x2, a_y2, b_x1, b_y1, b_x2, b_y2 = + line . split . map do |n| n . to_i end + + puts (([a_x1, a_x2] . max - [a_x1, a_x2] . min) * + ([a_y1, a_y2] . max - [a_y1, a_y2] . min) + + ([b_x1, b_x2] . max - [b_x1, b_x2] . min) * + ([b_y1, b_y2] . max - [b_y1, b_y2] . min) - + [[[a_x1, a_x2] . max, [b_x1, b_x2] . max] . min - + [[a_x1, a_x2] . min, [b_x1, b_x2] . min] . max, 0] . max * + [[[a_y1, a_y2] . max, [b_y1, b_y2] . max] . min - + [[a_y1, a_y2] . min, [b_y1, b_y2] . min] . max, 0] . max) + +end diff --git a/challenge-152/abigail/t/ctest.ini b/challenge-152/abigail/t/ctest.ini new file mode 100644 index 0000000000..b26e09b1d8 --- /dev/null +++ b/challenge-152/abigail/t/ctest.ini @@ -0,0 +1,9 @@ +#
+# Configuration file for running tests, using ctest.
+# See https://github.com/Abigail/Misc/blob/master/ctest
+#
+
+[names]
+1-1 = Given Examples
+2-1 = Given Examples
+2-2 = Swapped corners
diff --git a/challenge-152/abigail/t/input-1-1 b/challenge-152/abigail/t/input-1-1 new file mode 100644 index 0000000000..7978c8edd8 --- /dev/null +++ b/challenge-152/abigail/t/input-1-1 @@ -0,0 +1,2 @@ +1 5 3 2 3 4 7 1 0 2 6 4 5 2 8 +5 2 3 4 1 5 0 1 2 3 7 2 4 1 9 diff --git a/challenge-152/abigail/t/input-2-1 b/challenge-152/abigail/t/input-2-1 new file mode 100644 index 0000000000..a692cc7c35 --- /dev/null +++ b/challenge-152/abigail/t/input-2-1 @@ -0,0 +1,2 @@ +-1 0 2 2 0 -1 4 4 +-3 -1 1 3 -1 -3 2 2 diff --git a/challenge-152/abigail/t/input-2-2 b/challenge-152/abigail/t/input-2-2 new file mode 100644 index 0000000000..018833f504 --- /dev/null +++ b/challenge-152/abigail/t/input-2-2 @@ -0,0 +1,2 @@ + 2 2 -1 0 0 -1 4 4 +-3 -1 1 3 2 2 -1 -3 diff --git a/challenge-152/abigail/t/output-1-1.exp b/challenge-152/abigail/t/output-1-1.exp new file mode 100644 index 0000000000..512858e604 --- /dev/null +++ b/challenge-152/abigail/t/output-1-1.exp @@ -0,0 +1,2 @@ +8 +9 diff --git a/challenge-152/abigail/t/output-2-1.exp b/challenge-152/abigail/t/output-2-1.exp new file mode 100644 index 0000000000..e688fd9423 --- /dev/null +++ b/challenge-152/abigail/t/output-2-1.exp @@ -0,0 +1,2 @@ +22 +25 diff --git a/challenge-152/abigail/t/output-2-2.exp b/challenge-152/abigail/t/output-2-2.exp new file mode 100644 index 0000000000..e688fd9423 --- /dev/null +++ b/challenge-152/abigail/t/output-2-2.exp @@ -0,0 +1,2 @@ +22 +25 |
