Skip to content
Snippets Groups Projects
Commit 637d3eeb authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Day 4 complete

parent 000fb434
Branches
No related tags found
No related merge requests found
# 2021 Advent of Coding Solutions # 2021 Advent of Code Solutions
## Day 1 ## Day 1
...@@ -15,3 +15,10 @@ switch statements. ...@@ -15,3 +15,10 @@ switch statements.
My CSCE 231 students should be able to do this problem in their sleep -- bit My CSCE 231 students should be able to do this problem in their sleep -- bit
masks are cool. masks are cool.
## Day 4
This problem is a little more interesting since there are two stringly types in
the data, and one of them (the Bingo cards) requires a delimiter between
instances. Defining a custom class to represent Bingo cards simplifies the
post-parsing problem.
\ No newline at end of file
...@@ -4,7 +4,6 @@ import edu.unl.cse.bohn.Puzzle; ...@@ -4,7 +4,6 @@ import edu.unl.cse.bohn.Puzzle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Day1 extends Puzzle { public class Day1 extends Puzzle {
...@@ -40,7 +39,7 @@ public class Day1 extends Puzzle { ...@@ -40,7 +39,7 @@ public class Day1 extends Puzzle {
@Override @Override
public int computePart1(List<String> data) { public int computePart1(List<String> data) {
depths = data.stream().map(Integer::parseInt).collect(Collectors.toList()); depths = data.stream().map(Integer::parseInt).toList();
return countIncreases(depths); return countIncreases(depths);
} }
......
...@@ -3,7 +3,6 @@ package edu.unl.cse.bohn.year2021; ...@@ -3,7 +3,6 @@ package edu.unl.cse.bohn.year2021;
import edu.unl.cse.bohn.Puzzle; import edu.unl.cse.bohn.Puzzle;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Day2 extends Puzzle { public class Day2 extends Puzzle {
...@@ -35,7 +34,7 @@ public class Day2 extends Puzzle { ...@@ -35,7 +34,7 @@ public class Day2 extends Puzzle {
.map(instruction -> new NavigationInstruction( .map(instruction -> new NavigationInstruction(
instruction.split(" ")[0], instruction.split(" ")[0],
Integer.parseInt(instruction.split(" ")[1]))) Integer.parseInt(instruction.split(" ")[1])))
.collect(Collectors.toList()); .toList();
range = 0; range = 0;
depth = 0; depth = 0;
for (NavigationInstruction instruction : navigationInstructions) { for (NavigationInstruction instruction : navigationInstructions) {
......
...@@ -5,7 +5,6 @@ import edu.unl.cse.bohn.Puzzle; ...@@ -5,7 +5,6 @@ import edu.unl.cse.bohn.Puzzle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.stream.Collectors;
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class Day3 extends Puzzle { public class Day3 extends Puzzle {
...@@ -33,7 +32,7 @@ public class Day3 extends Puzzle { ...@@ -33,7 +32,7 @@ public class Day3 extends Puzzle {
@Override @Override
public int computePart1(List<String> data) { public int computePart1(List<String> data) {
diagnosticReports = data.stream().map(report -> Integer.parseInt(report, 2)).collect(Collectors.toList()); diagnosticReports = data.stream().map(report -> Integer.parseInt(report, 2)).toList();
reportLength = data.get(0).length(); reportLength = data.get(0).length();
ones = new int[]{ ones = new int[]{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
...@@ -76,11 +75,11 @@ public class Day3 extends Puzzle { ...@@ -76,11 +75,11 @@ public class Day3 extends Puzzle {
if (predicate.test(numberOfOnes, numberOfZeroes)) { if (predicate.test(numberOfOnes, numberOfZeroes)) {
workingSet = workingSet.stream() workingSet = workingSet.stream()
.filter(report -> (report & mask) != 0) .filter(report -> (report & mask) != 0)
.collect(Collectors.toList()); .toList();
} else { } else {
workingSet = workingSet.stream() workingSet = workingSet.stream()
.filter(report -> (report & mask) == 0) .filter(report -> (report & mask) == 0)
.collect(Collectors.toList()); .toList();
} }
bit--; bit--;
// Need to re-count the ones and zeroes since the old counts are now wrong // Need to re-count the ones and zeroes since the old counts are now wrong
......
package edu.unl.cse.bohn.year2021;
import edu.unl.cse.bohn.Puzzle;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
@SuppressWarnings("unused")
public class Day4 extends Puzzle {
private List<Integer> numbers;
private List<BingoCard> cards;
public Day4() {
day = 4;
sampleData = """
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7""";
isProductionReady = true;
}
private void parseData(List<String> data) {
cards = new LinkedList<>();
int[][] card = null;
int startingLine = 0;
for (int lineNumber = 0; lineNumber < data.size(); lineNumber++) {
String line = data.get(lineNumber);
if (lineNumber == 0) {
numbers = Arrays.stream(line.split(",")).map(Integer::parseInt).toList();
} else {
if (line.isBlank()) {
if (lineNumber != 1) {
cards.add(new BingoCard(Objects.requireNonNull(card)));
}
card = null;
} else {
int[] row = new int[0];
try {
row = Arrays.stream(line.trim().split("\s+")).mapToInt(Integer::parseInt).toArray();
} catch (NumberFormatException e) {
System.err.println("oops");
}
if (card == null) {
startingLine = lineNumber;
// We're assuming square Bingo cards
card = new int[row.length][row.length];
}
card[lineNumber - startingLine] = row;
}
}
}
cards.add(new BingoCard(Objects.requireNonNull(card)));
}
@Override
public int computePart1(List<String> data) {
parseData(data);
Iterator<Integer> numberIterator = numbers.iterator();
while (cards.stream().noneMatch(BingoCard::hasBingo) && numberIterator.hasNext()) {
int number = numberIterator.next();
for (BingoCard card : cards) {
card.markCard(number);
}
}
BingoCard winningCard = cards.stream().filter(BingoCard::hasBingo).findAny().orElseThrow();
return winningCard.score();
}
@Override
public int computePart2(List<String> data) {
Iterator<Integer> numberIterator = numbers.iterator();
while ((cards.stream().filter(BingoCard::hasBingo).count() < cards.size() - 1) && numberIterator.hasNext()) {
int number = numberIterator.next();
for (BingoCard card : cards) {
card.markCard(number);
}
}
BingoCard losingCard = cards.stream().filter(card -> !card.hasBingo()).findAny().orElseThrow();
while (!losingCard.hasBingo() && numberIterator.hasNext()) {
losingCard.markCard(numberIterator.next());
}
return losingCard.score();
}
public static class BingoCard {
private final int[][] numbers;
private final boolean[][] markers;
private int lastNumberCalled = -1;
public BingoCard(int[][] cardNumbers) {
markers = new boolean[cardNumbers.length][cardNumbers[0].length];
for (boolean[] row : markers) {
Arrays.fill(row, false);
}
numbers = Arrays.stream(cardNumbers).map(int[]::clone).toArray(int[][]::new);
}
public void markCard(int number) {
lastNumberCalled = number;
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] == number) {
markers[i][j] = true;
}
}
}
}
public boolean hasBingo() {
boolean bingo = false;
// We're going to assume square Bingo cards
for (int i = 0; i < markers.length; i++) {
int rowMarkerCount = 0, columnMarkerCount = 0;
for (int j = 0; j < markers.length; j++) {
rowMarkerCount += markers[i][j] ? 1 : 0;
columnMarkerCount += markers[j][i] ? 1 : 0;
}
bingo = bingo || (rowMarkerCount == markers.length) || (columnMarkerCount == markers.length);
}
return bingo;
}
public int score() {
int rawScore = 0;
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {
rawScore += markers[i][j] ? 0 : numbers[i][j];
}
}
return rawScore * lastNumberCalled;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment