diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-11 00:14:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-11 00:14:15 +0100 |
| commit | 1bdfa0e96835a0d5da5957ef5388fdca0386dcfc (patch) | |
| tree | 17d279332060639ddf0d41f6435265ca326a9d33 | |
| parent | e07b5bf8bdddeb12360228852d5daef917242092 (diff) | |
| parent | e23c295383779f281154e6b9f1ca97ea1a0cecf2 (diff) | |
| download | perlweeklychallenge-club-1bdfa0e96835a0d5da5957ef5388fdca0386dcfc.tar.gz perlweeklychallenge-club-1bdfa0e96835a0d5da5957ef5388fdca0386dcfc.tar.bz2 perlweeklychallenge-club-1bdfa0e96835a0d5da5957ef5388fdca0386dcfc.zip | |
Merge pull request #12166 from jacoby/master
DAJ 324 blogged
| -rw-r--r-- | challenge-324/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-324/dave-jacoby/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-324/dave-jacoby/perl/ch-2.pl | 45 |
3 files changed, 91 insertions, 0 deletions
diff --git a/challenge-324/dave-jacoby/blog.txt b/challenge-324/dave-jacoby/blog.txt new file mode 100644 index 0000000000..b36af44f40 --- /dev/null +++ b/challenge-324/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby-lpwk.onrender.com/2025/06/05/traveling-through-another-dimension-weekly-challenge-324.html diff --git a/challenge-324/dave-jacoby/perl/ch-1.pl b/challenge-324/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..c162dea473 --- /dev/null +++ b/challenge-324/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; + +my @examples = ( + + { r => 2, c => 2, ints => [ 1, 2, 3, 4 ] }, + { r => 1, c => 3, ints => [ 1, 2, 3 ] }, + { r => 4, c => 1, ints => [ 1, 2, 3, 4 ] }, +); + +for my $example (@examples) { + my $r = $example->{r}; + my $c = $example->{c}; + my $ints = join ', ', $example->{ints}->@*; + my @output = two_d_array($example); + my $output = join ', ', map { qq{[$_]} } + map { join ', ', $_->@* } @output; + say <<"END"; + Input: \@ints = ($ints), + \$r = $r, + \$c = $c + Output: ($output) +END +} + +sub two_d_array($example) { + my @output; + my $r = $example->{r}; + my $c = $example->{c}; + my @ints = $example->{ints}->@*; + my $rr = my $cc = 0; + for my $i (@ints) { + $output[$rr][$cc] = $i; + $cc++; + if ( $cc >= $c ) { + $cc = 0; + $rr++; + } + } + return wantarray ? @output : \@output; +} + diff --git a/challenge-324/dave-jacoby/perl/ch-2.pl b/challenge-324/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..07e1264602 --- /dev/null +++ b/challenge-324/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say state postderef signatures }; +use Algorithm::Permute; + +my @examples = ( + + [ 1, 3 ], + [ 5, 1, 6 ], + [ 3, 4, 5, 6, 7, 8 ], +); + +for my $example (@examples) { + my $ints = join ', ', $example->@*; + my $output = total_xor( $example->@* ); + say <<"END"; + Input: \@ints = ($ints) + Output: $output +END +} + +sub total_xor(@example) { + my $sum = 0; + my @indices = 0 .. $#example; + for my $l ( 1 .. scalar @example ) { + my %done; + my $p = Algorithm::Permute->new( \@indices, $l ); + while ( my @x = sort $p->next ) { + my $x = join ' ', sort @x; + next if $done{$x}++; + my @y = map { $example[$_] } @x; + my $s = find_xor(@y); + $sum += $s; + } + } + return $sum; +} + +sub find_xor (@array) { + my $x = 0; + while (@array) { my $i = shift @array; $x = $x ^ $i; } + return $x; +} |
