diff --git a/2023/src/main/java/nl/e86/aoc/aoc2023/day04/Pile.java b/2023/src/main/java/nl/e86/aoc/aoc2023/day04/Pile.java index 52a0125..0e2c598 100644 --- a/2023/src/main/java/nl/e86/aoc/aoc2023/day04/Pile.java +++ b/2023/src/main/java/nl/e86/aoc/aoc2023/day04/Pile.java @@ -13,7 +13,7 @@ public int getSolutionPt1() { return getScore(); } public int getSolutionPt2() { - return -1; + return winCards(); } private int getScore() { @@ -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 input) { List cards = new ArrayList<>(); for (String inputLine : input) { diff --git a/2023/src/main/java/nl/e86/aoc/aoc2023/day04/ScratchCard.java b/2023/src/main/java/nl/e86/aoc/aoc2023/day04/ScratchCard.java index f897bef..0cf2aae 100644 --- a/2023/src/main/java/nl/e86/aoc/aoc2023/day04/ScratchCard.java +++ b/2023/src/main/java/nl/e86/aoc/aoc2023/day04/ScratchCard.java @@ -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 winningNumbers; private final List numbersOnCard; - private ScratchCard(Integer cardNumber, List winningNumbers, List numbersOnCard) { + private ScratchCard(int cardNumber, List winningNumbers, List 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) { @@ -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(); diff --git a/2023/src/test/java/nl/e86/aoc/aoc2023/day04/Day04Test.java b/2023/src/test/java/nl/e86/aoc/aoc2023/day04/Day04Test.java index fe6ed21..fdee38a 100644 --- a/2023/src/test/java/nl/e86/aoc/aoc2023/day04/Day04Test.java +++ b/2023/src/test/java/nl/e86/aoc/aoc2023/day04/Day04Test.java @@ -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); + } }