3 ms·
SPOILERS BELOW One way to solve this: use strict; use warnings; use Algorithm::Combinatorics qw(variations_with_repetition); my @pm = ('+',
by jgrahamc 9y ago
SPOILERS BELOW
One way to solve this:
use strict;
use warnings;
use Algorithm::Combinatorics qw(variations_with_repetition);
my @pm = ('+', '-', '');
my $i = variations_with_repetition(\@pm, 8);
while (my $c = $i->next) {
my ($e, $j) = ('1', 2);
foreach my $a (@$c) {
$e .= "$a$j";
$j += 1;
}
print length($e)-9, " $e\n" if (eval($e) == 100);
}
Output:
$ perl gchq.pl
7 1+2+3-4+5+6+78+9
6 1+2+34-5+67-8+9
6 1+23-4+5+6+78-9
6 1+23-4+56+7+8+9
6 12+3+4+5-6-7+89
6 12+3-4+5+67+8+9
6 12-3-4+5-6+7+89
4 123+4-5+67-89
4 123+45-67+8-9
6 123-4-5-6-7+8-9
3 123-45-67+89
Or:
$ perl gchq.pl | sort -k1 -n | head -n1 | cut -d' ' -f1
3