Skip to content

Commit

Permalink
Solution to challenge 44 task 2 in Raku by Noud
Browse files Browse the repository at this point in the history
  • Loading branch information
noudald committed Jan 25, 2020
1 parent 343ff1c commit 93f7379
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions challenge-044/noud/raku/ch-2.p6
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Make it $200
#
# You have only $1 left at the start of the week. You have been given an
# opportunity to make it $200. The rule is simple with every move you can
# either double what you have or add another $1. Write a script to help you get
# $200 with the smallest number of moves.

use experimental :cached;

sub best-strategy($cur-val, $target=200) is cached {
if ($cur-val == $target) {
return 0, [];
}
if ($cur-val > $target) {
return -1, [];
}

my ($d1, $strat1) = best-strategy(2 * $cur-val, $target);
my ($d2, $strat2) = best-strategy($cur-val + 1, $target);

if ($d1 < 0 and $d2 < 0) {
return -1, [];
}
if ($d1 < 0) {
return $d2 + 1, (|($strat2), 2);
}
if ($d2 < 0) {
return $d1 + 1, (|($strat1), 1);
}
if ($d1 < $d2) {
return $d1 + 1, (|($strat1), 1);
}
else {
return $d2 + 1, (|($strat2), 2);
}
}

sub print-strategy($target) {
my ($a, $b) = best-strategy(1, $target);
say "In $a moves we can get $target dollar:";
my $r = '';
for $b.list -> $s {
if ($s == 1) {
$r ~= "2*(";
} else {
$r ~= "1+(";
}
}
$r ~= '1' ~ (')' x $a);
say $r ~ "=$target";
}


print-strategy(10);
print-strategy(200);
print-strategy(4200);
print-strategy(12345);

0 comments on commit 93f7379

Please sign in to comment.