-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd08_1_2.php
76 lines (56 loc) · 1.66 KB
/
d08_1_2.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
67
68
69
70
71
72
73
74
75
<?php
$mems = [];
$max = 0;
$highest = 0;
$fh = fopen('input_d08.txt','r');
while ($line = fgets($fh, 100)) {
$parts = explode('if', $line);
// instruction part
$instructions = explode(' ', trim($parts[0]));
$ins_target = isset($mems[$instructions[0]]) ? $mems[$instructions[0]] : 0;
$ins_value = intval($instructions[2]);
// condition part
$conditions = explode(' ', trim($parts[1]));
$con_target = isset($mems[$conditions[0]]) ? $mems[$conditions[0]] : 0;
$con_value = intval($conditions[2]);
$condition_met = false;
switch (trim($conditions[1])) {
case '>':
$condition_met = $con_target > $con_value;
break;
case '<':
$condition_met = $con_target < $con_value;
break;
case '>=':
$condition_met = $con_target >= $con_value;
break;
case '<=':
$condition_met = $con_target <= $con_value;
break;
case '==':
$condition_met = $con_target == $con_value;
break;
case '!=':
$condition_met = $con_target != $con_value;
break;
}
if ($condition_met) {
switch (trim($instructions[1])) {
case 'inc':
$ins_target += $ins_value;
break;
case 'dec':
$ins_target -= $ins_value;
break;
}
}
if ($ins_target > $highest) $highest = $ins_target;
$mems[$instructions[0]] = $ins_target;
}
foreach ($mems as $mem => $val) {
if ($val > $max) {
$max = $val;
}
}
echo 'Max value is: ' . $max;
echo 'Highest value is: ' . $highest;