diff options
| author | Adam Russell <ac.russell@live.com> | 2020-11-15 18:36:04 -0500 |
|---|---|---|
| committer | Adam Russell <ac.russell@live.com> | 2020-11-15 18:36:04 -0500 |
| commit | cffcf70aae86cd63c94bdad11dcefe27981ced7b (patch) | |
| tree | f3551e39b2165d91f83d6a6d26fbd3d866810866 | |
| parent | f1b99967e1fd48afaa02968bbb923c8af0cc0ff1 (diff) | |
| download | perlweeklychallenge-club-cffcf70aae86cd63c94bdad11dcefe27981ced7b.tar.gz perlweeklychallenge-club-cffcf70aae86cd63c94bdad11dcefe27981ced7b.tar.bz2 perlweeklychallenge-club-cffcf70aae86cd63c94bdad11dcefe27981ced7b.zip | |
perl and prolog solutions for challenge 086
| -rw-r--r-- | challenge-086/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-086/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-086/adam-russell/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-086/adam-russell/prolog/ch-1.p | 34 | ||||
| -rw-r--r-- | challenge-086/adam-russell/prolog/ch-2.p | 54 |
5 files changed, 126 insertions, 0 deletions
diff --git a/challenge-086/adam-russell/blog.txt b/challenge-086/adam-russell/blog.txt new file mode 100644 index 0000000000..4360236687 --- /dev/null +++ b/challenge-086/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2020/11/15 diff --git a/challenge-086/adam-russell/blog1.txt b/challenge-086/adam-russell/blog1.txt new file mode 100644 index 0000000000..ef84506468 --- /dev/null +++ b/challenge-086/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2020/11/15 diff --git a/challenge-086/adam-russell/perl/ch-1.pl b/challenge-086/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..efc52a4f60 --- /dev/null +++ b/challenge-086/adam-russell/perl/ch-1.pl @@ -0,0 +1,36 @@ +use strict; +use warnings; +## +# 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. +## +use boolean; +use Math::Combinatorics; + +sub build_constraints{ + my @constraints; + my $a_not_equal_b = sub { $_[0] != $_[1] }; + my $difference_equal_n = sub { $_[0] - $_[1] == $_[2] }; + return ( + $a_not_equal_b, + $difference_equal_n + ); +} + +MAIN:{ + my $combinations = Math::Combinatorics->new( + count => 2, + data => [@ARGV[1 .. @ARGV - 1]], + ); + my $found = false; + my ($check_equal, $check_difference) = build_constraints(); + while(my @combination = $combinations->next_combination()){ + if($check_equal->(@combination) && $check_difference->(@combination, $ARGV[0])){ + $found = true; + print "1\n"; last; + } + } + print "0\n" if(!$found); +}
\ No newline at end of file diff --git a/challenge-086/adam-russell/prolog/ch-1.p b/challenge-086/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..a698d4bdc0 --- /dev/null +++ b/challenge-086/adam-russell/prolog/ch-1.p @@ -0,0 +1,34 @@ +:- use_module(library(optparse)). +/* + 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. +*/ +opts_spec( + [ + [opt(numbers), + default([10, 8, 12, 15, 5]), + longflags([numbers])], + + [opt(a), + default(7), + longflags([a])] + ]). + +ch_1(L, N):- + member(A, L), + member(B, L), + A =\= B, + D is A - B, + N = D, + writeln(1). + +ch_1(_, _):- + writeln(0). + +main:- + opts_spec(OptsSpec), + opt_arguments(OptsSpec, [numbers(L), a(A)], _AdditionalArguments), + ch_1(L, A), + halt.
\ No newline at end of file diff --git a/challenge-086/adam-russell/prolog/ch-2.p b/challenge-086/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..9c6d29d81d --- /dev/null +++ b/challenge-086/adam-russell/prolog/ch-2.p @@ -0,0 +1,54 @@ +:- use_module(library(clpfd)). + +sudoku(Puzzle, Solution) :- + Solution = Puzzle, + Puzzle = [S11, S12, S13, S14, S15, S16, S17, S18, S19, + S21, S22, S23, S24, S25, S26, S27, S28, S29, + S31, S32, S33, S34, S35, S36, S37, S38, S39, + S41, S42, S43, S44, S45, S46, S47, S48, S49, + S51, S52, S53, S54, S55, S56, S57, S58, S59, + S61, S62, S63, S64, S65, S66, S67, S68, S69, + S71, S72, S73, S74, S75, S76, S77, S78, S79, + S81, S82, S83, S84, S85, S86, S87, S88, S89, + S91, S92, S93, S94, S95, S96, S97, S98, S99], + + ins(Puzzle, 1..9), + + Row1 = [S11, S12, S13, S14, S15, S16, S17, S18, S19], + Row2 = [S21, S22, S23, S24, S25, S26, S27, S28, S29], + Row3 = [S31, S32, S33, S34, S35, S36, S37, S38, S39], + Row4 = [S41, S42, S43, S44, S45, S46, S47, S48, S49], + Row5 = [S51, S52, S53, S54, S55, S56, S57, S58, S59], + Row6 = [S61, S62, S63, S64, S65, S66, S67, S68, S69], + Row7 = [S71, S72, S73, S74, S75, S76, S77, S78, S79], + Row8 = [S81, S82, S83, S84, S85, S86, S87, S88, S89], + Row9 = [S91, S92, S93, S94, S95, S96, S97, S98, S99], + + Column1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91], + Column2 = [S12, S22, S32, S42, S52, S62, S72, S82, S92], + Column3 = [S13, S23, S33, S43, S53, S63, S73, S83, S93], + Column4 = [S14, S24, S34, S44, S54, S64, S74, S84, S94], + Column5 = [S15, S25, S35, S45, S55, S65, S75, S85, S95], + Column6 = [S16, S26, S36, S46, S56, S66, S76, S86, S96], + Column7 = [S17, S27, S37, S47, S57, S67, S77, S87, S97], + Column8 = [S18, S28, S38, S48, S58, S68, S78, S88, S98], + Column9 = [S19, S29, S39, S49, S59, S69, S79, S89, S99], + + SubBox1 = [S11, S12, S13, S21, S22, S23, S31, S32, S33], + SubBox2 = [S41, S42, S43, S51, S52, S53, S61, S62, S63], + SubBox3 = [S71, S72, S73, S81, S82, S83, S91, S92, S93], + SubBox4 = [S14, S15, S16, S24, S25, S26, S34, S35, S36], + SubBox5 = [S44, S45, S46, S54, S55, S56, S64, S65, S66], + SubBox6 = [S74, S75, S76, S84, S85, S86, S94, S95, S96], + SubBox7 = [S17, S18, S19, S27, S28, S29, S37, S38, S39], + SubBox8 = [S47, S48, S49, S57, S58, S59, S67, S68, S69], + SubBox9 = [S77, S78, S79, S87, S88, S89, S97, S98, S99], + + valid([Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9, + Column1, Column2, Column3, Column4, Column5, Column6, Column7, Column8, Column9, + SubBox1, SubBox2, SubBox3, SubBox4, SubBox5, SubBox6, SubBox7, SubBox8, SubBox9]). + +valid([]). +valid([H|T]) :- + all_different(H), + valid(T).
\ No newline at end of file |
