diff options
| -rw-r--r-- | challenge-117/adam-russell/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-117/adam-russell/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-117/adam-russell/data | 14 | ||||
| -rw-r--r-- | challenge-117/adam-russell/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-117/adam-russell/perl/ch-2.pl | 112 | ||||
| -rw-r--r-- | challenge-117/adam-russell/prolog/ch-1.p | 43 |
6 files changed, 211 insertions, 0 deletions
diff --git a/challenge-117/adam-russell/blog.txt b/challenge-117/adam-russell/blog.txt new file mode 100644 index 0000000000..8d8366bb72 --- /dev/null +++ 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 new file mode 100644 index 0000000000..aae1cd44ec --- /dev/null +++ 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/perl/ch-1.pl b/challenge-117/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..bd250abf34 --- /dev/null +++ b/challenge-117/adam-russell/perl/ch-1.pl @@ -0,0 +1,40 @@ +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; + } +} + +MAIN:{ + my @line_numbers; + while(<DATA>){ + chomp; + m/([0-9]+),.*/; + push @line_numbers, $1; + } + my $missing = find_missing(@line_numbers); + print "$missing\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 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..999acf07a8 --- /dev/null +++ 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"; +} 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..3532217524 --- /dev/null +++ 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. |
