-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay9.java
87 lines (72 loc) · 1.53 KB
/
Day9.java
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
76
77
78
79
80
81
82
83
84
85
86
87
package com.sbaars.adventofcode.year17.days;
import com.sbaars.adventofcode.year17.Day2017;
public class Day9 extends Day2017 {
public Day9() {
super(9);
}
public static void main(String[] args) {
new Day9().printParts();
}
@Override
public Object part1() {
String input = day().trim();
int depth = 0;
int score = 0;
boolean inGarbage = false;
boolean skipNext = false;
for (char c : input.toCharArray()) {
if (skipNext) {
skipNext = false;
continue;
}
if (c == '!') {
skipNext = true;
continue;
}
if (inGarbage) {
if (c == '>') {
inGarbage = false;
}
continue;
}
if (c == '{') {
depth++;
score += depth;
} else if (c == '}') {
depth--;
} else if (c == '<') {
inGarbage = true;
}
}
return score;
}
@Override
public Object part2() {
String input = day().trim();
int garbageCount = 0;
boolean inGarbage = false;
boolean skipNext = false;
for (char c : input.toCharArray()) {
if (skipNext) {
skipNext = false;
continue;
}
if (c == '!') {
skipNext = true;
continue;
}
if (inGarbage) {
if (c == '>') {
inGarbage = false;
} else {
garbageCount++;
}
continue;
}
if (c == '<') {
inGarbage = true;
}
}
return garbageCount;
}
}