aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-10 07:16:49 +0000
committerGitHub <noreply@github.com>2020-11-10 07:16:49 +0000
commit3a4950a88d34887b52b97622cd7f8511eef9c16c (patch)
treeb4293686aefd17e9829a32a736fc2b2f4c93d779
parent57797ff049fef375654bcc7ffc47379211f9da8c (diff)
parent5bf71a1afc60b6eb2435b0208027a470a9d3cde6 (diff)
downloadperlweeklychallenge-club-3a4950a88d34887b52b97622cd7f8511eef9c16c.tar.gz
perlweeklychallenge-club-3a4950a88d34887b52b97622cd7f8511eef9c16c.tar.bz2
perlweeklychallenge-club-3a4950a88d34887b52b97622cd7f8511eef9c16c.zip
Merge pull request #2740 from waltman/branch-for-challenge-086
Branch for challenge 086
-rw-r--r--challenge-086/walt-mankowski/README1
-rw-r--r--challenge-086/walt-mankowski/README.md37
-rw-r--r--challenge-086/walt-mankowski/perl/ch-1.pl30
-rw-r--r--challenge-086/walt-mankowski/perl/ch-2.pl38
-rw-r--r--challenge-086/walt-mankowski/perl/sudodu.txt9
5 files changed, 114 insertions, 1 deletions
diff --git a/challenge-086/walt-mankowski/README b/challenge-086/walt-mankowski/README
deleted file mode 100644
index cf907b02dd..0000000000
--- a/challenge-086/walt-mankowski/README
+++ /dev/null
@@ -1 +0,0 @@
-Solutions by Walt Mankowski.
diff --git a/challenge-086/walt-mankowski/README.md b/challenge-086/walt-mankowski/README.md
new file mode 100644
index 0000000000..9e5e507562
--- /dev/null
+++ b/challenge-086/walt-mankowski/README.md
@@ -0,0 +1,37 @@
+Solutions by Walt Mankowski.
+
+# Task #1: Pair Difference
+
+For this task we're given an array numbers `n`, and we need to check there
+exists a pair of numbers whose difference is a given value `a`.
+
+To solve this I used
+[Algorithm::Combinatorics](https://metacpan.org/pod/Algorithm::Combinatorics)
+to generate all the pairs of elements in `n`, then checked to see if
+the absolute value of their difference was `a`:
+
+```perl
+my $res = 0;
+my $iter = combinations(\@n, 2);
+while (my $c = $iter->next) {
+ if (abs($c->[0] - $c->[1]) == $a) {
+ $res = 1;
+ last;
+ }
+}
+
+say $res;
+```
+
+# Task #2: Sudoku Puzzle
+
+In this task we need to solve a Sudoku puzzle. I used
+[Games::Sudoku::Lite](https://metacpan.org/pod/Games::Sudoku::Lite) to
+solve the puzzle. Now, some might argue that this is cheating, but I
+don't think so. If it's OK to use a combinatorics module to solve task
+1, surely it's also OK to use a Sudoku module to solve task 2!
+
+In fact that's the same conclusion I reached when a Sudoku problem
+came up in [Project Euler](https://projecteuler.net/problem=96). I
+just had to tweak my parsing code a bit from the code I wrote back
+then and I was good to go.
diff --git a/challenge-086/walt-mankowski/perl/ch-1.pl b/challenge-086/walt-mankowski/perl/ch-1.pl
new file mode 100644
index 0000000000..918cf2923d
--- /dev/null
+++ b/challenge-086/walt-mankowski/perl/ch-1.pl
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use Algorithm::Combinatorics qw(combinations);
+
+# TASK #1 › Pair Difference
+# Submitted by: Mohammad S Anwar
+#
+# You are given an array of integers @N and an integer $A.
+#
+# Write a script to find find if there exists a pair of elements in
+# the array whose difference is $A.
+#
+# Print 1 if exists otherwise 0.
+
+my @n = @ARGV[0..$#ARGV-1];
+my $a = $ARGV[-1];
+
+my $res = 0;
+my $iter = combinations(\@n, 2);
+while (my $c = $iter->next) {
+ if (abs($c->[0] - $c->[1]) == $a) {
+ $res = 1;
+ last;
+ }
+}
+
+say $res;
diff --git a/challenge-086/walt-mankowski/perl/ch-2.pl b/challenge-086/walt-mankowski/perl/ch-2.pl
new file mode 100644
index 0000000000..fa06b18985
--- /dev/null
+++ b/challenge-086/walt-mankowski/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use feature qw(:5.32);
+use experimental qw(signatures);
+use Games::Sudoku::Lite;
+
+# TASK #2 › Sudoku Puzzle
+# Submitted by: Mohammad S Anwar
+#
+# You are given Sudoku puzzle (9x9).
+#
+# Write a script to complete the puzzle
+
+my $board;
+my $rows = 0;
+my $tot = 0;
+
+while (<>) {
+ if (/^Grid/) {
+ say;
+ next;
+ }
+
+ # reformat the problem to the format G::S::L expects
+ s/_/./g;
+ s/[\[\] ]//g;
+ $board .= $_;
+ if (++$rows == 9) {
+ my $puzzle = Games::Sudoku::Lite->new($board);
+ $puzzle->solve;
+ my $sol = $puzzle->solution;
+
+ # add spaces between the digits to make it easier to read
+ $sol =~ s/(\d)/$1 /g;
+ say $sol;
+ }
+}
diff --git a/challenge-086/walt-mankowski/perl/sudodu.txt b/challenge-086/walt-mankowski/perl/sudodu.txt
new file mode 100644
index 0000000000..686c091688
--- /dev/null
+++ b/challenge-086/walt-mankowski/perl/sudodu.txt
@@ -0,0 +1,9 @@
+[ _ _ _ 2 6 _ 7 _ 1 ]
+[ 6 8 _ _ 7 _ _ 9 _ ]
+[ 1 9 _ _ _ 4 5 _ _ ]
+[ 8 2 _ 1 _ _ _ 4 _ ]
+[ _ _ 4 6 _ 2 9 _ _ ]
+[ _ 5 _ _ _ 3 _ 2 8 ]
+[ _ _ 9 3 _ _ _ 7 4 ]
+[ _ 4 _ _ 5 _ _ 3 6 ]
+[ 7 _ 3 _ 1 8 _ _ _ ]