diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2020-11-16 15:37:01 -0500 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2020-11-16 15:37:01 -0500 |
| commit | 2fad7cc7d337cb44995dc087778c53c798f77c10 (patch) | |
| tree | 698a10655a51a822d00dc7d89d598f86a0f62194 | |
| parent | 4c31310c2fcdcc28b67276d0d5a90fdf820d8b48 (diff) | |
| download | perlweeklychallenge-club-2fad7cc7d337cb44995dc087778c53c798f77c10.tar.gz perlweeklychallenge-club-2fad7cc7d337cb44995dc087778c53c798f77c10.tar.bz2 perlweeklychallenge-club-2fad7cc7d337cb44995dc087778c53c798f77c10.zip | |
Challenge 86 by Jaldhar H. Vyas
| -rw-r--r-- | challenge-086/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-086/jaldhar-h-vyas/perl/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-086/jaldhar-h-vyas/perl/ch-2.pl | 103 | ||||
| -rwxr-xr-x | challenge-086/jaldhar-h-vyas/raku/ch-1.p6 | 8 | ||||
| -rwxr-xr-x | challenge-086/jaldhar-h-vyas/raku/ch-2.p6 | 74 | ||||
| -rw-r--r-- | challenge-086/jaldhar-h-vyas/sudoku.txt | 9 |
6 files changed, 237 insertions, 0 deletions
diff --git a/challenge-086/jaldhar-h-vyas/blog.txt b/challenge-086/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..8b4d12e36e --- /dev/null +++ b/challenge-086/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2020/11/perl_weekly_challenge_week_86.html diff --git a/challenge-086/jaldhar-h-vyas/perl/ch-1.pl b/challenge-086/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..dd7166103a --- /dev/null +++ b/challenge-086/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; + +sub usage { + print << "-USAGE-"; +Usage: + $PROGRAM_NAME <A> [<N> ...] + + <A> difference between any pair from <N> + [<N> ...] array of integers +-USAGE- + + exit(0); +} + +sub combinations { + my @list = @{$_[0]}; + my $length = $_[1]; + + if ($length <= 1) { + return map [$_], @list; + } + + my @combos; + + for (my $i = 0; $i + $length <= scalar @list; $i++) { + my $val = $list[$i]; + my @rest = @list[$i + 1 .. $#list]; + for my $c (combinations(\@rest, $length - 1)) { + push @combos, [$val, @{$c}] ; + } + } + + return @combos; +} + +my $A = shift // usage(); +my @N = @ARGV || usage(); + +say scalar(grep { abs($_->[0] - $_->[1]) == $A; } combinations(\@N, 2)) ? 1 : 0;
\ No newline at end of file diff --git a/challenge-086/jaldhar-h-vyas/perl/ch-2.pl b/challenge-086/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..82b20e76e8 --- /dev/null +++ b/challenge-086/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl +use 5.020; +use warnings; +use English qw/ -no_match_vars /; + +sub process { + my ($file) = @_; + my @puzzle; + + open my $fn, '<', $file or die "$OS_ERROR\n"; + local $RS = undef; + my $data = <$fn>; + close $fn; + + $data =~ s/_/0/g; + my @lines = split /\n/, $data; + for my $line (@lines) { + push @puzzle, [ grep { /\d/ } split //, $line]; + } + + return @puzzle; +} + +sub columns { + my ($puzzle) = @_; + my @columns; + + for my $i (0 .. scalar @{$puzzle} - 1) { + for my $j (0 .. scalar @{$puzzle->[$i]} - 1) { + push @{$columns[$i]}, @{$puzzle->[$j]}[$i]; + } + } + + return @columns; +} + +sub inBox { + my ($puzzle, $row, $col, $num) = @_; + my @box; + + for my $i ($row .. $row + 2) { + push @box, @{$puzzle->[$i]}[$col .. $col + 2]; + } + + return scalar grep { $_ == $num; } @box; +} + +sub inCol { + my ($columns, $col, $num) = @_; + return scalar grep { $_ == $num; } @{$columns->[$col]}; +} + +sub inRow { + my ($puzzle, $row, $num) = @_; + return scalar grep { $_ == $num; } @{$puzzle->[$row]}; +} + +sub isValid { + my ($puzzle, $row, $col, $num) = @_; + my @columns = columns($puzzle); + + return + !inRow($puzzle, $row, $num) && + !inCol(\@columns, $col, $num) && + !inBox($puzzle, $row - $row % 3, $col - $col % 3, $num); +} + +sub solve { + my ($puzzle) = @_; + + for my $row (0 .. scalar @{$puzzle} - 1) { + for my $col (0 .. scalar @{$puzzle->[$row]} - 1) { + if ($puzzle->[$row][$col] == 0) { + + + for my $num (1 .. 9) { + if (isValid($puzzle, $row, $col, $num)) { + $puzzle->[$row][$col] = $num; + if(solve($puzzle)) { + return 1; + } + $puzzle->[$row][$col] = 0; + } + } + + return undef; + } + } + } + + return 1; +} + +my $sudoku = shift; +my @puzzle = process($sudoku); + +if (solve(\@puzzle)) { + for my $row (@puzzle) { + say q{[ }, (join q{ }, @{$row}), q{ ]}; + } +} else { + say 'Unsolvable.'; +}
\ No newline at end of file diff --git a/challenge-086/jaldhar-h-vyas/raku/ch-1.p6 b/challenge-086/jaldhar-h-vyas/raku/ch-1.p6 new file mode 100755 index 0000000000..af68ac3c82 --- /dev/null +++ b/challenge-086/jaldhar-h-vyas/raku/ch-1.p6 @@ -0,0 +1,8 @@ +#!/usr/bin/perl6 + +sub MAIN ( + $A, #= difference between any pair from <N> + *@N #= array of integers +) { + say @N.combinations(2).grep({ @_.max - @_.min == $A }).elems ?? 1 !! 0; +}
\ No newline at end of file diff --git a/challenge-086/jaldhar-h-vyas/raku/ch-2.p6 b/challenge-086/jaldhar-h-vyas/raku/ch-2.p6 new file mode 100755 index 0000000000..bcb5a43502 --- /dev/null +++ b/challenge-086/jaldhar-h-vyas/raku/ch-2.p6 @@ -0,0 +1,74 @@ +#!/usr/bin/perl6 + +sub process($file) { + return $file.IO.lines.map({ + [ $_.subst(q{_}, 0, :g).comb.grep({ / \d /; }) ]; + }); +} + +sub inBox(@puzzle, $row, $col, $num) { + my @box; + + for $row .. $row + 2 -> $i { + @box.push(| @puzzle[$i][$col .. $col + 2]); + } + + return $num == @box.any; +} + +sub inCol(@columns, $col, $num) { + return $num == @columns[$col].any; +} + +sub inRow(@puzzle, $row, $num) { + return $num == @puzzle[$row].any; +} + +sub isValid(@puzzle, $row, $col, $num) { + my @columns = [Z] @puzzle; + + return + !inRow(@puzzle, $row, $num) && + !inCol(@columns, $col, $num) && + !inBox(@puzzle, $row - $row % 3, $col - $col % 3, $num); +} + +sub solve(@puzzle is copy) { + + for 0 ..^ @puzzle.elems -> $row { + for 0 ..^ @puzzle[$row].elems -> $col { + + if @puzzle[$row][$col] == 0 { + + for (1 .. 9) -> $num { + if isValid(@puzzle, $row, $col, $num) { + @puzzle[$row][$col] = $num; + + if solve(@puzzle) { + return True; + } + @puzzle[$row][$col] = 0; + } + } + + return False; + } + } + } + + return True; +} + +sub MAIN ( + $sudoku, #= file containing a sudoku puzzle +) { + my @puzzle = process($sudoku); + + if solve(@puzzle) { + for @puzzle -> @row { + say q{[ }, @row.join(q{ }), q{ ]}; + } + } else { + say 'Unsolvable.'; + } +}
\ No newline at end of file diff --git a/challenge-086/jaldhar-h-vyas/sudoku.txt b/challenge-086/jaldhar-h-vyas/sudoku.txt new file mode 100644 index 0000000000..ff1add060c --- /dev/null +++ b/challenge-086/jaldhar-h-vyas/sudoku.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 _ _ _ ]
\ No newline at end of file |
