aboutsummaryrefslogtreecommitdiff
path: root/challenge-337/sgreen/perl
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2025-09-07 21:12:46 +1000
committerSimon Green <mail@simon.green>2025-09-07 21:12:46 +1000
commita044dc87aa5e75e5510f78ad13e643b04ed4713b (patch)
treee3e966a1072d453807f51d34cb79ff7692a36501 /challenge-337/sgreen/perl
parente80c93c27044ee16a834a9ee64a0087c1c7d0b1d (diff)
downloadperlweeklychallenge-club-a044dc87aa5e75e5510f78ad13e643b04ed4713b.tar.gz
perlweeklychallenge-club-a044dc87aa5e75e5510f78ad13e643b04ed4713b.tar.bz2
perlweeklychallenge-club-a044dc87aa5e75e5510f78ad13e643b04ed4713b.zip
sgreen solutions to challenge 337
Diffstat (limited to 'challenge-337/sgreen/perl')
-rwxr-xr-xchallenge-337/sgreen/perl/ch-1.pl21
-rwxr-xr-xchallenge-337/sgreen/perl/ch-2.pl46
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-337/sgreen/perl/ch-1.pl b/challenge-337/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..403110c537
--- /dev/null
+++ b/challenge-337/sgreen/perl/ch-1.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@numbers) {
+ my @solution = ();
+
+ foreach my $number (@numbers) {
+ # Count how many numbers are smaller or equal than the current number.
+ # Subtract one to exclude the current number itself.
+ my $count = grep { $_ <= $number } @numbers;
+ push @solution, $count - 1;
+ }
+
+ say '(' . join(', ', @solution) . ')';
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-337/sgreen/perl/ch-2.pl b/challenge-337/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..72252f72cb
--- /dev/null
+++ b/challenge-337/sgreen/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub point_score ( $row, $col, $point ) {
+ if ( $row == $point->[0] && $col == $point->[1] ) {
+ return 2;
+ }
+ if ( $row == $point->[0] || $col == $point->[1] ) {
+ return 1;
+ }
+ return 0;
+}
+
+sub main (@ints) {
+ my $rows = shift @ints;
+ my $cols = shift @ints;
+ my @points = ();
+ for ( my $i = 0 ; $i < $#ints ; $i += 2 ) {
+ push @points, [ $ints[$i], $ints[ $i + 1 ] ];
+ }
+
+ my $odd_cells = 0;
+
+ foreach my $row ( 0 .. $rows - 1 ) {
+ foreach my $col ( 0 .. $cols - 1 ) {
+ # Calculate the score for this cell
+ my $score = 0;
+ foreach my $point (@points) {
+ $score += point_score( $row, $col, $point );
+ }
+
+ # If the score is odd, increment the count of odd cells
+ if ( $score % 2 == 1 ) {
+ $odd_cells++;
+ }
+ }
+ }
+
+ say $odd_cells;
+}
+
+main(@ARGV);