-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day8.java
250 lines (242 loc) · 9.29 KB
/
Day8.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
public class Day8 {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("day8.txt");
// part1(file);
part2(file);
}
public static void part1(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
int count = 0;
while (scan.hasNextLine()) {
String line[] = scan.nextLine().split(" \\| ");
for (String s : line[1].split(" ")) {
if (s.length() == 4 || s.length() == 3 || s.length() == 7 || s.length() == 2)
count++;
}
}
System.out.println(count);
}
public static void part2(File file) throws FileNotFoundException {
Scanner scan = new Scanner(file);
// BigInteger sum = new BigInteger("0");
long sum = 0;
while (scan.hasNextLine()) {
String[] line = scan.nextLine().split(" \\| ");
// input.add(line[0]);
// digits.add(line[1]);
String input = line[0];
String digits = line[1];
Map<String, String> map = new HashMap<>();
List<String> twoOrThreeOrFive = new ArrayList<>();
List<String> sixOrNineOrZero = new ArrayList<>();
for (String s : input.split(" ")) {
for (String ss : s.split(" ")) {
switch (ss.length()) {
case 2:
char[] ch = ss.toCharArray();
Arrays.sort(ch);
ss = new String(ch);
map.put("1", ss);
break;
case 3:
ch = ss.toCharArray();
Arrays.sort(ch);
ss = new String(ch);
map.put("7", ss);
break;
case 4:
ch = ss.toCharArray();
Arrays.sort(ch);
ss = new String(ch);
map.put("4", ss);
break;
case 5:
ch = ss.toCharArray();
Arrays.sort(ch);
ss = new String(ch);
twoOrThreeOrFive.add(ss);
break;
case 6:
ch = ss.toCharArray();
Arrays.sort(ch);
ss = new String(ch);
sixOrNineOrZero.add(ss);
break;
default:
ch = ss.toCharArray();
Arrays.sort(ch);
ss = new String(ch);
map.put("8", ss);
}
}
}
// find 2 using 4
for (String s : twoOrThreeOrFive) {
if (findDifference(s, map.get("4")) == 3) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
s = new String(ch);
map.put("2", s);
twoOrThreeOrFive.remove(s);
break;
}
}
// find 3 and 5 using 2
for (String s : twoOrThreeOrFive) {
if (findDifference(s, map.get("2")) == 1) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
s = new String(ch);
map.put("3", s);
} else if (findDifference(s, map.get("2")) == 2) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
s = new String(ch);
map.put("5", s);
}
}
// find 9 using 3
for (String s : sixOrNineOrZero) {
if (findDifference(s, map.get("3")) == 1) {
map.put("9", s);
char[] ch = s.toCharArray();
Arrays.sort(ch);
s = new String(ch);
sixOrNineOrZero.remove(s);
break;
}
}
// find zero and 6 using 7
for (String s : sixOrNineOrZero) {
if (findDifference(s, map.get("7")) == 3) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
s = new String(ch);
map.put("0", s);
} else if (findDifference(s, map.get("7")) == 4) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
s = new String(ch);
map.put("6", s);
}
}
// String s = "";
StringBuilder sb = new StringBuilder();
for (String digit : digits.split(" ")) {
char[] ch = digit.toCharArray();
Arrays.sort(ch);
digit = new String(ch);
for (Entry<String, String> entry : map.entrySet()) {
if (digit.equals(entry.getValue())) {
// s += entry.getKey();
sb.append(entry.getKey());
}
}
}
sum += Long.parseLong(sb.toString());
}
System.out.println(sum);
}
public static int findDifference(String s1, String s2) {
String s = "";
for (char c1 : s1.toCharArray()) {
boolean isContained = false;
for (char c2 : s2.toCharArray()) {
if (c2 == c1)
isContained = true;
}
if (!isContained)
s += "" + c1;
}
return s.length();
}
// public static void part2(File file) throws FileNotFoundException {
// Scanner scan = new Scanner(file);
// List<String> input = new ArrayList<>();
// List<String> digits = new ArrayList<>();
// while (scan.hasNextLine()) {
// String[] line = scan.nextLine().split(" \\| ");
// input.add(line[0]);
// digits.add(line[1]);
// }
// Map<Integer, String> map = new HashMap<>();
// Map<Integer, String> oneSevenFourEight = new HashMap<>();
// List<String> twoOrThreeOrFive = new ArrayList<>();
// List<String> sixOrNineOrZero = new ArrayList<>();
// for (String s : input) {
// for (String ss : s.split(" ")) {
// switch (ss.length()) {
// case 2:
// oneSevenFourEight.put(1, ss);
// break;
// case 3:
// oneSevenFourEight.put(7, ss);
// break;
// case 4:
// oneSevenFourEight.put(4, ss);
// break;
// case 5:
// twoOrThreeOrFive.add(ss);
// break;
// case 6:
// sixOrNineOrZero.add(ss);
// break;
// default:
// oneSevenFourEight.put(8, ss);
// }
// }
// }
// map.put(1, findDifference(oneSevenFourEight.get(7), oneSevenFourEight.get(1)));
// map.put(3, findSimilarity(oneSevenFourEight.get(7), oneSevenFourEight.get(1)));
// map.put(6, findSimilarity(oneSevenFourEight.get(7), oneSevenFourEight.get(1)));
// map.put(2, findDifference(oneSevenFourEight.get(4), oneSevenFourEight.get(1)));
// map.put(4, findDifference(oneSevenFourEight.get(4), oneSevenFourEight.get(1)));
// String fiveAndSix = "";
// for (String s : twoOrThreeOrFive) {
// String diff =
// }
// }
// public static String findSimilarity(String s1, String s2) {
// String s = "";
// for (char c1 : s1.toCharArray()) {
// boolean isContained = false;
// for (char c2 : s2.toCharArray()) {
// if (c2 == c1)
// isContained = true;
// }
// if (isContained)
// s += "" + c1;
// }
// return s;
// }
// public static String findDifference(String s1, String s2) {
// String s = "", longString = "", shortString = "";
// if (s1.length() > s2.length()) {
// longString = s1;
// shortString = s2;
// } else {
// longString = s2;
// shortString = s1;
// }
// for (char c1 : longString.toCharArray()) {
// boolean isContained = false;
// for (char c2 : shortString.toCharArray()) {
// if (c2 == c1)
// isContained = true;
// }
// if (!isContained)
// s += "" + c1;
// }
// return s;
// }
}