advent_of_code_2020/day_3/part_2.php

44 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2020-12-03 12:28:02 +01:00
<?php
// je récupère la map de base
$map = array_map('str_split', explode("\n", file_get_contents('input.txt')));
$height = count($map);
$length = count($map[0]);
// je la multiplie pour qu'elle soit complète
$multiply_by = ceil($height / $length) * 8;
array_walk($map, static function(&$row) use ($multiply_by) {
$row = str_split(str_repeat(implode('', $row), $multiply_by));
});
$height = count($map);
$length = count($map[0]);
// je la visite et compte les arbres
$tests = [
['Right' => 1, 'down' => 1],
['Right' => 3, 'down' => 1],
['Right' => 5, 'down' => 1],
['Right' => 7, 'down' => 1],
['Right' => 1, 'down' => 2],
];
$result = 1;
foreach ($tests as $i => $test) {
$right = 0;
$down = 0;
$count_trees = 0;
while ($right < $length && $down < $height) {
if ($map[$down][$right] === '#') {
$count_trees++;
}
$right += $test['Right'];
$down += $test['down'];
}
// echo sprintf('Trouvé %u arbres sur le parcours %u', $count_trees, $i);
$result *= $count_trees;
}
echo $result;