aboutsummaryrefslogtreecommitdiff
path: root/challenge-192/2colours/raku/ch-2.raku
blob: 3a66a530453839db074b5e3dea8324ecd88f6611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env raku

subset Choice of List where ((Int, Int), Int);

sub next-placement(@normal-values) {
  @normal-values.pairs.grep(*.value != 0).rotor(2 => -1).grep({ [!=] $_>>.value>>.sign }) andthen .so ?? .min({ [R-] $_>>.key }) !! Nil
}

multi dist-steps(@ where { .sum !%% .elems } ) { -1 }
multi dist-steps(@list) {
  my $average = @list.sum div +@list;
  my @norm-list = @list >>->> $average;
  my @costs = gather while @norm-list.&next-placement -> $endpoints {
    my $moved-amount = $endpoints.>>.value>>.abs.min;
    my $distance = [R-] $endpoints>>.key;
    take $moved-amount * $distance;
    $_ -= $moved-amount * .sign for $endpoints>>.value;
  };
  @costs.sum
}

my token natural-number { 0 | <[1..9]> <[0..9]>* }
subset NatList of Str where /^ '(' <natural-number>* % [\s* ',' \s*] ')' $/;
sub MAIN($n) {
  die 'Please provide a valid list of natural numbers' unless $n ~~ NatList;
  my @list = $<natural-number>>>.Int;
  @list.&dist-steps.say;
}