-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday15.php
More file actions
26 lines (26 loc) · 803 Bytes
/
day15.php
File metadata and controls
26 lines (26 loc) · 803 Bytes
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
<?php
// You know we are in for a wild ride when we give PHP full system resources!
ini_set('memory_limit', '-1');
$input = array_map('intval', explode(",", file_get_contents($argv[1])));
// I wanted to use zero-based indexing but ran into issues.
// array_merge with a dummy value was way easier
$nums = array_flip(array_merge(['$'], $input));
$turn = $n = null;
$i = count($nums);
$update = function($i) {
// stupid closure visibility
global $nums, $n, $turn;
$n = empty($turn) ? 0 : $i - 1 - $turn;
// I learned about null coalescing when making sure ternaries existed
$turn = $nums[$n] ?? null;
$nums[$n] = $i;
};
while ($i <= 2020) {
$update($i++);
}
echo $n . PHP_EOL;
while ($i <= 30000000) {
$update($i++);
}
echo $n . PHP_EOL;
?>