-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.java
78 lines (62 loc) · 1.99 KB
/
Day10.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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Stack;
public class Day10 {
public static long function(String fname,boolean alsoComplete) throws IOException{
HashMap<Character,Integer> mappingStart=new HashMap<Character,Integer>();
HashMap<Character,Integer> mappingEnd=new HashMap<Character,Integer>();
int[] corruptScore= {3,57,1197,25137};
int[] completeScore= {1,2,3,4};
char[] openings= {'(','[','{','<'};
char[] ends= {')',']','}','>'};
for (int i=0;i<4;++i) {
mappingStart.put(openings[i],i);
mappingEnd.put(ends[i],i);
}
BufferedReader brdr=new BufferedReader(new FileReader(fname));
String ln;
int corruptedAns=0;
ArrayList<Long> completeScores=new ArrayList<Long>();
while ((ln=brdr.readLine())!=null) {
Stack<Character> s=new Stack<Character>();
boolean corrupted=false;
for (int i=0;i<ln.length();++i) {
char c=ln.charAt(i);
if (mappingStart.containsKey(c))
s.push(c);
else {
if (mappingEnd.containsKey(c) && openings[mappingEnd.get(c)]!=s.peek()) {
corruptedAns+=corruptScore[mappingEnd.get(c)];
corrupted=true;
break;
}else
s.pop();
}
}
if (alsoComplete && !corrupted) {
long completeAns=0;
while (!s.empty())
completeAns=5*completeAns + completeScore[mappingStart.get(s.pop())];
completeScores.add(completeAns);
}
}
if (alsoComplete) {
Collections.sort(completeScores);
return completeScores.get(completeScores.size()/2 );
}
return corruptedAns;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
String SAMPLE="input/day10_sample.txt";
String REAL="input/day10.txt";
System.out.println(function(SAMPLE,false));
System.out.println(function(REAL,false));
System.out.println(function(SAMPLE,true));
System.out.println(function(REAL,true));
}
}