-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.java
61 lines (52 loc) · 1.29 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
package com.adventofcode.advent2017;
import java.util.Stack;
public class Day9 {
static int part1(String input) {
int weight = 1;
int score = 0;
int canceled = 0;
Stack<Character> stack = new Stack<>();
int i = 0;
stack.push(input.charAt(i++));
while (i < input.length()) {
char c = stack.peek();
char n = input.charAt(i++);
if (n == '!') {
i++;
} else if (c == '{') {
if (n == '}') {
// found closing
stack.pop();
score += weight;
weight--;
} else if (n == '{') {
weight++;
stack.push(n);
} else if (n == '<') {
stack.push('<');
}
} else if (c == '<') {
if (n == '>') {
stack.pop();
} else {
canceled++;
}
}
}
// return score // part 1
return canceled; // part 2
}
public static void main(String[] args) {
String[] inputs = {
"<>", // 0 characters",
"<random characters>", // 17 characters",
"<<<<>", // 3 characters",
"<{!>}>", // 2 characters",
"<!!>", // 0 characters",
"<!!!>>", // 0 characters",
"<{o\"i!a,<{i<a>"}; // 10 characters
for (String i : inputs) {
System.out.println(part1(i));
}
}
}