advent_of_code_2020/day_8/part_1.php

45 lines
945 B
PHP
Raw Permalink Normal View History

2020-12-09 13:06:05 +01:00
<?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;
}
}