Skip to content

Instantly share code, notes, and snippets.

@hughsimpson
Created December 24, 2015 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughsimpson/eca3ddfcce69e56a5c97 to your computer and use it in GitHub Desktop.
Save hughsimpson/eca3ddfcce69e56a5c97 to your computer and use it in GitHub Desktop.
countdown numbers game (needs an input csv of the form `a,b,c,d` for the generating numbers
sub getPartial(Str $a, Rat $x, Rat $y) {
my @comps = $x + $y, $x *$y, $x - $y, $x ** $y, $x / $y;
my @agg = "($a " «~« <+ * - ** /> »~» " $y)";
return @comps Z=> @agg
}
sub comp(@ps, %a) {
if @ps.elems == 0 {return %a}
my ($h, @t) = @ps;
if %a.elems == 0 {return comp(@t, $h => $h.Str)}
sub foo($kv) {getPartial($kv.value, $kv.key.Rat, $h)}
my %b = flat map &foo, %a;
comp(@t, %b)
}
sub calcTargetX(Rat $target, Rat @is) {
say "foo $target";
my @ps = @is.permutations;
my %extras = %{};
my %h = gather {
for @ps -> @p {
say @p;
for comp(@p, %{}) -> $k, $v {
if %h{$k} {say "dupe, including { %h{$k} } "}
take $k => $v unless %h{$k}
};
say 'done';
}
};
say %h{$target};
}
sub processPath($path) {
my $numbs = open($path, :chomp).get() ~~ / <-[\d]>* [(\d+) <-[\d]>*]+ / ;
my Rat @numbs = $numbs.flat.map:{.Rat}
for @numbs { .say }
calcTargetX(532/1, @numbs);
say 'END'
}
sub MAIN(*@paths) {
for @paths -> $p { processPath($p) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment