-
Notifications
You must be signed in to change notification settings - Fork 0
/
d18_1.php
59 lines (46 loc) · 1.1 KB
/
d18_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
54
55
56
57
58
59
<?php
$vals = [];
$sound = 0;
// Instructions
$ins = file('input_d18.txt', FILE_IGNORE_NEW_LINES);
$i = 0;
while (1) {
$in = $ins[$i];
$parts = explode(' ', substr($in, 4));
if (!isset($vals[$parts[0]])) $vals[$parts[0]] = 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 'snd':
$sound = $vals[$parts[0]];
break;
case 'set':
$vals[$parts[0]] = $val;
break;
case 'add':
$vals[$parts[0]] += $val;
break;
case 'mul':
$vals[$parts[0]] *= $val;
break;
case 'mod':
$vals[$parts[0]] = $vals[$parts[0]] % $val;
break;
case 'rcv':
if ($sound !== 0) echo $sound;
break 2;
case 'jgz':
if ($vals[$parts[0]] > 0) {
$i += $val;
continue 2;
}
break;
}
$i++;
}