diff options
| author | Kang-min Liu <gugod@gugod.org> | 2020-11-11 08:59:14 +0900 |
|---|---|---|
| committer | Kang-min Liu <gugod@gugod.org> | 2020-11-11 08:59:14 +0900 |
| commit | 03f586bfec84af9d3953f7f6bef2f5fc8990ea35 (patch) | |
| tree | ddf36b961e931dd9e025fe08b24248ffa16d4fec /challenge-086/gugod | |
| parent | 2b18ed9e0f64655266754ae3fec10f3e6509b030 (diff) | |
| download | perlweeklychallenge-club-03f586bfec84af9d3953f7f6bef2f5fc8990ea35.tar.gz perlweeklychallenge-club-03f586bfec84af9d3953f7f6bef2f5fc8990ea35.tar.bz2 perlweeklychallenge-club-03f586bfec84af9d3953f7f6bef2f5fc8990ea35.zip | |
a second version of 086.1
which take linear space and linear time and should be faster than v1
when @N is long.
Diffstat (limited to 'challenge-086/gugod')
| -rw-r--r-- | challenge-086/gugod/raku/ch-1.raku | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/challenge-086/gugod/raku/ch-1.raku b/challenge-086/gugod/raku/ch-1.raku index 3033da1356..a1d3ea41fe 100644 --- a/challenge-086/gugod/raku/ch-1.raku +++ b/challenge-086/gugod/raku/ch-1.raku @@ -1,7 +1,18 @@ -my $A = @*ARGS.head; -my @N = @*ARGS[1..*]; +sub v1-exists-diff-by (Num $A, @N) { + @N.combinations(2).first(-> @combo { $A == abs([-] @combo) }); +} -my $yes = @N.combinations(2).first(-> @combo { $A == abs([-] @combo) }); +sub v2-exists-diff-by (Num $A, @N) { + my %pos; + @N.kv.map(-> $k, $v { %pos{$v}.push($k) }); + @N.first( + -> $n { + %pos{$n + $A}:exists or %pos{$n - $A}:exists + }); +} -say defined($yes) ?? 1 !! 0; +my $A = @*ARGS.head.Num; +my @N = @*ARGS[1..*].map(*.Num); + +say v2-exists-diff-by($A, @N) ?? 1 !! 0; |
