-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartTwo.java
76 lines (67 loc) · 2.39 KB
/
PartTwo.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
/**
* --- Day 4: Scratchcards --- --- Part Two ---
*
*
* https://adventofcode.com/2023/day/4
**/
public class PartTwo {
/**
* The ownCardsMap contains the own cards for each cardId
*/
public static TreeMap<Integer, String[]> ownCardsMap = new TreeMap<>();
/**
* The winningCardsMap contains the winning cards for each cardId
*/
public static TreeMap<Integer, String[]> winningCardsMap = new TreeMap<>();
public static void main(String[] args) {
long totalCards = 0;
try (Scanner scanner = new Scanner(new File("./input.txt"))) {
while (scanner.hasNextLine()) {
String input = scanner.nextLine();
String[] cardSplit = input.split(":\\s+");
int cardNumber = Integer.parseInt(cardSplit[0]
.replaceAll("[^0-9]", ""));
String[] ownAndWinningCards = cardSplit[1].split(" \\| ");
String[] ownCards = ownAndWinningCards[0].split("\\s+");
String[] winningCards = ownAndWinningCards[1].split("\\s+");
ownCardsMap.put(cardNumber - 1, ownCards);
winningCardsMap.put(cardNumber - 1, winningCards);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
for (int cardId = 0; cardId < ownCardsMap.size(); cardId++) {
totalCards += getWinningCards(cardId);
}
System.out.println(totalCards);
}
/**
* Returns the amount of winning cards for a given cardId including the
* Scratchcards
*
* @param cardId the cardId
* @return the amount of winning cards
*/
public static long getWinningCards(int cardId) {
long winningCardCount = getAmountOfWinningCards(cardId);
long result = 1;
for (int i = 1; i <= winningCardCount; i++) {
result += getWinningCards(cardId + i);
}
return result;
}
/**
* Returns the amount of winning cards for a given cardId
*
* @param cardId the cardId
* @return the amount of winning cards
*/
private static long getAmountOfWinningCards(int cardId) {
return Arrays.stream(ownCardsMap.get(cardId))
.filter(Arrays.asList(winningCardsMap.get(cardId))::contains)
.count();
}
}