advent_of_code_2021/day_1/part_2.php

18 lines
385 B
PHP
Raw Permalink Normal View History

<?php
$numbers = array_map('intval', explode("\n", file_get_contents('input.txt')));
$current_slice = array_sum(array_slice($numbers, 0, 3));
$count = 0;
for ($i = 1, $l = count($numbers) - 3; $i <= $l; $i++) {
$new_slice = array_sum(array_slice($numbers, $i, 3));
if ($new_slice > $current_slice) {
$count++;
}
$current_slice = $new_slice;
}
echo $count;