Skip to content

Commit

Permalink
Day 4 (2023) (Second Part)
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleErwin committed Dec 4, 2023
1 parent 4b8ac38 commit 696d029
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
30 changes: 29 additions & 1 deletion 2023/src/main/java/nl/e86/aoc/aoc2023/day04/Pile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public int getSolutionPt1() {
return getScore();
}
public int getSolutionPt2() {
return -1;
return winCards();
}

private int getScore() {
Expand All @@ -24,6 +24,34 @@ private int getScore() {
return score;
}

private int winCards() {
int totalCards = 0;
int cardIndex = 0;
while (cardIndex < cards.size()) {
ScratchCard currentCard = cards.get(cardIndex);
totalCards++;
int extraCards = currentCard.getExtraCards();
for (int i = 1; i <= extraCards; ++i) {
ScratchCard extraCard = duplicateCard(currentCard.getCardNumber() + i);
cards.add(extraCard);
}
cardIndex++;
}
return totalCards;
}

private ScratchCard duplicateCard(int cardId) {
ScratchCard match = null;
for (ScratchCard card : cards) {
if (card.getCardNumber() == cardId) {
match = card.getCopy();
break;
}
}

return match;
}

public static Pile createFromInput(List<String> input) {
List<ScratchCard> cards = new ArrayList<>();
for (String inputLine : input) {
Expand Down
24 changes: 21 additions & 3 deletions 2023/src/main/java/nl/e86/aoc/aoc2023/day04/ScratchCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ public class ScratchCard {
private static final Pattern CARD_NUMBER_PATTERN = Pattern.compile("Card\\s*(\\d+):");
private static final Pattern WINNING_NUMBERS_PATTERN = Pattern.compile(":(.*)\\|");
private static final Pattern NUMBERS_ON_CARD_PATTERN = Pattern.compile("\\|(.*)$");
private final Integer cardNumber;
private final int cardNumber;
private final List<Integer> winningNumbers;
private final List<Integer> numbersOnCard;

private ScratchCard(Integer cardNumber, List<Integer> winningNumbers, List<Integer> numbersOnCard) {
private ScratchCard(int cardNumber, List<Integer> winningNumbers, List<Integer> numbersOnCard) {
this.cardNumber = cardNumber;
this.winningNumbers = Collections.unmodifiableList(winningNumbers);
this.numbersOnCard = Collections.unmodifiableList(numbersOnCard);
}

public int getCardNumber() {
return cardNumber;
}

public int getScore() {
int score = 0;
for (Integer number : numbersOnCard) {
Expand All @@ -34,10 +38,24 @@ public int getScore() {
return score;
}

public int getExtraCards() {
int extraCards = 0;
for (Integer number : numbersOnCard) {
if (winningNumbers.contains(number)) {
extraCards++;
}
}
return extraCards;
}

public ScratchCard getCopy() {
return new ScratchCard(cardNumber, winningNumbers, numbersOnCard);
}

public static ScratchCard createCardFromInputData(String input) {
Matcher cardNumberMatcher = CARD_NUMBER_PATTERN.matcher(input);
cardNumberMatcher.find();
Integer cardNumber = Integer.parseInt(cardNumberMatcher.group(1));
int cardNumber = Integer.parseInt(cardNumberMatcher.group(1));

Matcher winningNumbersMatcher = WINNING_NUMBERS_PATTERN.matcher(input);
winningNumbersMatcher.find();
Expand Down
6 changes: 5 additions & 1 deletion 2023/src/test/java/nl/e86/aoc/aoc2023/day04/Day04Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ public void testDay04_1() {
Assert.assertEquals(13, output);
}


@Test
public void testDay04_2() {
int output = Day04.calculatePart2("day04.1.input");
Assert.assertEquals(30, output);
}
}

0 comments on commit 696d029

Please sign in to comment.