-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.java
92 lines (72 loc) · 2.82 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
package net.eugenpaul.adventofcode.y2021.day3;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import lombok.Getter;
import net.eugenpaul.adventofcode.helper.SolutionTemplate;
public class Day3 extends SolutionTemplate {
@Getter
private long powerConsumption;
@Getter
private long lifeSupportRating;
public static void main(String[] args) {
Day3 puzzle = new Day3();
puzzle.doPuzzleFromFile("y2021/day3/puzzle1.txt");
}
@Override
public boolean doEvent(List<String> eventData) {
powerConsumption = computePowerConsumption(eventData);
lifeSupportRating = filterOxygenGeneratorRating(eventData) * filterCO2ScrubberRating(eventData);
logger.log(Level.INFO, () -> "powerConsumption : " + getPowerConsumption());
logger.log(Level.INFO, () -> "lifeSupportRating : " + getLifeSupportRating());
return true;
}
private long computePowerConsumption(List<String> eventData) {
StringBuilder gammaRate = new StringBuilder(eventData.get(0).length());
StringBuilder epsilonRate = new StringBuilder(eventData.get(0).length());
for (int i = 0; i < eventData.get(0).length(); i++) {
if (getMostCommonValue(eventData, i) == '1') {
gammaRate.append("1");
epsilonRate.append("0");
} else {
gammaRate.append("0");
epsilonRate.append("1");
}
}
return Long.parseLong(gammaRate.toString(), 2) * Long.parseLong(epsilonRate.toString(), 2);
}
private long filterOxygenGeneratorRating(List<String> eventData) {
List<String> data = new LinkedList<>(eventData);
int currentPos = 0;
while (data.size() > 1) {
char mostCommonValue = getMostCommonValue(data, currentPos);
int pos = currentPos;
data.removeIf(v -> v.charAt(pos) != mostCommonValue);
currentPos++;
}
return Long.parseLong(data.get(0), 2);
}
private long filterCO2ScrubberRating(List<String> eventData) {
List<String> data = new LinkedList<>(eventData);
int currentPos = 0;
while (data.size() > 1) {
char mostCommonValue = getMostCommonValue(data, currentPos);
int pos = currentPos;
data.removeIf(v -> v.charAt(pos) == mostCommonValue);
currentPos++;
}
return Long.parseLong(data.get(0), 2);
}
private char getMostCommonValue(List<String> eventData, int position) {
int counter0 = 0;
int counter1 = 0;
for (String data : eventData) {
if (data.charAt(position) == '1') {
counter1++;
} else {
counter0++;
}
}
return (counter0 > counter1) ? '0' : '1';
}
}