-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.php
47 lines (40 loc) · 922 Bytes
/
9.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
<?php
$contents = trim(file_get_contents('9-input.txt'));
$group = false;
$garbage = false;
$ignore = false;
$level = 0;
$score = 0;
$chars = 0;
for ($i = 0; $i < strlen($input); $i++) {
if ($ignore) {
$ignore = false;
continue;
}
if ($input[$i] == '!') {
$ignore = true;
continue;
}
if ($garbage) {
if ($input[$i] == '>') {
$garbage = false;
} else { // Part B
$chars++;
}
} else {
if ($input[$i] == '{') {
$group = true;
$level++;
} elseif ($input[$i] == '<') {
$garbage = true;
} elseif ($input[$i] == '}') {
$group = false;
$score += $level;
$level--;
} elseif ($input[$i] == '>') {
$garbage = false;
}
}
}
echo 'Part A: ' . $score . PHP_EOL;
echo 'Part B: ' . $chars . PHP_EOL;