-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay3.java
97 lines (72 loc) · 2.88 KB
/
Day3.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
package aoc21;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import utils.StaticUtils;
public class Day3 {
private List<String> rawData;
public Day3(File input) {
rawData = StaticUtils.inputFileToStringList(input);
}
public int run1() {
StringBuilder gamma = new StringBuilder();
StringBuilder epsilon = new StringBuilder();
int numOfBits = rawData.get(0).length();
for(int i = 0; i < numOfBits; i++) {
final int index = i;
int countOn = (int) rawData.stream().mapToInt(elem -> Integer.parseInt(Character.toString(elem.charAt(index)))).filter(n -> n == 1).count();
int countOff = (int) rawData.stream().mapToInt(elem -> Integer.parseInt(Character.toString(elem.charAt(index)))).filter(n -> n == 0).count();
gamma.append(countOn > countOff ? 1 : 0);
epsilon.append(countOn > countOff ? 0 : 1);
}
return Integer.parseInt(gamma.toString(), 2) * Integer.parseInt(epsilon.toString(), 2);
}
public int run2() {
String oxy = "";
String coTwo = "";
List<String> oxyList = new ArrayList<>(rawData);
int numOfBits = rawData.get(0).length();
for(int i = 0; i < numOfBits; i++) {
final int index = i;
int countOn = (int) oxyList.stream().mapToInt(elem -> Integer.parseInt(Character.toString(elem.charAt(index)))).filter(n -> n == 1).count();
int countOff = (int) oxyList.stream().mapToInt(elem -> Integer.parseInt(Character.toString(elem.charAt(index)))).filter(n -> n == 0).count();
String removeBit = "";
if(countOn >= countOff) {
removeBit = "0";
} else {
removeBit = "1";
}
final String finalRemoveBit = removeBit;
oxyList = oxyList.stream().filter(n -> n.charAt(index) != finalRemoveBit.charAt(0)).collect(Collectors.toList());
if(oxyList.size() == 1) {
oxy = oxyList.get(0);
break;
}
}
List<String> coTwoList = new ArrayList<>(rawData);
for(int i = 0; i < numOfBits; i++) {
final int index = i;
int countOn = (int) coTwoList.stream().mapToInt(elem -> Integer.parseInt(Character.toString(elem.charAt(index)))).filter(n -> n == 1).count();
int countOff = (int) coTwoList.stream().mapToInt(elem -> Integer.parseInt(Character.toString(elem.charAt(index)))).filter(n -> n == 0).count();
String removeBit = "";
if(countOn >= countOff) {
removeBit = "1";
} else {
removeBit = "0";
}
final String finalRemoveBit = removeBit;
coTwoList = coTwoList.stream().filter(n -> n.charAt(index) != finalRemoveBit.charAt(0)).collect(Collectors.toList());
if(coTwoList.size() == 1) {
coTwo = coTwoList.get(0);
break;
}
}
return Integer.parseInt(oxy, 2) * Integer.parseInt(coTwo, 2);
}
public static void main(String[] args) {
Day3 test = new Day3(new File("C:\\Users\\tmerdin\\aoc\\aoc21\\day3\\InputFile1.txt"));
System.out.println(test.run1());
System.out.println(test.run2());
}
}