diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-17 11:49:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-17 11:49:05 +0000 |
| commit | d5803f886f590cb601a705b0d69d1998f4ac73d8 (patch) | |
| tree | add91efbbae034d70a8ebde6ede8c5d9a3b22a49 | |
| parent | 8a62751194c2d9006efc21f750b1a632b6bbb838 (diff) | |
| parent | 530f0f415e3e31f3925a25e73f7a658c948cbb87 (diff) | |
| download | perlweeklychallenge-club-d5803f886f590cb601a705b0d69d1998f4ac73d8.tar.gz perlweeklychallenge-club-d5803f886f590cb601a705b0d69d1998f4ac73d8.tar.bz2 perlweeklychallenge-club-d5803f886f590cb601a705b0d69d1998f4ac73d8.zip | |
Merge pull request #3556 from pkmnx/branch-for-100
challenge #100, no. 2
| -rwxr-xr-x | challenge-100/pkmnx/raku/ch-2.raku | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-100/pkmnx/raku/ch-2.raku b/challenge-100/pkmnx/raku/ch-2.raku new file mode 100755 index 0000000000..efc91d62f7 --- /dev/null +++ b/challenge-100/pkmnx/raku/ch-2.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env raku + +# see usage: +# +# pk@pkx:~/stuff/raku/perlweeklychallenge-club/challenge-100/pkmnx/raku$ cat input.1 +# 1 +# 2,4 +# 6,4,9 +# 5,1,7,2 +# pk@pkx:~/stuff/raku/perlweeklychallenge-club/challenge-100/pkmnx/raku$ ./ch-2.raku < input.1 +# The minimum path sum from top to bottom: 1 + 2 + 4 + 1 = 8 +# +# [1] +# [2] 4 +# 6 [4] 9 +# 5 [1] 7 2 +# + +our $cch = []; +sub MAIN() { + my ( $mvls, $out, $lsz ) = ( [], [], 0 ); + ## take input + my $d = lines().map(*.comb(/\d+/)); + ## validate + for $d { my $nsz = $_.elems; die("bad input!") if $lsz > 0 && $nsz != $lsz + 1; $lsz = $nsz }; + ## run + f($d,0,0); + ## display - distinguish lowest val per line in "[]" and display sum + for (^$d) -> $y { + my $m = $cch[$y].pairs.min(*.value); + $out.push( (^$d[$y]).map({ my $v = $d[$y][$_]; $_ == $m.key ??($mvls.push($v) && "[$v]") !!$v }).join(" ") ); + } + say "The minimum path sum from top to bottom: " ~ $mvls.join(" + ") ~ " = " ~ $mvls.sum ~ "\n"; + say $out.join("\n"); +} + +sub f ($d, $y, $x) { + ! $d[$y][$x].defined && return Nil; + $cch[$y][$x].defined && return $cch[$y][$x]; + ## recur + my $ar = (0,1).map({f($d, $y +1, $x +$_)}).grep(*.defined); + ## set cache elem to min of path from bottom up + return $cch[$y][$x] = $d[$y][$x] + ( $ar.elems >0 ??$ar.min !!0 ); +} |
