diff options
| -rw-r--r-- | challenge-127/mark-anderson/raku/ch-2.raku | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/challenge-127/mark-anderson/raku/ch-2.raku b/challenge-127/mark-anderson/raku/ch-2.raku index ecda04171a..a36cd65ddd 100644 --- a/challenge-127/mark-anderson/raku/ch-2.raku +++ b/challenge-127/mark-anderson/raku/ch-2.raku @@ -1,23 +1,40 @@ #!/usr/bin/env raku use Test; -plan 2; +plan 4; -is-deeply conflict-intervals((1,4), (3,5), (6,8), (12, 13), (3,20)), [(3,5), (3,20)]; -is-deeply conflict-intervals((3,4), (5,7), (6,9), (10, 12), (13,15)), [(6,9),]; +is-deeply conflict-intervals((1,4), (3,5), (6,8), (12,13), (3,20)), [(3,5), (3,20)]; +is-deeply conflict-intervals((3,4), (5,7), (6,9), (10,12), (13,15)), [(6,9),]; -sub conflict-intervals(**@intervals) +is-deeply conflict-intervals-v2((1,4), (3,5), (6,8), (12,13), (3,20)), [(3,5), (3,20)]; +is-deeply conflict-intervals-v2((3,4), (5,7), (6,9), (10,12), (13,15)), [(6,9),]; + +sub conflict-intervals(**@i) { - gather + .Array given gather { - while @intervals > 1 + while @i > 1 { - if @intervals[1].head < @intervals[0].tail + if @i[1].head < @i[0].tail { - take @intervals[1]; + take @i[1]; } - @intervals.shift; + @i.shift; + } + } +} + +sub conflict-intervals-v2(**@i) +{ + @i .= sort; + + .Array given gather + { + while @i > 1 + { + take @i[1] if [<=] flat zip @i[0]<>, @i[1]<>; + shift @i; } - }.Array; + } } |
