45 lines
945 B
PHP
45 lines
945 B
PHP
<?php
|
|
|
|
$instructions = explode("\n", file_get_contents('input.txt'));
|
|
|
|
$acc = 0;
|
|
$used_indexes = [];
|
|
$current_index = 0;
|
|
|
|
while (true) {
|
|
if (in_array($current_index, $used_indexes, true)) {
|
|
echo $acc;
|
|
exit;
|
|
}
|
|
$used_indexes[] = $current_index;
|
|
$current_op = $instructions[$current_index];
|
|
[$command, $value] = explode(' ', $current_op);
|
|
|
|
if ($command === 'nop') {
|
|
$current_index++;
|
|
continue;
|
|
}
|
|
|
|
$math_op = $value[0];
|
|
$math_value = substr($value, 1);
|
|
|
|
if ($command === 'acc') {
|
|
if ($math_op === '+') {
|
|
$acc += (int)$math_value;
|
|
} else {
|
|
$acc -= (int)$math_value;
|
|
}
|
|
$current_index++;
|
|
continue;
|
|
}
|
|
|
|
if ($command === 'jmp') {
|
|
if ($math_op === '+') {
|
|
$current_index += (int)$math_value;
|
|
} else {
|
|
$current_index -= (int)$math_value;
|
|
}
|
|
continue;
|
|
}
|
|
}
|