aboutsummaryrefslogtreecommitdiff
path: root/challenge-262/2colours/php/ch-2.php
blob: bec91376946e4cbd6efb035ac1eff84c3e0878e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

const PARENS = ['(', ')'];
const SQUARE_BRACKETS = ['[', ']'];
echo '@ints = ';
$ints = json_decode(str_replace(PARENS, SQUARE_BRACKETS, fgets(STDIN)));
echo '$k = ';
$k = fgets(STDIN);
$result = 0;
foreach ($ints as $index => $value) {
    $needed_factor = intdiv($k, gmp_intval(gmp_gcd($index, $k)));
    $next_valid = ceil(($index + 1) / $needed_factor) * $needed_factor;
    $values_to_check = array_column(array_chunk(array_slice($ints, $next_valid), $needed_factor), 0);
    $result += @array_count_values($values_to_check)[$value];
}
echo $result;