diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-06 11:07:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-06 11:07:48 +0000 |
| commit | a32920aa0d0071ff0c2fa8db18a17358d31eb67d (patch) | |
| tree | 23003534d1ce7f93034225c225a7772b422d509a /challenge-089 | |
| parent | 9e092ba89d6c4fefa1d914c8d43dbfbde8a6512d (diff) | |
| parent | 83701d6fa861230fc14c4c3cda71d3b675040bd6 (diff) | |
| download | perlweeklychallenge-club-a32920aa0d0071ff0c2fa8db18a17358d31eb67d.tar.gz perlweeklychallenge-club-a32920aa0d0071ff0c2fa8db18a17358d31eb67d.tar.bz2 perlweeklychallenge-club-a32920aa0d0071ff0c2fa8db18a17358d31eb67d.zip | |
Merge pull request #2919 from LubosKolouch/master
Perl solutions LK Challenge 089
Diffstat (limited to 'challenge-089')
| -rw-r--r-- | challenge-089/lubos-kolouch/perl/ch_1.pl | 42 | ||||
| -rw-r--r-- | challenge-089/lubos-kolouch/perl/ch_2.pl | 115 |
2 files changed, 157 insertions, 0 deletions
diff --git a/challenge-089/lubos-kolouch/perl/ch_1.pl b/challenge-089/lubos-kolouch/perl/ch_1.pl new file mode 100644 index 0000000000..14ec005fbc --- /dev/null +++ b/challenge-089/lubos-kolouch/perl/ch_1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_1.pl +# +# USAGE: ./ch_1.pl +# +# DESCRIPTION: Perl Weekly Challenge 089 +# Task 1 +# GCD Sum +# +# AUTHOR: Lubos Kolouch +# CREATED: 12/04/2020 09:40:19 PM +#=============================================================================== + +use strict; +use warnings; +use Math::Utils qw/gcd/; +use Math::Combinatorics; + +sub get_gcd_sum { + my $n = shift; + + my $gcd_sum = 0; + + my $combinat = Math::Combinatorics->new( + count => 2, + data => [ 1 .. $n ], + ); + while ( my @combo = $combinat->next_combination ) { + $gcd_sum += gcd(@combo); + } + + return $gcd_sum; +} + +use Test::More; + +is( get_gcd_sum(3), 3 ); +is( get_gcd_sum(4), 7 ); + +done_testing; diff --git a/challenge-089/lubos-kolouch/perl/ch_2.pl b/challenge-089/lubos-kolouch/perl/ch_2.pl new file mode 100644 index 0000000000..7dffbe932c --- /dev/null +++ b/challenge-089/lubos-kolouch/perl/ch_2.pl @@ -0,0 +1,115 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch_2.pl +# +# USAGE: ./ch_2.pl +# +# DESCRIPTION: Perl Weekly Challenge 089 +# Task 2 +# Magical Matrix +# +# AUTHOR: Lubos Kolouch +# CREATED: 12/04/2020 09:40:19 PM +#=============================================================================== + +use strict; +use warnings; +use List::Util qw/sum/; +use Data::Dumper; + +sub is_complete { + my $matrix = shift; + # 0 1 2 + # 3 4 5 + # 6 7 8 + + return 0 unless $matrix->[0] + $matrix->[1] + $matrix->[2] == 15; + return 0 unless $matrix->[3] + $matrix->[4] + $matrix->[5] == 15; + return 0 unless $matrix->[6] + $matrix->[7] + $matrix->[8] == 15; + + return 0 unless $matrix->[0] + $matrix->[3] + $matrix->[6] == 15; + return 0 unless $matrix->[1] + $matrix->[4] + $matrix->[7] == 15; + return 0 unless $matrix->[2] + $matrix->[5] + $matrix->[8] == 15; + + return 0 unless $matrix->[0] + $matrix->[4] + $matrix->[8] == 15; + return 0 unless $matrix->[2] + $matrix->[4] + $matrix->[6] == 15; + + return 1; +} + +sub is_valid { + my $matrix = shift; + + # 0 1 2 + # 3 4 5 + # 6 7 8 + + return 0 unless $matrix->[0] + $matrix->[1] + $matrix->[2] <= 15; + return 0 unless $matrix->[3] + $matrix->[4] + $matrix->[5] <= 15; + return 0 unless $matrix->[6] + $matrix->[7] + $matrix->[8] <= 15; + + return 0 unless $matrix->[0] + $matrix->[3] + $matrix->[6] <= 15; + return 0 unless $matrix->[1] + $matrix->[4] + $matrix->[7] <= 15; + return 0 unless $matrix->[2] + $matrix->[5] + $matrix->[8] <= 15; + + return 0 unless $matrix->[0] + $matrix->[4] + $matrix->[8] <= 15; + return 0 unless $matrix->[2] + $matrix->[4] + $matrix->[6] <= 15; + + return 1; +} + +sub get_magical_matrix { + my $matrix = shift; + + my $next_pos = -1; + + for (0..8) { + if ($matrix->[$_] == 0) { + $next_pos = $_; + last; + } + } + + my %available_numbers; + $available_numbers{$_} = 1 for (1..9); + delete $available_numbers{$matrix->[$_]} for (0..8); + + for my $key (keys %available_numbers) { + $matrix->[$next_pos] = $key; + unless (is_valid($matrix)) { + $matrix->[$next_pos] = 0; + next; + } + + my $new_matrix = get_magical_matrix([@$matrix]); + return $new_matrix if is_complete($new_matrix); + } + + return $matrix; +} + +sub print_matrix { + my $matrix = shift; + + for my $row (0..2) { + for my $col (0..2) { + print $matrix->[$row*3+$col]." "; + } + print "\n"; + } +} + + +my @init_matrix; +for my $pos (0..8) { + $init_matrix[$pos] = 0; +} + +my $result = get_magical_matrix(\@init_matrix); +print_matrix($result); + +use Test::More; +is(is_complete($result), 1); + +done_testing; |
