diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-02-17 22:23:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-17 22:23:57 +0000 |
| commit | 833512d983db4860442b52c48e0edcc10c0dce92 (patch) | |
| tree | b4e7eee4c833a1beb00576d0e93e7c70a3505fb6 | |
| parent | 916fda5a82a161847f3cddd3b2d85e2abe083fd5 (diff) | |
| parent | b5fd3802512855d5b96356500ad37e6bfdfe5b2a (diff) | |
| download | perlweeklychallenge-club-833512d983db4860442b52c48e0edcc10c0dce92.tar.gz perlweeklychallenge-club-833512d983db4860442b52c48e0edcc10c0dce92.tar.bz2 perlweeklychallenge-club-833512d983db4860442b52c48e0edcc10c0dce92.zip | |
Merge pull request #3560 from pkmnx/branch-for-100
update for output errors and some formatting updates (centering)
| -rwxr-xr-x | challenge-100/pkmnx/raku/ch-2.raku | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/challenge-100/pkmnx/raku/ch-2.raku b/challenge-100/pkmnx/raku/ch-2.raku index efc91d62f7..d5964b00d6 100755 --- a/challenge-100/pkmnx/raku/ch-2.raku +++ b/challenge-100/pkmnx/raku/ch-2.raku @@ -8,37 +8,51 @@ # 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 +# 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 ); + my ( $mvls, $dm, $out, $lsz, $y, $x ) = ( [], [], [], 0, 0, 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 + + ## display - find path from cache + while ( $y < $d.elems ) { + my ($min, $mini) = (Inf, 0); + (0,1).map({ + if ( $cch[$y][$x +$_].defined ) { + my $v = $cch[$y][$x +$_]; if ( $v < $min ) { $min = $v; $mini = $_ } + } + }); + $x += $mini; + $dm.push( $x ); + $mvls.push( $d[$y][$x] ); + $y++; + } + ## center printf formatting & highlight each in "[]" and display sum + my $mxl = (^$d).map({ my $l = $d[$_]; my $chs = gather { for (^$l) { take $l[$_].chars +1 } }; $chs.sum }).max +4; 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(" ") ); + $out.push( (^$d[$y]).map({ my $v = $d[$y][$_]; $_ == $dm[$y] ??"[$v]" !!$v }).join(" ") ); } - say "The minimum path sum from top to bottom: " ~ $mvls.join(" + ") ~ " = " ~ $mvls.sum ~ "\n"; - say $out.join("\n"); + say "The minimum path sum from top to bottom: " ~ $mvls.join(" +") ~ " = " ~ $mvls.sum ~ "\n"; + for (^$out) { printf("%*s\n", ($mxl - $out[$_].chars)/2 +$out[$_].chars, $out[$_]) } } -sub f ($d, $y, $x) { +sub f ($d, $y, $x) { ! $d[$y][$x].defined && return Nil; - $cch[$y][$x].defined && return $cch[$y][$x]; + $cch[$y][$x].defined && return $cch[$y][$x]; ## no need to recalc same path ## 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 ); + return $cch[$y][$x] = $d[$y][$x] +( $ar.elems >0 ??$ar.min !!0 ); } |
