aboutsummaryrefslogtreecommitdiff
path: root/challenge-092/gugod
diff options
context:
space:
mode:
authorKang-min Liu <gugod@gugod.org>2020-12-23 10:20:44 +0900
committerKang-min Liu <gugod@gugod.org>2020-12-23 10:20:44 +0900
commitc654cae7cfef9b9b7224e42ba7d5999cd4274e2c (patch)
treedeec1bfaac4f76b7e0925271ffdd54502ef54072 /challenge-092/gugod
parentd8e1684b5a839403cffe589b123c8bd5937624ec (diff)
downloadperlweeklychallenge-club-c654cae7cfef9b9b7224e42ba7d5999cd4274e2c.tar.gz
perlweeklychallenge-club-c654cae7cfef9b9b7224e42ba7d5999cd4274e2c.tar.bz2
perlweeklychallenge-club-c654cae7cfef9b9b7224e42ba7d5999cd4274e2c.zip
a solution to pwc 092.2
Diffstat (limited to 'challenge-092/gugod')
-rw-r--r--challenge-092/gugod/raku/ch-2.raku44
1 files changed, 44 insertions, 0 deletions
diff --git a/challenge-092/gugod/raku/ch-2.raku b/challenge-092/gugod/raku/ch-2.raku
new file mode 100644
index 0000000000..d863448b48
--- /dev/null
+++ b/challenge-092/gugod/raku/ch-2.raku
@@ -0,0 +1,44 @@
+sub MAIN {
+
+ my @cases = (
+ (((1,4), (8,10)), (2,6)),
+ (((1,2), (3,7), (8,10)), (5,8)),
+ (((1,5), (7,9)), (10,11)),
+ (((1,2), (3,7), (8,10)), (2,8)),
+ );
+
+ for @cases -> $it {
+ my $S = $it[0];
+ my $N = $it[1];
+ my $S2 = insert-intervals($S, $N);
+ say $S2.gist ~ " <- " ~ $S.gist ~ " (+) " ~ $N.gist;
+ }
+}
+
+sub insert-intervals (@S is copy, $N) {
+ my $i = @S.first(-> $s { $s[0] <= $N[0] <= $s[1] }, :k);
+ my $j = @S.first(-> $s { $s[0] <= $N[1] <= $s[1] }, :k);
+
+ if $i.defined {
+ @S[$i] = (@S[$i][0], max(@S[$i][1], $N[1]));
+ }
+
+ if $j.defined {
+ @S[$j] = (@S[$j][0], max(@S[$j][1], $N[1]));
+
+ if $i.defined and @S[$i][0] <= @S[$j][0] <= @S[$i][1] {
+ @S[$i] = (@S[$i][0], @S[$j][1]);
+
+ for $i^..$j -> $x {
+ @S[$x] = Nil;
+ }
+ }
+ }
+
+ if none($i.defined, $j.defined) {
+ @S.push($N);
+ @S = @S.sort({ $^a[0] <=> $^b[0] });
+ }
+
+ return @S.grep({ .defined });
+}