aboutsummaryrefslogtreecommitdiff
path: root/challenge-113
diff options
context:
space:
mode:
authorAaron Smith <aaronreidsmith@gmail.com>2021-05-22 15:59:02 -0500
committerAaron Smith <aaronreidsmith@gmail.com>2021-05-22 15:59:02 -0500
commit710ea66f4495240acd47f96d1e0236733e7ef8e6 (patch)
tree8514c974d0bc4790b800f0ed7387719f8dcd2089 /challenge-113
parent1720995a5127b6d99fad5334b6ba8983c06693b2 (diff)
downloadperlweeklychallenge-club-710ea66f4495240acd47f96d1e0236733e7ef8e6.tar.gz
perlweeklychallenge-club-710ea66f4495240acd47f96d1e0236733e7ef8e6.tar.bz2
perlweeklychallenge-club-710ea66f4495240acd47f96d1e0236733e7ef8e6.zip
Challenge 113 - Raku
Diffstat (limited to 'challenge-113')
-rw-r--r--challenge-113/aaronreidsmith/blog.txt1
-rw-r--r--challenge-113/aaronreidsmith/raku/ch-1.raku33
-rw-r--r--challenge-113/aaronreidsmith/raku/ch-2.raku73
3 files changed, 107 insertions, 0 deletions
diff --git a/challenge-113/aaronreidsmith/blog.txt b/challenge-113/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..9617638f2d
--- /dev/null
+++ b/challenge-113/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-113/
diff --git a/challenge-113/aaronreidsmith/raku/ch-1.raku b/challenge-113/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..20f68521aa
--- /dev/null
+++ b/challenge-113/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+subset PositiveInt of Int where * > 0;
+
+sub challenge(PositiveInt $N, PositiveInt $D) returns Int {
+ my $output = (1..^$N)
+ .race
+ .grep(*.contains($D))
+ .combinations(2..*)
+ .map(*.sum)
+ .any == $N;
+ $output.Bool.Int;
+}
+
+multi sub MAIN(PositiveInt $N, PositiveInt $D) {
+ say challenge($N, $D);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (25, 7, 0),
+ (24, 7, 1),
+ (97, 4, 1)
+ );
+
+ for @tests -> ($N, $D, $expected) {
+ is(challenge($N, $D), $expected);
+ }
+
+ done-testing;
+}
diff --git a/challenge-113/aaronreidsmith/raku/ch-2.raku b/challenge-113/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..52e58e8fab
--- /dev/null
+++ b/challenge-113/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,73 @@
+#!/usr/bin/env raku
+
+subset NodeValue of Str where { $_ ~~ /^<digit>$/ || $_ eq 'Nil' }
+
+class Node {
+ has Node $.left is rw = Nil;
+ has Node $.right is rw = Nil;
+ has Int $.value is rw = 0;
+}
+
+# Adapted from https://rosettacode.org/wiki/Visualize_a_tree#Raku
+sub format-tree(
+ Node $root,
+ Str :$indent = '',
+ :@mid = ('├─', '│ '),
+ :@end = ('└─', ' ')
+) returns Str {
+ sub visit(Node $node, *@pre) {
+ with $node {
+ |gather {
+ take @pre[0] ~ $node.value;
+ my @children = ($node.right, $node.left).grep(*.defined);
+ my $end = @children.end;
+ for @children.kv -> $_, $child {
+ when $end { take visit($child, (@pre[1] X~ @end)) }
+ default { take visit($child, (@pre[1] X~ @mid)) }
+ }
+ }
+ }
+ }
+ visit($root, $indent xx 2).join("\n");
+}
+
+sub build-tree(@array, $root is copy = Nil, Int $i = 0) returns Node {
+ if $i < @array.elems && @array[$i] ne 'Nil' {
+ $root = Node.new(value => @array[$i].Int);
+ $root.left = build-tree(@array, $root.left, 2 * $i + 1);
+ $root.right = build-tree(@array, $root.right, 2 * $i + 2);
+ }
+ $root;
+}
+
+sub challenge(Node $root is copy, @values = ()) returns Node {
+ sub extract-values(Node $root) returns Positional {
+ with $root {
+ ($root.value, |extract-values($root.left), |extract-values($root.right));
+ }
+ }
+
+ with $root {
+ my @node-values = @values.elems > 0 ?? @values !! extract-values($root);
+ $root.value = @node-values.grep(* != $root.value).sum;
+ challenge($root.left, @node-values);
+ challenge($root.right, @node-values);
+ }
+ $root;
+}
+
+multi sub MAIN(*@nodes where all(@nodes) ~~ NodeValue) {
+ my $root = build-tree(@nodes);
+ say format-tree(challenge($root));
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my $input-tree = build-tree(<1 2 3 4 Nil 5 6 Nil 7 Nil Nil>);
+ my $expected-tree = build-tree(<27 26 25 24 Nil 23 22 Nil 21 Nil Nil>);
+
+ is(format-tree(challenge($input-tree)), format-tree($expected-tree));
+
+ done-testing;
+}