-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-21.php
66 lines (55 loc) · 2.45 KB
/
day-21.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
$input = trim(file_get_contents('input/' . substr(basename(__FILE__), 0, -4)));
$start = microtime(true);
$input = explode("\n", $input);
$functions = [
'addr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] + $reg[$b]; },
'addi' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] + $b; },
'mulr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] * $reg[$b]; },
'muli' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] * $b; },
'banr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] & $reg[$b]; },
'bani' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] & $b; },
'borr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] | $reg[$b]; },
'bori' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] | $b; },
'setr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a]; },
'seti' => function($a, $b, $c, array &$reg) { $reg[$c] = $a; },
'gtir' => function($a, $b, $c, array &$reg) { $reg[$c] = $a > $reg[$b] ? 1 : 0; },
'gtri' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] > $b ? 1 : 0; },
'gtrr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] > $reg[$b] ? 1 : 0; },
'eqir' => function($a, $b, $c, array &$reg) { $reg[$c] = $a === $reg[$b] ? 1 : 0; },
'eqri' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] === $b ? 1 : 0; },
'eqrr' => function($a, $b, $c, array &$reg) { $reg[$c] = $reg[$a] === $reg[$b] ? 1 : 0; },
];
$ipValue = (int) explode(' ', array_shift($input))[1];
$program = [];
foreach ($input as $line) {
$part = explode(' ', $line);
$program[] = [$part[0], (int) $part[1], (int) $part[2], (int) $part[3]];
}
$steps = count($program);
$pointer = -1;
$registers = [0, 0, 0, 0, 0, 0];
$counter = 0;
$values = [];
while (++$pointer < $steps) {
$instr = $program[$pointer];
$registers[$ipValue] = $pointer;
$functions[$instr[0]]($instr[1], $instr[2], $instr[3], $registers);
$pointer = $registers[$ipValue];
if ($pointer === 28) {
if (!isset($values[$registers[3]])) {
$values[$registers[3]] = $counter;
} else {
break;
}
}
if ($counter++ > 50 && $pointer === 17) {
$registers[1] = floor($registers[2] / 256);
}
}
asort($values);
reset($values);
echo 'Answer 1: ' . key($values) . PHP_EOL;
end($values);
echo 'Answer 2: ' . key($values) . PHP_EOL;
echo 'Execution time: ' . (microtime(true) - $start) . PHP_EOL;