From dc1d45fa369aa039ad3c7d6c721a3de267a60bba Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Wed, 16 Jun 2021 20:40:32 -0400 Subject: initial commit --- challenge-117/adam-russell/blog.txt | 0 challenge-117/adam-russell/blog1.txt | 0 challenge-117/adam-russell/perl/ch-1.pl | 78 ++++++++++++++++++++++++++++++++ challenge-117/adam-russell/perl/ch-2.pl | 0 challenge-117/adam-russell/prolog/ch-1.p | 0 challenge-117/adam-russell/prolog/ch-2.p | 0 6 files changed, 78 insertions(+) create mode 100644 challenge-117/adam-russell/blog.txt create mode 100644 challenge-117/adam-russell/blog1.txt create mode 100644 challenge-117/adam-russell/perl/ch-1.pl create mode 100644 challenge-117/adam-russell/perl/ch-2.pl create mode 100644 challenge-117/adam-russell/prolog/ch-1.p create mode 100644 challenge-117/adam-russell/prolog/ch-2.p diff --git a/challenge-117/adam-russell/blog.txt b/challenge-117/adam-russell/blog.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-117/adam-russell/blog1.txt b/challenge-117/adam-russell/blog1.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-117/adam-russell/perl/ch-1.pl b/challenge-117/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..56c6e7048a --- /dev/null +++ b/challenge-117/adam-russell/perl/ch-1.pl @@ -0,0 +1,78 @@ +use constant TRIANGLE_TOP => q|/\\| ; +use constant TRIANGLE_BOTTOM => q|/__\\|; + +sub print_triangle_3{ + my($n) = @_; + print " " x 5; + print TRIANGLE_TOP x 1; + print "\n"; + print " " x4; + print TRIANGLE_BOTTOM x 1; + print "\n"; + + print " " ." " . " ".TRIANGLE_TOP . " " . " " .TRIANGLE_TOP ; + print "\n"; + print " ". " ".TRIANGLE_BOTTOM . TRIANGLE_BOTTOM; + print "\n"; + + print " " .TRIANGLE_TOP . " ". " ".TRIANGLE_TOP . " ". " ". TRIANGLE_TOP; + print "\n"; + print TRIANGLE_BOTTOM . TRIANGLE_BOTTOM .TRIANGLE_BOTTOM; + print "\n"; + +} + +sub print_triangle_2{ + my($n) = @_; + print " " x 4; + print TRIANGLE_TOP x 1; + print "\n"; + print " " x3; + print TRIANGLE_BOTTOM x 1; + print "\n"; + + print " " x1; + print " " .TRIANGLE_TOP . " " . " " .TRIANGLE_TOP; + print "\n"; + print " ".TRIANGLE_BOTTOM . TRIANGLE_BOTTOM; + print "\n"; + +} + +sub print_triangle{ + my($n) = @_; + my $top = TRIANGLE_TOP . " "; + for my $i (1 .. $n ){ + print " "; + print " " x ($n - $i); + print $top x $i ; + print "\n"; + print " " x ($n - $i ); + print TRIANGLE_BOTTOM x ($i ); + print "\n"; + } +} + +MAIN:{ + print_triangle(1); + print "\n"; + print "\n"; + print_triangle(2); + print "\n"; + print "\n"; + print_triangle(3); + print "\n"; + print "\n"; + print_triangle(4); + print "\n"; + print "\n"; + print_triangle(7); + print "\n"; + print "\n"; + print_triangle(10); + print "\n"; + print "\n"; + print_triangle(20); + print "\n"; + print "\n"; +} diff --git a/challenge-117/adam-russell/perl/ch-2.pl b/challenge-117/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-117/adam-russell/prolog/ch-1.p b/challenge-117/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..e69de29bb2 diff --git a/challenge-117/adam-russell/prolog/ch-2.p b/challenge-117/adam-russell/prolog/ch-2.p new file mode 100644 index 0000000000..e69de29bb2 -- cgit From 7f2dc459a0e72e2d1bed64b9af06588dc47cfcd8 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Fri, 18 Jun 2021 23:35:33 -0400 Subject: task #2 solution --- challenge-117/adam-russell/perl/ch-2.pl | 112 ++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/challenge-117/adam-russell/perl/ch-2.pl b/challenge-117/adam-russell/perl/ch-2.pl index e69de29bb2..999acf07a8 100644 --- a/challenge-117/adam-russell/perl/ch-2.pl +++ b/challenge-117/adam-russell/perl/ch-2.pl @@ -0,0 +1,112 @@ +use strict; +use warnings; +## +# You are given size of a triangle. +# Write a script to find all possible paths from top to the bottom right corner. +## +use constant FINAL => "end"; +use constant DEADEND => "-1"; +use constant TRIANGLE_TOP => q|/\\| ; +use constant TRIANGLE_BOTTOM => q|/__\\|; + +sub find_paths{ + my($n) = @_; + my %paths; + my @complete_paths; + my @vertices; + for my $i (0 .. $n){ + for my $j (0 .. $i){ + push @vertices, "$i-$j"; + } + } + $paths{""}=["0-0",["0-0"]]; + my %updated_paths; + while((keys %paths) > 0){ + %updated_paths = (); + for my $path (keys %paths){ + my @exists; + my @visited; + my $current = $paths{$path}->[0]; + my $visited = $paths{$path}->[1]; + my @ij = split(/\-/, $current); + my($left, $horizontal, $right) = (($ij[0] + 1) . "-" . $ij[1], $ij[0] . "-" . ($ij[1] + 1), ($ij[0] + 1) . "-" . ($ij[1] + 1)); + @exists = grep {$_ eq $left} @vertices; + @visited = grep {$_ eq $left} @{$visited}; + if(@exists && !@visited){ + my $visited_left = [@{$visited}, $left]; + if($left eq "$n-$n"){ + push @complete_paths, $path . "L"; + } + else{ + $updated_paths{$path . "L"} = [$left, $visited_left]; + } + } + @exists = grep {$_ eq $horizontal} @vertices; + @visited = grep {$_ eq $horizontal} @{$visited}; + if(@exists && !@visited){ + my $visited_horizontal = [@{$visited}, $horizontal]; + if($horizontal eq "$n-$n"){ + push @complete_paths, $path . "H"; + } + else{ + $updated_paths{$path . "H"} = [$horizontal, $visited_horizontal]; + } + } + @exists = grep {$_ eq $right} @vertices; + @visited = grep {$_ eq $right} @{$visited}; + if(@exists && !@visited){ + my $visited_right = [@{$visited}, $right]; + if($right eq "$n-$n"){ + push @complete_paths, $path . "R"; + } + else{ + $updated_paths{$path . "R"} = [$right, $visited_right]; + } + } + } + %paths = %updated_paths; + } + return @complete_paths; +} + +sub print_triangle{ + my($n) = @_; + my $top = TRIANGLE_TOP . " "; + for my $i (1 .. $n ){ + print " "; + print " " x ($n - $i); + print $top x $i ; + print "\n"; + print " " x ($n - $i ); + print TRIANGLE_BOTTOM x ($i ); + print "\n"; + } +} + +MAIN:{ + my($N); + $N = 1; + print_triangle($N); + for my $path (find_paths($N)){ + print "$path "; + } + print "\n"; + $N = 2; + print_triangle($N); + for my $path (find_paths($N)){ + print "$path "; + } + print "\n"; + $N = 3; + print_triangle($N); + for my $path (find_paths($N)){ + print "$path "; + } + print "\n"; + $N = 4; + print_triangle($N); + for my $path (find_paths($N)){ + print "$path "; + } + print "\n"; +} -- cgit From 91626ea5c5961030a54122d39b2aa2d3be8493b7 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Fri, 18 Jun 2021 23:54:15 -0400 Subject: task #1 solution --- challenge-117/adam-russell/perl/ch-1.pl | 108 +++++++++++--------------------- 1 file changed, 35 insertions(+), 73 deletions(-) diff --git a/challenge-117/adam-russell/perl/ch-1.pl b/challenge-117/adam-russell/perl/ch-1.pl index 56c6e7048a..bd250abf34 100644 --- a/challenge-117/adam-russell/perl/ch-1.pl +++ b/challenge-117/adam-russell/perl/ch-1.pl @@ -1,78 +1,40 @@ -use constant TRIANGLE_TOP => q|/\\| ; -use constant TRIANGLE_BOTTOM => q|/__\\|; - -sub print_triangle_3{ - my($n) = @_; - print " " x 5; - print TRIANGLE_TOP x 1; - print "\n"; - print " " x4; - print TRIANGLE_BOTTOM x 1; - print "\n"; - - print " " ." " . " ".TRIANGLE_TOP . " " . " " .TRIANGLE_TOP ; - print "\n"; - print " ". " ".TRIANGLE_BOTTOM . TRIANGLE_BOTTOM; - print "\n"; - - print " " .TRIANGLE_TOP . " ". " ".TRIANGLE_TOP . " ". " ". TRIANGLE_TOP; - print "\n"; - print TRIANGLE_BOTTOM . TRIANGLE_BOTTOM .TRIANGLE_BOTTOM; - print "\n"; - -} - -sub print_triangle_2{ - my($n) = @_; - print " " x 4; - print TRIANGLE_TOP x 1; - print "\n"; - print " " x3; - print TRIANGLE_BOTTOM x 1; - print "\n"; - - print " " x1; - print " " .TRIANGLE_TOP . " " . " " .TRIANGLE_TOP; - print "\n"; - print " ".TRIANGLE_BOTTOM . TRIANGLE_BOTTOM; - print "\n"; - +use strict; +use warnings; +## +# You are given text file with rows numbered 1-15 in +# random order but there is a catch one row in missing in the file. +# Write a script to find the missing row number. +## +sub find_missing{ + my(@numbers) = sort {$a <=> $b} @_; + for(my $i=0; $i< @numbers - 1; $i++){ + return $numbers[$i] + 1 if $numbers[$i] != $numbers[$i + 1] - 1; + } } -sub print_triangle{ - my($n) = @_; - my $top = TRIANGLE_TOP . " "; - for my $i (1 .. $n ){ - print " "; - print " " x ($n - $i); - print $top x $i ; - print "\n"; - print " " x ($n - $i ); - print TRIANGLE_BOTTOM x ($i ); - print "\n"; +MAIN:{ + my @line_numbers; + while(){ + chomp; + m/([0-9]+),.*/; + push @line_numbers, $1; } + my $missing = find_missing(@line_numbers); + print "$missing\n"; } -MAIN:{ - print_triangle(1); - print "\n"; - print "\n"; - print_triangle(2); - print "\n"; - print "\n"; - print_triangle(3); - print "\n"; - print "\n"; - print_triangle(4); - print "\n"; - print "\n"; - print_triangle(7); - print "\n"; - print "\n"; - print_triangle(10); - print "\n"; - print "\n"; - print_triangle(20); - print "\n"; - print "\n"; -} +__DATA__ +11, Line Eleven +1, Line one +9, Line Nine +13, Line Thirteen +2, Line two +6, Line Six +8, Line Eight +10, Line Ten +7, Line Seven +4, Line Four +14, Line Fourteen +3, Line three +15, Line Fifteen +5, Line Five -- cgit From 0cef48306ca4303778c36476c1debdb2eaccecf7 Mon Sep 17 00:00:00 2001 From: Adam Russell Date: Sun, 20 Jun 2021 17:01:56 -0400 Subject: solutions for challenge 117 --- challenge-117/adam-russell/blog.txt | 1 + challenge-117/adam-russell/blog1.txt | 1 + challenge-117/adam-russell/data | 14 +++++++++++ challenge-117/adam-russell/prolog/ch-1.p | 43 ++++++++++++++++++++++++++++++++ challenge-117/adam-russell/prolog/ch-2.p | 0 5 files changed, 59 insertions(+) create mode 100644 challenge-117/adam-russell/data delete mode 100644 challenge-117/adam-russell/prolog/ch-2.p diff --git a/challenge-117/adam-russell/blog.txt b/challenge-117/adam-russell/blog.txt index e69de29bb2..8d8366bb72 100644 --- a/challenge-117/adam-russell/blog.txt +++ b/challenge-117/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2021/06/20 diff --git a/challenge-117/adam-russell/blog1.txt b/challenge-117/adam-russell/blog1.txt index e69de29bb2..aae1cd44ec 100644 --- a/challenge-117/adam-russell/blog1.txt +++ b/challenge-117/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/prolog/2021/06/20 diff --git a/challenge-117/adam-russell/data b/challenge-117/adam-russell/data new file mode 100644 index 0000000000..5b9d9ab1ce --- /dev/null +++ b/challenge-117/adam-russell/data @@ -0,0 +1,14 @@ +11, Line Eleven +1, Line one +9, Line Nine +13, Line Thirteen +2, Line two +6, Line Six +8, Line Eight +10, Line Ten +7, Line Seven +4, Line Four +14, Line Fourteen +3, Line three +15, Line Fifteen +5, Line Five diff --git a/challenge-117/adam-russell/prolog/ch-1.p b/challenge-117/adam-russell/prolog/ch-1.p index e69de29bb2..3532217524 100644 --- a/challenge-117/adam-russell/prolog/ch-1.p +++ b/challenge-117/adam-russell/prolog/ch-1.p @@ -0,0 +1,43 @@ +:-initialization(main). + +check_and_read(10, [] ,_):- + !. +check_and_read(13, [], _):- + !. +check_and_read(44, [], _):- + !. +check_and_read(end_of_file, [], _):- + !. +check_and_read(Char, [Char|Chars], Stream):- + get_code(Stream, NextChar), + check_and_read(NextChar, Chars, Stream). + +read_data(Stream, []):- + at_end_of_stream(Stream). +read_data(Stream, [X|L]):- + \+ at_end_of_stream(Stream), + get_code(Stream, Char), + check_and_read(Char, Chars, Stream), + atom_codes(X, Chars), + read_data(Stream, L). + +line_numbers([], []). +line_numbers([N0,_|T], [N1|N]):- + number_atom(N1, N0), + line_numbers(T, N). + +missing(Contents, Missing):- + line_numbers(Contents, Numbers), + max_list(Numbers, Max), + min_list(Numbers, Min), + between(Min, Max, X), + \+ member(X, Numbers), + Missing = X. + +main:- + open('data', read, Stream), + read_data(Stream, Contents), + close(Stream), + missing(Contents, Missing), + format('Missing: ~d ~N', [Missing]), + halt. diff --git a/challenge-117/adam-russell/prolog/ch-2.p b/challenge-117/adam-russell/prolog/ch-2.p deleted file mode 100644 index e69de29bb2..0000000000 -- cgit