advent_of_code_2020/day_3/part_1.php

29 lines
685 B
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) * 4;
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
$right = 0;
$down = 0;
$count_trees = 0;
while ($right < $length && $down < $height) {
if ($map[$down][$right] === '#') {
$count_trees++;
}
$right += 3;
++$down;
}
echo $count_trees;