-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-8.php
41 lines (33 loc) · 1.11 KB
/
day-8.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
<?php
$input = trim(file_get_contents(__DIR__ . '/input/day-8'));
$start = microtime(true);
$width = 25;
$height = 6;
$strLen = strlen($input);
$numLayers = $strLen / ($width * $height);
$digitsPerLayer = $strLen / $numLayers;
$map = [];
$image = [];
for ($i = 0; $i < $numLayers; $i++) {
$chunk = substr($input, $i * $digitsPerLayer, $digitsPerLayer);
$chars = count_chars($chunk, 1);
$map[$i] = $chars[48] ?? 0;
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
if (($image[$y][$x] ?? 2) !== 2) continue;
$image[$y][$x] = (int) $chunk[$y * $width + $x];
}
}
}
$layerWithMinZero = array_search(min($map), $map);
$layer = substr($input, $layerWithMinZero * $digitsPerLayer, $digitsPerLayer);
$chars = count_chars($layer, 1);
echo 'Part 1: ' . ($chars[49] * $chars[50]) . PHP_EOL;
echo 'Part 2: ' . PHP_EOL;
foreach ($image as $y => $line) {
foreach ($line as $x => $pixel) {
echo $pixel == 1 ? '#' : ' ';
}
echo PHP_EOL;
}
echo 'Finished in ' . (microtime(true) - $start) . PHP_EOL;