-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-16.php
46 lines (35 loc) · 1.08 KB
/
day-16.php
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
$input = trim(file_get_contents(__DIR__ . '/input/day-16'));
$start = microtime(true);
$pattern = [0, 1, 0, -1];
// Part 1
$len = strlen($input);
$copy = str_split($input);
for ($p = 0; $p < 100; $p++) {
$output = [];
for ($i = $len; $i >= 1; $i--) {
$sum = 0;
for ($j = $len - 1, $to = $i - 1; $j >= $to; $j--) {
$delta = (int) (($j + 1) / $i) % 4;
$sum += $copy[$j] * $pattern[$delta];
}
$output[] = abs($sum) % 10;
}
$copy = array_reverse($output);
}
echo 'Part 1: ' . implode('', array_slice($copy, 0, 8)). PHP_EOL;
// Part 2
$offset = (int) substr($input, 0, 7);
$input = substr(str_repeat($input, 10000), $offset);
$input = str_split($input);
$len = count($input);
for ($p = 0; $p < 100; $p++) {
$result = [$len => 0];
for ($i = $len - 1; $i >= 0; $i--) {
$result[$i] = ($input[$i] + $result[$i + 1]) % 10;
}
$input = $result;
}
$input = array_reverse(array_slice($input, -8));
echo 'Part 2: ' . implode('', $input) . PHP_EOL;
echo 'Finished in ' . (microtime(true) - $start) . PHP_EOL;