Skip to content

Commit b3552ee

Browse files
committed
Day 3: Binary Diagnostic ratings + math utils
1 parent daf6787 commit b3552ee

File tree

5 files changed

+1137
-0
lines changed

5 files changed

+1137
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Day03;
2+
3+
import advent.utils.AdventMathUtils;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class BinaryPowerConsumptionDiagnoser {
9+
10+
public long diagnoseBinaryPowerConsumption(List<String> binaryReportEntries) {
11+
int reportSize = binaryReportEntries.get(0).length();
12+
StringBuilder gamma = new StringBuilder();
13+
StringBuilder epsilon = new StringBuilder();
14+
for (int bitPosition = 0; bitPosition < reportSize; bitPosition++) {
15+
char mostFrequentAtPos = getMostFrequentElementAtPosition(binaryReportEntries, bitPosition);
16+
gamma.append(mostFrequentAtPos);
17+
epsilon.append(mostFrequentAtPos == '1' ? '0' : '1');
18+
}
19+
return multiplyRates(gamma.toString(), epsilon.toString());
20+
}
21+
22+
public long diagnoseLifeSupportRating(List<String> reportEntries) {
23+
List<String> oxygenRate = new ArrayList<>(List.copyOf(reportEntries));
24+
List<String> cO2Rate = new ArrayList<>(List.copyOf(reportEntries));
25+
int reportSize = reportEntries.get(0).length();
26+
27+
for (int bitPosition = 0; bitPosition < reportSize; bitPosition++) {
28+
char mostFrequentOxygenElement = getMostFrequentElementAtPosition(oxygenRate, bitPosition);
29+
char leastFrequentCo2Element = getMostFrequentElementAtPosition(cO2Rate, bitPosition) == '1' ? '0' : '1';
30+
31+
int finalBitPosition = bitPosition;
32+
if (oxygenRate.size() > 1) {
33+
oxygenRate.removeIf(p -> p.charAt(finalBitPosition) != mostFrequentOxygenElement);
34+
}
35+
if (cO2Rate.size() > 1) {
36+
cO2Rate.removeIf(p -> p.charAt(finalBitPosition) != leastFrequentCo2Element);
37+
}
38+
if (cO2Rate.size() == 1 && oxygenRate.size() == 1) {
39+
break;
40+
}
41+
}
42+
return multiplyRates(oxygenRate.get(0), cO2Rate.get(0));
43+
}
44+
45+
private long multiplyRates(String s, String s2) {
46+
return Integer.parseInt(s, 2) * Integer.parseInt(s2, 2);
47+
}
48+
49+
private char getMostFrequentElementAtPosition(List<String> reportEntries, int bitPosition) {
50+
List<Character> currentPositionList = new ArrayList<>();
51+
for (String currentReport : reportEntries) {
52+
currentPositionList.add(currentReport.charAt(bitPosition));
53+
}
54+
List<Character> mostFrequentElementsInList = AdventMathUtils.findMostFrequentElementsInList(currentPositionList);
55+
if (mostFrequentElementsInList.size() == 1) {
56+
return mostFrequentElementsInList.get(0);
57+
} else {
58+
return '1';
59+
}
60+
}
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Day03;
2+
3+
import advent.utils.AdventFileUtils;
4+
5+
import java.util.List;
6+
7+
public class Day03Main {
8+
9+
public static void main(String[] args) {
10+
List<String> inputAsStringList = getInput();
11+
System.out.println("This is the input: \n" + inputAsStringList);
12+
BinaryPowerConsumptionDiagnoser diagnoser = new BinaryPowerConsumptionDiagnoser();
13+
14+
long powerConsumptionRating = diagnoser.diagnoseBinaryPowerConsumption(inputAsStringList);
15+
System.out.println("The power consumption rating is: " + powerConsumptionRating);
16+
17+
long lifeSupportRating = diagnoser.diagnoseLifeSupportRating(inputAsStringList);
18+
System.out.println("The life support rating is: " + lifeSupportRating);
19+
}
20+
21+
protected static List<String> getInput() {
22+
return AdventFileUtils.readClassInputIntoStringLines(Day03Main.class);
23+
}
24+
}

Advent_2021/src/main/java/advent/utils/AdventMathUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package advent.utils;
22

3+
import java.util.Collections;
4+
import java.util.Comparator;
35
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Map;
48
import java.util.Set;
9+
import java.util.function.BinaryOperator;
10+
import java.util.function.Function;
11+
import java.util.stream.Collectors;
512

613
public class AdventMathUtils {
714

@@ -15,6 +22,19 @@ public static <T> Set<T> getIntersectionOf(Set<T> firstSet, Set<T> secondSet) {
1522
return intersection;
1623
}
1724

25+
public static <T> T calculateMode(List<T> elements) {
26+
return elements.stream()
27+
.reduce(BinaryOperator.maxBy(Comparator.comparingInt(o -> Collections.frequency(elements, o)))).orElse(null);
28+
}
29+
30+
public static <T> List<T> findMostFrequentElementsInList(List<T> elements) {
31+
return elements.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
32+
.entrySet().stream()
33+
.collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())))
34+
.entrySet().stream().max(Map.Entry.comparingByKey()).map(Map.Entry::getValue)
35+
.orElse(Collections.emptyList());
36+
}
37+
1838
public static boolean isSumOfElements(long sum, long element1, long element2) {
1939
return element1 + element2 == sum;
2040
}

0 commit comments

Comments
 (0)