-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubmarineDiagnostor.java
More file actions
139 lines (124 loc) · 4.36 KB
/
SubmarineDiagnostor.java
File metadata and controls
139 lines (124 loc) · 4.36 KB
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
import java.util.ArrayList;
import java.util.List;
/**
* Solution to Day 3: Binary Diagnostic.
*
* @author bluebillxp
*/
public class SubmarineDiagnostor {
private static class Bit {
int zeros;
int ones;
}
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day3-binary-diagnostic.txt");
System.out.println("Day 3: Binary Diagnostic: " + input.size() + " binaries loaded.");
System.out.println("Day 3: Binary Diagnostic --- Part One ---");
System.out.println("What is the power consumption of the submarine??");
final int answerOne = solutionPartOne(input);
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 3: Binary Diagnostic --- Part Two ---");
System.out.println("What is the life support rating of the submarine?");
final int answerTwo = solutionPartTwo(input);
System.out.println("Answer: " + answerTwo);
}
private static Bit[] binarySummary(List<String> input) {
Bit[] bits = new Bit[12];
for (String binary : input) {
for (int i = 0; i < binary.length(); i++) {
if (bits[i] == null) {
bits[i] = new Bit();
}
char bin = binary.charAt(i);
if (bin == '0') {
bits[i].zeros++;
} else {
bits[i].ones++;
}
}
}
return bits;
}
/**
* Calculates the power consumption of the submarine.
*
* @param input Defined input from the challenge.
*
* @return answer
*/
private static int solutionPartOne(List<String> input) {
Bit[] bits = binarySummary(input);
int gamma = 0;
int epsilon = 0;
for (int i = 0; i < bits.length; i++) {
Bit bit = bits[i];
if (bit.ones > bit.zeros) {
gamma += Math.pow(2, bits.length - 1 - i);
} else {
epsilon += Math.pow(2, bits.length - 1 - i);
}
}
return gamma * epsilon;
}
/**
* Calculates the life support rating of the submarine.
*
* @param input Defined input from the challenge.
*
* @return answer
*/
private static int solutionPartTwo(List<String> input) {
Bit[] bits = binarySummary(input);
String oxygenGenRating = locateOxygenGenRating(input, bits, 0);
String co2ScrubRating = locateCo2ScrubRating(input, bits, 0);
int oxygenRating = 0;
int co2Rating = 0;
for (int i = 0; i < bits.length; i++) {
if (oxygenGenRating.charAt(i) == '1') {
oxygenRating += Math.pow(2, bits.length - 1 - i);
}
if (co2ScrubRating.charAt(i) == '1') {
co2Rating += Math.pow(2, bits.length - 1 - i);
}
}
return oxygenRating * co2Rating;
}
private static String locateOxygenGenRating(List<String> binaries, Bit[] bits, int pos) {
if (binaries.size() == 1) {
return binaries.get(0);
}
List<String> nextBinaries = new ArrayList<>();
for (String binary : binaries) {
if (bits[pos].ones >= bits[pos].zeros) {
if (binary.charAt(pos) == '1') {
nextBinaries.add(binary);
}
} else {
if (binary.charAt(pos) == '0') {
nextBinaries.add(binary);
}
}
}
Bit[] nextBits = binarySummary(nextBinaries);
return locateOxygenGenRating(nextBinaries, nextBits, ++pos);
}
private static String locateCo2ScrubRating(List<String> binaries, Bit[] bits, int pos) {
if (binaries.size() == 1) {
return binaries.get(0);
}
List<String> nextBinaries = new ArrayList<>();
for (String binary : binaries) {
if (bits[pos].ones >= bits[pos].zeros) {
if (binary.charAt(pos) == '0') {
nextBinaries.add(binary);
}
} else {
if (binary.charAt(pos) == '1') {
nextBinaries.add(binary);
}
}
}
Bit[] nextBits = binarySummary(nextBinaries);
return locateCo2ScrubRating(nextBinaries, nextBits, ++pos);
}
}