diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-13 23:03:41 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-06-13 23:03:41 +0100 |
| commit | a068a20e0ba48863f3ccade315a393b8b54f4292 (patch) | |
| tree | 07f26118f1ab0e08a8432f2c04eae01501749599 /challenge-064 | |
| parent | f1300dc426a326c6b25f3001e172e53bca02786e (diff) | |
| download | perlweeklychallenge-club-a068a20e0ba48863f3ccade315a393b8b54f4292.tar.gz perlweeklychallenge-club-a068a20e0ba48863f3ccade315a393b8b54f4292.tar.bz2 perlweeklychallenge-club-a068a20e0ba48863f3ccade315a393b8b54f4292.zip | |
- Added solutions by Arne Sommer.
Diffstat (limited to 'challenge-064')
| -rw-r--r-- | challenge-064/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/ch-1.p6 | 72 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/ch-2.p6 | 17 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/matrix-3x3.txt | 3 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/matrix-zero.txt | 6 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/misupa | 72 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/misupa-direction | 83 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/misupa-random | 93 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/word-break | 17 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/word-break-short | 9 | ||||
| -rwxr-xr-x | challenge-064/arne-sommer/raku/word-break-turbo | 27 |
11 files changed, 400 insertions, 0 deletions
diff --git a/challenge-064/arne-sommer/blog.txt b/challenge-064/arne-sommer/blog.txt new file mode 100644 index 0000000000..76e55cad56 --- /dev/null +++ b/challenge-064/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/minimum-break.html diff --git a/challenge-064/arne-sommer/raku/ch-1.p6 b/challenge-064/arne-sommer/raku/ch-1.p6 new file mode 100755 index 0000000000..e8d9e70f1d --- /dev/null +++ b/challenge-064/arne-sommer/raku/ch-1.p6 @@ -0,0 +1,72 @@ +#! /usr/bin/env raku + +multi MAIN (:$verbose) +{ + MAIN(lines().join, :$verbose); +} + +multi MAIN ($file where $file.IO.e, :$verbose) +{ + MAIN($file.IO.lines().join, :$verbose); +} + +multi MAIN (Str $string is copy where ! $string.IO.e, :$verbose) +{ + subset PositiveIntZero of Int where * >= 0; + + my @matrix; + + while $string ~~ /"[" (.*?) "]" (.*)/ + { + $string = $1.Str.trim; + + say ":: Row: $0" if $verbose; + + my @values = $0.words>>.Int; + die "Illegal value in row $0" unless all(@values) ~~ PositiveIntZero; + + @matrix.push: @values; + } + + my @path = (); + my $best = Inf; + my $last_row = @matrix.elems - 1; + my $last_col = @matrix[0].elems -1; + + die "Not the same length on the rows" unless [==] @matrix>>.elems; + + traverse((@matrix[0;0],), 0, 0); + + for @path -> @current + { + say "$best ( { @current.join(" → ") } )"; + } + + sub traverse (@my-path, $x, $y) + { + if $x == $last_row && $y == $last_col + { + my $sum = @my-path.sum; + say ":: At the end with sum $sum and path: @my-path[]" if $verbose; + ( $best = $sum; @path = () ) if $sum < $best; + @path.push: @my-path if $sum == $best; + return; + } + + say ":: Currently at pos: [$x,$y] with path: @my-path[]" if $verbose; + + if $x < $last_row + { + my @new-path = @my-path; + @new-path.push: @matrix[$x+1;$y]; + traverse(@new-path, $x+1, $y); + } + if $y < $last_col + { + my @new-path = @my-path; + @new-path.push: @matrix[$x;$y+1]; + traverse(@new-path, $x, $y+1); + } + } + +} diff --git a/challenge-064/arne-sommer/raku/ch-2.p6 b/challenge-064/arne-sommer/raku/ch-2.p6 new file mode 100755 index 0000000000..67309e3c6a --- /dev/null +++ b/challenge-064/arne-sommer/raku/ch-2.p6 @@ -0,0 +1,17 @@ +#! /usr/bin/env raku + +sub MAIN ($string, *@words where @words.elems > 0, :$verbose) +{ + my $match = 0; + for @words.permutations -> @candidate + { + my $wordlist = @candidate.map({ "\"$_\"" }).join(", "); + say ": $wordlist" if $verbose; + if @candidate.join eq $string + { + say $wordlist; + $match++; + } + } + say "0" unless $match; +} diff --git a/challenge-064/arne-sommer/raku/matrix-3x3.txt b/challenge-064/arne-sommer/raku/matrix-3x3.txt new file mode 100755 index 0000000000..6c7666be82 --- /dev/null +++ b/challenge-064/arne-sommer/raku/matrix-3x3.txt @@ -0,0 +1,3 @@ +[ 1 2 3 ] +[ 4 5 6 ] +[ 7 8 9 ] diff --git a/challenge-064/arne-sommer/raku/matrix-zero.txt b/challenge-064/arne-sommer/raku/matrix-zero.txt new file mode 100755 index 0000000000..e5a12a9a6d --- /dev/null +++ b/challenge-064/arne-sommer/raku/matrix-zero.txt @@ -0,0 +1,6 @@ +[ 0 0 0 0 0 ] +[ 0 0 0 0 0 ] +[ 0 0 0 0 0 ] +[ 0 0 0 0 0 ] +[ 0 0 0 0 0 ] +[ 0 0 0 0 0 ] diff --git a/challenge-064/arne-sommer/raku/misupa b/challenge-064/arne-sommer/raku/misupa new file mode 100755 index 0000000000..e8d9e70f1d --- /dev/null +++ b/challenge-064/arne-sommer/raku/misupa @@ -0,0 +1,72 @@ +#! /usr/bin/env raku + +multi MAIN (:$verbose) +{ + MAIN(lines().join, :$verbose); +} + +multi MAIN ($file where $file.IO.e, :$verbose) +{ + MAIN($file.IO.lines().join, :$verbose); +} + +multi MAIN (Str $string is copy where ! $string.IO.e, :$verbose) +{ + subset PositiveIntZero of Int where * >= 0; + + my @matrix; + + while $string ~~ /"[" (.*?) "]" (.*)/ + { + $string = $1.Str.trim; + + say ":: Row: $0" if $verbose; + + my @values = $0.words>>.Int; + die "Illegal value in row $0" unless all(@values) ~~ PositiveIntZero; + + @matrix.push: @values; + } + + my @path = (); + my $best = Inf; + my $last_row = @matrix.elems - 1; + my $last_col = @matrix[0].elems -1; + + die "Not the same length on the rows" unless [==] @matrix>>.elems; + + traverse((@matrix[0;0],), 0, 0); + + for @path -> @current + { + say "$best ( { @current.join(" → ") } )"; + } + + sub traverse (@my-path, $x, $y) + { + if $x == $last_row && $y == $last_col + { + my $sum = @my-path.sum; + say ":: At the end with sum $sum and path: @my-path[]" if $verbose; + ( $best = $sum; @path = () ) if $sum < $best; + @path.push: @my-path if $sum == $best; + return; + } + + say ":: Currently at pos: [$x,$y] with path: @my-path[]" if $verbose; + + if $x < $last_row + { + my @new-path = @my-path; + @new-path.push: @matrix[$x+1;$y]; + traverse(@new-path, $x+1, $y); + } + if $y < $last_col + { + my @new-path = @my-path; + @new-path.push: @matrix[$x;$y+1]; + traverse(@new-path, $x, $y+1); + } + } + +} diff --git a/challenge-064/arne-sommer/raku/misupa-direction b/challenge-064/arne-sommer/raku/misupa-direction new file mode 100755 index 0000000000..25dc785e00 --- /dev/null +++ b/challenge-064/arne-sommer/raku/misupa-direction @@ -0,0 +1,83 @@ +#! /usr/bin/env raku + +multi MAIN (:$verbose, :$arrows) +{ + MAIN(lines().join, :$verbose, :$arrows); +} + +multi MAIN ($file where $file.IO.e, :$verbose, :$arrows) +{ + MAIN($file.IO.lines().join, :$verbose, :$arrows); +} + +multi MAIN (Str $string is copy where ! $string.IO.e, :$verbose, :$arrows) +{ + subset PositiveIntZero of Int where * >= 0; + + my @matrix; + + while $string ~~ /"[" (.*?) "]" (.*)/ + { + $string = $1.Str.trim; + + say ":: Row: $0" if $verbose; + + my @values = $0.words>>.Int; + die "Illegal value in row $0" unless all(@values) ~~ PositiveIntZero; + + @matrix.push: @values; + } + + my @path = (); + my @arrows = (); + my $best = Inf; + my $last_row = @matrix.elems - 1; + my $last_col = @matrix[0].elems -1; + + die "Not the same length on the rows" unless [==] @matrix>>.elems; + + traverse((@matrix[0;0],), 0, 0, ""); + + if $arrows + { + for ^@path -> $index + { + say "$best ({ (roundrobin @(@path[$index]), @arrows[$index].comb).flat })"; + } + + } + else + { + for @path -> @current + { + say "$best ( { @current.join(" → ") } )"; + } + } + + sub traverse (@my-path, $x, $y, $arrows) + { + if $x == $last_row && $y == $last_col + { + my $sum = @my-path.sum; + say ":: At the end with sum $sum and path: @my-path[]" if $verbose; + ( $best = $sum; @path = (); @arrows = () ) if $sum < $best; + ( @path.push: @my-path; @arrows.push: $arrows) if $sum == $best; + return; + } + + say ":: Currently at pos: [$x,$y] with path: @my-path[]" if $verbose; + + if $x < $last_row + { + my @new-path = @my-path; + @new-path.push: @matrix[$x+1;$y]; + traverse(@new-path, $x+1, $y, $arrows ~ "↓"); + } + if $y < $last_col + { + my @new-path = @my-path; + @new-path.push: @matrix[$x;$y+1]; + traverse(@new-path, $x, $y+1, $arrows ~ "→"); + } + } +} diff --git a/challenge-064/arne-sommer/raku/misupa-random b/challenge-064/arne-sommer/raku/misupa-random new file mode 100755 index 0000000000..c498a88ba1 --- /dev/null +++ b/challenge-064/arne-sommer/raku/misupa-random @@ -0,0 +1,93 @@ +#! /usr/bin/env raku + +multi MAIN ("random", :$verbose, :$arrows, :$cols = 3, :$rows = 3, :$low = 0, :$high = 9) +{ + my $matrix; + $matrix ~= "[ { ($low .. $high).pick($cols).join(" ") } ]" for ^$rows; + + say "Matrix: $matrix"; + + MAIN($matrix, :$verbose, :$arrows); +} + +multi MAIN (:$verbose, :$arrows) +{ + MAIN(lines().join, :$verbose, :$arrows); +} + +multi MAIN ($file where $file.IO.e, :$verbose, :$arrows) +{ + MAIN($file.IO.lines().join, :$verbose, :$arrows); +} + +multi MAIN (Str $string is copy where ! $string.IO.e, :$verbose, :$arrows) +{ + subset PositiveIntZero of Int where * >= 0; + + my @matrix; + + while $string ~~ /"[" (.*?) "]" (.*)/ + { + $string = $1.Str.trim; + + say ":: Row: $0" if $verbose; + + my @values = $0.words>>.Int; + die "Illegal value in row $0" unless all(@values) ~~ PositiveIntZero; + + @matrix.push: @values; + } + + my @path = (); + my @arrows = (); + my $best = Inf; + my $last_row = @matrix.elems - 1; + my $last_col = @matrix[0].elems -1; + + die "Not the same length on the rows" unless [==] @matrix>>.elems; + + traverse((@matrix[0;0],), 0, 0, ""); + + if $arrows + { + for ^@path -> $index + { + say "$best ({ (roundrobin @(@path[$index]), @arrows[$index].comb).flat })"; + } + + } + else + { + for @path -> @current + { + say "$best ( { @current.join(" → ") } )"; + } + } + + sub traverse (@my-path, $x, $y, $arrows) + { + if $x == $last_row && $y == $last_col + { + my $sum = @my-path.sum; + say ":: At the end with sum $sum and path: @my-path[]" if $verbose; + ( $best = $sum; @path = (); @arrows = () ) if $sum < $best; + ( @path.push: @my-path; @arrows.push: $arrows) if $sum == $best; + return; + } + + say ":: Currently at pos: [$x,$y] with path: @my-path[]" if $verbose; + + if $x < $last_row + { + my @new-path = @my-path; + @new-path.push: @matrix[$x+1;$y]; + traverse(@new-path, $x+1, $y, $arrows ~ "↓"); + } + if $y < $last_col + { + my @new-path = @my-path; + @new-path.push: @matrix[$x;$y+1]; + traverse(@new-path, $x, $y+1, $arrows ~ "→"); + } + } +} diff --git a/challenge-064/arne-sommer/raku/word-break b/challenge-064/arne-sommer/raku/word-break new file mode 100755 index 0000000000..67309e3c6a --- /dev/null +++ b/challenge-064/arne-sommer/raku/word-break @@ -0,0 +1,17 @@ +#! /usr/bin/env raku + +sub MAIN ($string, *@words where @words.elems > 0, :$verbose) +{ + my $match = 0; + for @words.permutations -> @candidate + { + my $wordlist = @candidate.map({ "\"$_\"" }).join(", "); + say ": $wordlist" if $verbose; + if @candidate.join eq $string + { + say $wordlist; + $match++; + } + } + say "0" unless $match; +} diff --git a/challenge-064/arne-sommer/raku/word-break-short b/challenge-064/arne-sommer/raku/word-break-short new file mode 100755 index 0000000000..9ffd12f0df --- /dev/null +++ b/challenge-064/arne-sommer/raku/word-break-short @@ -0,0 +1,9 @@ +#! /usr/bin/env raku + +sub MAIN ($string, *@words where @words.elems > 0) +{ + for @words.permutations -> @candidate + { + say @candidate.map({ "\"$_\"" }).join(", ") if @candidate.join eq $string; + } +} diff --git a/challenge-064/arne-sommer/raku/word-break-turbo b/challenge-064/arne-sommer/raku/word-break-turbo new file mode 100755 index 0000000000..c07b015ce9 --- /dev/null +++ b/challenge-064/arne-sommer/raku/word-break-turbo @@ -0,0 +1,27 @@ +#! /usr/bin/env raku + +sub MAIN ($string, *@words where @words.elems > 0, :$verbose) +{ + my $string-length = $string.chars; + my $shortest-word = @words>>.chars.min; + + say ": SL: $string-length SW: $shortest-word" if $verbose; + + my @words2 = (@words xx $string-length div $shortest-word).flat.sort({ $^a.chars <=> $^b.chars || $^a cmp $^b }); + + say ": Words: @words2[]" if $verbose; + + my $match = 0; + for @words2.combinations(1..3).unique(:with(&[eqv])).map({ .elems > 1 ?? | .permutations !! $_ }).unique(:with(&[eqv])) -> @candidate + { + my $wordlist = @candidate.map({ "\"$_\"" }).join(", "); + + say ": Candidate: $wordlist" if $verbose; + if @candidate.join eq $string + { + say $wordlist; + $match++; + } + } + say "0" unless $match; +} |
