10 my ($i, $depth) = (0, 0);
12 $depth-- if substr($_, $i, 1) eq ']';
13 $depth++ if substr($_, $i, 1) eq '[';
17 next unless s/\G\[(\d+),(\d+)\]/X/;
18 my ($l, $r) = ($1, $2);
19 s/(\d+)([^\d]*X)/($1+$l).$2/e;
20 s/(X[^\d]*)(\d+)/$1.($2+$r)/e;
22 $modified++; $depth--;
26 $modified++ if s|\d{2,}|'['.int($&/2).','.int(($&+1)/2).']'|e;
31 sub magnitude { $_ = shift; 1 while s/\[(\d+),(\d+)\]/3*$1+2*$2/e; $_ }
33 use List::Util qw(reduce);
35 chomp (my @nums = <>);
37 say magnitude( reduce { add($a, $b) } @nums );
40 for my $i (0 .. $#nums) {
41 for my $j (0 .. $#nums) {
43 my $r = magnitude( add($nums[$i], $nums[$j]) );
44 $max = $r if ($max < $r);