From 247d1176f91bedaf074985d3cb3fd4a84a8d752e Mon Sep 17 00:00:00 2001 From: Polgár Márton Date: Tue, 7 Mar 2023 22:25:47 +0100 Subject: 207th week by 2colours --- challenge-207/2colours/raku/ch-1-blead.raku | 46 +++++++++++++++++++++++++++++ challenge-207/2colours/raku/ch-1.raku | 13 ++++++++ challenge-207/2colours/raku/ch-2.raku | 21 +++++++++++++ 3 files changed, 80 insertions(+) create mode 100755 challenge-207/2colours/raku/ch-1-blead.raku create mode 100755 challenge-207/2colours/raku/ch-1.raku create mode 100755 challenge-207/2colours/raku/ch-2.raku diff --git a/challenge-207/2colours/raku/ch-1-blead.raku b/challenge-207/2colours/raku/ch-1-blead.raku new file mode 100755 index 0000000000..4e7ea3e6d8 --- /dev/null +++ b/challenge-207/2colours/raku/ch-1-blead.raku @@ -0,0 +1,46 @@ +#!/usr/bin/env raku +# WARNING: this solution only works with recent "nightly" builds of Rakudo! Not released yet, not even as 6.e preview! +use v6.*; + +sub either-char-class(@classes) { + my @word-choices = @classes.map: { + RakuAST::Regex::Group.new( + RakuAST::Regex::QuantifiedAtom.new( + atom => .&to-char-class, + quantifier => RakuAST::Regex::Quantifier::ZeroOrMore.new( + backtrack => RakuAST::Regex::Backtrack + ), + trailing-separator => False + ) + ) + }; + RakuAST::QuotedRegex.new( + match-immediately => False, + body => + RakuAST::Regex::Sequence.new( + RakuAST::Regex::InternalModifier::IgnoreCase.new, + RakuAST::Regex::Anchor::BeginningOfString.new, + RakuAST::Regex::Alternation.new(|@word-choices), + RakuAST::Regex::Anchor::EndOfString.new + ) + ).EVAL +} + +sub to-char-class(Str:D $input) { + my @elements = $input.comb.map: { + RakuAST::Regex::CharClassEnumerationElement::Character.new($_) + } + RakuAST::Regex::Assertion::CharClass.new( + RakuAST::Regex::CharClassElement::Enumeration.new(:@elements) + ) + +} + +constant @rows = ; +subset StrList of Str where /^ '(' ['"' $=[.*?] '"']* % ',' ')' $/; + +sub MAIN(Str $input) { + die 'Please provide a valid list of double-quoted (constant) strings.' unless $input.subst(/\s/, '', :g) ~~ StrList; + my Str() @words = $; + @words.grep(either-char-class(@rows)).say; +} \ No newline at end of file diff --git a/challenge-207/2colours/raku/ch-1.raku b/challenge-207/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..64f119bc1d --- /dev/null +++ b/challenge-207/2colours/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku + + +constant @rows = ; +subset StrList of Str where /^ '(' ['"' $=[.*?] '"']* % ',' ')' $/; + +sub MAIN(Str $input) { + die 'Please provide a valid list of double-quoted (constant) strings.' unless $input.subst(/\s/, '', :g) ~~ StrList; + my Str() @words = $; + @words + .grep: *.lc.comb.Set (<=) @rows.any.comb.Set andthen + .say +} \ No newline at end of file diff --git a/challenge-207/2colours/raku/ch-2.raku b/challenge-207/2colours/raku/ch-2.raku new file mode 100755 index 0000000000..e3b16bbbbd --- /dev/null +++ b/challenge-207/2colours/raku/ch-2.raku @@ -0,0 +1,21 @@ +#!/usr/bin/env raku + + +my token unsigned-integer { 0 | <[1..9]><[0..9]>* }; +my token integer { '-'? }; +# negative citation counts don't really make sense but that's what the task said /s +subset IntList of Str where /^ '(' * % ',' ')' $/; + +sub MAIN(Str $array) { + die 'Please supply a valid list of integers.' unless $array.subst(/\s/, '', :g) ~~ IntList; + my Int() @array = $; + @array + .sort + .reverse + .map: { + last unless $_ >= ++$; + 1 + } andthen + .sum + .say; +} -- cgit