-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-7.php
51 lines (38 loc) · 1.12 KB
/
day-7.php
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
require_once __DIR__ . '/utils.php';
$input = trim(file_get_contents(__DIR__ . '/input/day-7'));
$input = explode(',', $input);
$start = microtime(true);
$part1 = 0;
foreach (permutations([0,1,2,3,4]) as $comb) {
$setting = str_split($comb);
$signal = 0;
for ($i = 0; $i < 5; $i++) {
$comp = new IntCodeParser($input, $setting[$i], $signal);
$signal = $comp->output();
}
if ($signal > $part1) $part1 = $signal;
}
$part2 = 0;
foreach (permutations([5,6,7,8,9]) as $comb) {
$phase = str_split($comb);
$amps = [];
$signal = 0;
for ($i = 0; $i < 5; $i++) {
$amps[$i] = new IntCodeParser($input, $phase[$i]);
}
while (true) {
for ($i = 0; $i < 5; $i++) {
$amps[$i]->input($signal);
$output = $amps[$i]->output();
if ($output === null) {
if ($signal > $part2) $part2 = $signal;
break 2;
}
$signal = $output;
}
}
}
echo 'Part 1: ' . $part1 . PHP_EOL;
echo 'Part 2: ' . $part2 . PHP_EOL;
echo 'Finished in ' . (microtime(true) - $start) . PHP_EOL;