From 5b5e2c158c109d45f7066aad0f0484acf7de847f Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Mon, 21 Jun 2021 00:06:16 +0100 Subject: - Added solutions by Colin Crain. --- challenge-117/colin-crain/perl/ch-1.pl | 76 +++++++++++++++++++++ challenge-117/colin-crain/perl/ch-2.pl | 114 +++++++++++++++++++++++++++++++ challenge-117/colin-crain/raku/ch-1.raku | 54 +++++++++++++++ challenge-117/colin-crain/raku/ch-2.raku | 58 ++++++++++++++++ 4 files changed, 302 insertions(+) create mode 100644 challenge-117/colin-crain/perl/ch-1.pl create mode 100644 challenge-117/colin-crain/perl/ch-2.pl create mode 100644 challenge-117/colin-crain/raku/ch-1.raku create mode 100644 challenge-117/colin-crain/raku/ch-2.raku (limited to 'challenge-117') diff --git a/challenge-117/colin-crain/perl/ch-1.pl b/challenge-117/colin-crain/perl/ch-1.pl new file mode 100644 index 0000000000..c9ac472cdf --- /dev/null +++ b/challenge-117/colin-crain/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl +# +# missing-in-sequence.pl +# +# Missing Row +# Submitted by: Mohammad S Anwar +# 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. +# +# 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 +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +use warnings; +use strict; +use utf8; +use feature ":5.26"; +use feature qw(signatures); +no warnings 'experimental::signatures'; +use List::Util qw( max ); + +my %lookup = map { split /,/, $_, 2 } <>; + +my @missing = grep { say $_; ! exists $lookup{$_} } (1..max(keys %lookup)); + +## output +if (my $count = scalar @missing) { + say $count, ($count == 1 + ? " line is " + : " lines are "), "missing:"; + say "line $_" for @missing; +} +else { + say "all lines accounted for!"; +} + + +=cut +The Data File, 'missing.txt': + +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 +14, Line Fourteen +3, Line Three +15, Line Fifteen +5, Line Five + +The Result: + +2 lines are missing: +line 4 +line 12 diff --git a/challenge-117/colin-crain/perl/ch-2.pl b/challenge-117/colin-crain/perl/ch-2.pl new file mode 100644 index 0000000000..8de28bcc8b --- /dev/null +++ b/challenge-117/colin-crain/perl/ch-2.pl @@ -0,0 +1,114 @@ +#!/Users/colincrain/perl5/perlbrew/perls/perl-5.32.0/bin/perl +# +# triangular-tour.pl +# +# +# Find Possible Paths +# Submitted by: E. Choroba +# You are given size of a triangle. +# +# Write a script to find all possible paths from top to the bottom right +# corner. +# +# In each step, we can either move horizontally to the right (H), or +# move downwards to the left (L) or right (R). +# +# BONUS: Try if it can handle triangle of size 10 or 20. +# +# Example 1: +# Input: $N = 2 +# +# S +# / \ +# / _ \ +# /\ /\ +# /__\ /__\ E +# +# Output: RR, LHR, LHLH, LLHH, RLH, LRH +# Example 2: +# Input: $N = 1 +# +# S +# / \ +# / _ \ E +# +# Output: R, LH +# +# method: +# +# verified to n=13: +# +# A006318 Large Schröder numbers (or large Schroeder numbers, or big +# Schroeder numbers). +# 1, 2, 6, 22, 90, 394, 1806, 8558, 41586, 206098, 1037718, 5293446, +# 27297738, 142078746, 745387038, 3937603038, 20927156706, 111818026018, +# 600318853926, 3236724317174, 17518619320890, 95149655201962, +# 518431875418926, 2832923350929742, 15521467648875090 +# +# tp(n) = S(n+1) +# +# tp(10) = 1_037_718 +# tp(20) = 17_518_619_320_890 +# + +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +use warnings; +use strict; +use utf8; +use feature ":5.26"; +use feature qw(signatures); +no warnings 'experimental::signatures'; + +my $tri_size = shift // 10; +my $mat = [ map { ['L' x $_] } (0..$tri_size) ]; + +for my $pos (1..$tri_size) { ## horz position in the tri + my @next; + for my $level ($pos..$tri_size) { ## tri level + push $next[$level]->@*, (map { $_ . 'L' } $next[$level-1]->@*) + if defined $next[$level-1]; + push $next[$level]->@*, (map { $_ . 'R' } $mat->[$level-1]->@* ); + push $next[$level]->@*, (map { $_ . 'H' } $mat->[$level]->@*) + } + $mat = \@next; +} + +say "results: ", scalar $mat->[-1]->@*;; +say $_ for $mat->[-1]->@*; + + + +=cut +[colincrain@boris:~/Code/PWC]$ time perl triangular-tour-3.pl 10 +result: +1037718 + +real 0m0.916s +user 0m0.842s +sys 0m0.070s +[colincrain@boris:~/Code/PWC]$ time perl triangular-tour-3.pl 11 +result: +5293446 + +real 0m4.694s +user 0m4.311s +sys 0m0.377s +[colincrain@boris:~/Code/PWC]$ time perl triangular-tour-3.pl 12 +result: +27297738 + +real 0m24.150s +user 0m22.166s +sys 0m1.974s +[colincrain@boris:~/Code/PWC]$ time perl triangular-tour-2.pl 13 +result: +142078746 + +real 6m52.523s +user 2m42.176s +sys 2m48.754s <-- grinding diff --git a/challenge-117/colin-crain/raku/ch-1.raku b/challenge-117/colin-crain/raku/ch-1.raku new file mode 100644 index 0000000000..c3b0614a1c --- /dev/null +++ b/challenge-117/colin-crain/raku/ch-1.raku @@ -0,0 +1,54 @@ +#!/usr/bin/env perl6 +# +# +# missing-in-sequence.raku +# +# Missing Row +# Submitted by: Mohammad S Anwar +# 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. +# +# 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 +# +# +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + + +unit sub MAIN ( $file = './missing.txt' ) ; + +my %set = $file.IO + .lines + .map( *.comb: /\d+/, 1 ) + .Set; + +my @missing = (1..%set.keys.max({$_.Int})).grep: { $_.Str ∉ %set } ; + +if @missing.elems -> $count { + say $count, ($count == 1 + ?? " line is " + !! " lines are "), "missing:"; + "line $_".say for @missing; +} +else { + say "all lines accounted for!"; +} + + + + diff --git a/challenge-117/colin-crain/raku/ch-2.raku b/challenge-117/colin-crain/raku/ch-2.raku new file mode 100644 index 0000000000..fc68e36c46 --- /dev/null +++ b/challenge-117/colin-crain/raku/ch-2.raku @@ -0,0 +1,58 @@ +#!/usr/bin/env perl6 +# +# +# triangular-tour.raku +# +# Find Possible Paths +# Submitted by: E. Choroba +# You are given size of a triangle. +# +# Write a script to find all possible paths from top to the bottom right +# corner. +# +# In each step, we can either move horizontally to the right (H), or +# move downwards to the left (L) or right (R). +# +# BONUS: Try if it can handle triangle of size 10 or 20. +# +# Example 1: +# Input: $N = 2 +# +# S +# / \ +# / _ \ +# /\ /\ +# /__\ /__\ E +# +# Output: RR, LHR, LHLH, LLHH, RLH, LRH +# Example 2: +# Input: $N = 1 +# +# S +# / \ +# / _ \ E +# +# Output: R, LH +# +# © 2021 colin crain +## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## + + +unit sub MAIN (Int $tri-size = 5) ; + +my @mat = (0..$tri-size).map({ ('L' x $_ ).Array }); + +for 1..$tri-size -> $pos { + my @next; + for $pos..$tri-size -> $level { + @next[$level-1].defined && + @next[$level].push: |@next[$level-1].map({ $_ ~ 'L' }); + @next[$level].push: |@mat[$level-1].map({ $_ ~ 'R' }); + @next[$level].push: |@mat[$level].map({ $_ ~ 'H' }); + } + @mat = @next; +} + +say "results: ", @mat[*-1].elems;; +.say for |@mat[*-1]; + -- cgit