-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd23_1.php
53 lines (40 loc) · 971 Bytes
/
d23_1.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
<?php
$vals = [];
// Instructions
$ins = file('input_d23.txt', FILE_IGNORE_NEW_LINES);
$mul_count = 0;
$i = 0;
while (1) {
if ($i >= count($ins)) break;
$in = $ins[$i];
$parts = explode(' ', substr($in, 4));
if (!isset($vals[$parts[0]])) $vals[$parts[0]] = intval($parts[0]);
$val = 0;
if (count($parts) > 1) {
if (!is_numeric($parts[1])) {
$val = $vals[$parts[1]];
} else {
$val = intval($parts[1]);
}
}
switch(substr($in, 0, 3)) {
case 'set':
$vals[$parts[0]] = $val;
break;
case 'sub':
$vals[$parts[0]] -= $val;
break;
case 'mul':
$vals[$parts[0]] *= $val;
$mul_count++;
break;
case 'jnz':
if ($vals[$parts[0]] != 0) {
$i += $val;
continue 2;
}
break;
}
$i++;
}
var_dump($mul_count);