parent
fa2132ad25
commit
155c2905a8
2 changed files with 73 additions and 0 deletions
@ -0,0 +1,29 @@ |
||||
<?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; |
@ -0,0 +1,44 @@ |
||||
<?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; |
Loading…
Reference in new issue