diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-09-22 13:00:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-22 13:00:41 +0100 |
| commit | 3559e286f8071cc046ba270d39a7ce134cceaff1 (patch) | |
| tree | d8afc6facca4687c44cd74a95f4b6fa8fc023168 | |
| parent | 9d59984b8bb6b907da1608eae8e92894f8272de8 (diff) | |
| parent | 1c69072611e6a6d132cc322395a4cd9734154c20 (diff) | |
| download | perlweeklychallenge-club-3559e286f8071cc046ba270d39a7ce134cceaff1.tar.gz perlweeklychallenge-club-3559e286f8071cc046ba270d39a7ce134cceaff1.tar.bz2 perlweeklychallenge-club-3559e286f8071cc046ba270d39a7ce134cceaff1.zip | |
Merge pull request #2346 from jeongoon/master
[ch-079/jeongoon] Raku Solution added.
| -rw-r--r-- | challenge-079/jeongoon/raku/ch-1.raku | 5 | ||||
| -rw-r--r-- | challenge-079/jeongoon/raku/ch-2.raku | 81 |
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-079/jeongoon/raku/ch-1.raku b/challenge-079/jeongoon/raku/ch-1.raku new file mode 100644 index 0000000000..eeca1c3bf1 --- /dev/null +++ b/challenge-079/jeongoon/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/usr/bin/env raku +# -*- Mode: Raku -*- + +use v6.d; +our &MAIN=&say∘(*%1000000007)∘&sum∘(*.map(*.base(2).comb.grep(1).elems))∘(->$e {1..$e}); diff --git a/challenge-079/jeongoon/raku/ch-2.raku b/challenge-079/jeongoon/raku/ch-2.raku new file mode 100644 index 0000000000..b401839297 --- /dev/null +++ b/challenge-079/jeongoon/raku/ch-2.raku @@ -0,0 +1,81 @@ +#!/usr/bin/env raku +# -*- Mode: Raku; indent-tabs-mode: nil; coding: utf-8 -*- +# vim: set et ts=4 sw=4: + +use v6.d; + +# test with: +# raku jeongoon/ch-2.raku 2 1 4 1 2 5 # exmaple 1 +# raku jeongoon/ch-2.raku 3 1 3 1 1 5 # exmaple 2 +# raku jeongoon/ch-2.raku 1 2 3 4 3 1 # a mountain: no rain trapped + +unit sub MAIN ( *@T where { @T.elems > 0 and @T.all ~~ UInt } ); +#@T = @T».UInt; # unnecessary here. but note that thery are IntStr + +enum territory-stage +<terri-nothing terri-wall terri-mountain + terri-lake>; # terri-lake is not used here + +role lake { + method get-capacity( @territory = self.List ) { + # we need at least 3 data to build a water reservoir + @territory.elems < 3 and 0.return; + with @territory { + my $water-level = min( .head, .tail ); + my @t = .[ 1 .. * -2 ]; + @t.max > $water-level and 0.return; + ( [+] ( $water-level X- @t ) ).return; + } + } +} + +# we can do some brute-force for any combinations of region but +# let's scan the territory and find proper lake region +my $terri = class TerriInfo { + has ( $.left, $.right, $.start, + territory-stage $.stage ) is rw; + method has-valley ( $pos-x ) { $pos-x - $!start > 1 } +}.new( :left(0):start(0):stage(terri-nothing) ); + +my lake @lakes; + +for @T.kv -> $x, $h { + given $terri { + when .stage before terri-wall { + ( .left, .start, .stage ) = $h, $x, terri-wall; + } + when .stage before terri-mountain { + if .left <= $h { # no useful data on the left hand side + # -> update left boundary and position + ( .left, .start ) = $h, $x; + } + else { # has at lesast one lower height than left boundary + ( .right, .stage ) = $h, terri-mountain; + } + } + when .left < $h { # and .stage eq terri-mountain + # found a lake + @lakes.push( @T[ .start .. $x ] does lake ); + # right boundary is higher than left one and has valley + # -> start new scan with right boundary as new left boundary + $_ = TerriInfo.new( :start($x):left($h):stage( terri-wall ) ); + next; + } + default { # .left >= $h + # second-tallest height -> become a temporary right boundary + .right < $h and .right = $h; + + # otherwise we may have some water bucket here + # but still unsure until reach the right boundary + } + } + + LAST { + # check if any possble lake remained + .stage eq terri-mountain + and @lakes.push( @T[ .start .. * ] does lake ) with $terri; + } +} + +my &say-simple-answer = &say ∘ &sum ∘ ( *.map( *.get-capacity ) ); +say-simple-answer @lakes; |
