-
Notifications
You must be signed in to change notification settings - Fork 0
/
d09_1_2.php
61 lines (49 loc) · 1.03 KB
/
d09_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
<?php
$fh = fopen('input_d09.txt','r');
// Vars
$score = 0;
$ignored = '';
$no_garbage = '';
$ignore_next = false;
while ($char = fgetc($fh)) {
if ($ignore_next === true) {
$ignore_next = false;
continue;
}
if ($char !== '!') {
$ignored .= $char;
continue;
}
$ignore_next = true;
}
$is_garbage = false;
$garbage_char_count = 0;
for ($i = 0; $i < strlen($ignored); $i++) {
if ($ignored[$i] === '>') {
$is_garbage = false;
continue;
}
if ($is_garbage === true) {
$garbage_char_count++;
continue;
}
if ($ignored[$i] === '<') {
$is_garbage = true;
continue;
}
$no_garbage .= $ignored[$i];
}
// echo $ignored . PHP_EOL;
// echo $no_garbage . PHP_EOL;
$level = 0;
for ($i = 0; $i < strlen($no_garbage); $i++) {
if ($no_garbage[$i] === '{') {
$level++;
}
if ($no_garbage[$i] === '}') {
$score += $level;
$level--;
}
}
echo $score . PHP_EOL;
echo $garbage_char_count . PHP_EOL;