Skip to content

Commit b72b1c6

Browse files
committed
Solved day04 part 2
1 parent 6fc482b commit b72b1c6

File tree

7 files changed

+65
-6
lines changed

7 files changed

+65
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44

55
// project meta data
66
group 'de.havox_design.aoc2023'
7-
version '0.4.2'
7+
version '0.4.4'
88

99
// Switch to gradle "all" distribution.
1010
wrapper {

day04/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,44 @@ each of the three matches after the first).
4848
So, in this example, the Elf's pile of scratchcards is worth **`13` points**.
4949

5050
Take a seat in the large pile of colorful cards. **How many points are they worth in total**?
51+
52+
# Part Two
53+
Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed
54+
on the back of every card this whole time.
55+
56+
There's no such thing as "points". Instead, scratchcards only cause you to **win more scratchcards** equal to the
57+
number of winning numbers you have.
58+
59+
Specifically, you win **copies** of the scratchcards below the winning card equal to the number of matches. So, if
60+
card `10` were to have `5` matching numbers, you would win one copy each of cards `11`, `12`, `13`, `14`, and `15`.
61+
62+
Copies of scratchcards are scored like normal scratchcards and have the **same card number** as the card they copied.
63+
So, if you win a copy of card `10` and it has `5` matching numbers, it would then win a copy of the same cards that
64+
the original card `10` won: cards `11`, `12`, `13`, `14`, and `15`. This process repeats until none of the copies
65+
cause you to win any more cards. (Cards will never make you copy a card past the end of the table.)
66+
67+
This time, the above example goes differently:
68+
```
69+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
70+
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
71+
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
72+
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
73+
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
74+
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
75+
```
76+
* Card `1` has four matching numbers, so you win one copy each of the next four cards: cards `2`, `3`, `4`, and `5`.
77+
* Your original card `2` has two matching numbers, so you win one copy each of cards `3` and `4`.
78+
* Your copy of card `2` also wins one copy each of cards `3` and `4`.
79+
* Your four instances of card `3` (one original and three copies) have two matching numbers, so you win four copies
80+
each of cards `4` and `5`.
81+
* Your eight instances of card `4` (one original and seven copies) have one matching number, so you win eight copies of
82+
card `5`.
83+
* Your fourteen instances of card `5` (one original and thirteen copies) have no matching numbers and win no more cards.
84+
* Your one instance of card `6` (one original) has no matching numbers and wins no more cards.
85+
86+
Once all of the originals and copies have been processed, you end up with `1` instance of card `1`, `2` instances of
87+
card `2`, `4` instances of card `3`, `8` instances of card `4`, `14` instances of card `5`, and `1` instance of card
88+
`6`. In total, this example pile of scratchcards causes you to ultimately have `30` scratchcards!
89+
90+
Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of
91+
scratchcards, **how many total scratchcards do you end up with**?

day04/src/main/kotlin/de/havox_design/aoc2023/day04/Day04.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,23 @@ class Day04(private var filename: String) {
77
.map { ScratchCard.from(it) }
88
.sumOf { it.scoreV1() }
99

10-
fun solvePart2(): Long =
11-
0L
10+
fun solvePart2(): Long {
11+
val cards = getResourceAsText(filename)
12+
.filter(String::isNotBlank)
13+
.map { ScratchCard.from(it) }
14+
15+
for (i in cards.indices) {
16+
val winnings = cards[i]
17+
.countMatching()
18+
val originalCount = cards[i].count
19+
for (w in 1..winnings) {
20+
cards[i + w].count += originalCount
21+
}
22+
}
23+
24+
return cards
25+
.sumOf { it.count }
26+
}
1227

1328
private fun getResourceAsText(path: String): List<String> =
1429
this.javaClass.classLoader.getResourceAsStream(path)!!.bufferedReader().readLines()

day04/src/main/kotlin/de/havox_design/aoc2023/day04/ScratchCard.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package de.havox_design.aoc2023.day04
22

3-
data class ScratchCard(val winningNumbers: List<Int>, val playerNumbers: List<Int>) {
3+
data class ScratchCard(val winningNumbers: List<Int>, val playerNumbers: List<Int>, var count: Long = 1L) {
44
fun scoreV1(): Long {
55
var score = 0L
66
playerNumbers
@@ -14,6 +14,9 @@ data class ScratchCard(val winningNumbers: List<Int>, val playerNumbers: List<In
1414
return score
1515
}
1616

17+
fun countMatching(): Int = playerNumbers
18+
.count { it in winningNumbers }
19+
1720
companion object {
1821
private const val NAME_END_DELIMITER: String = ":"
1922
private const val DATA_DELIMITER: String = "|"

day04/src/test/kotlin/de/havox_design/aoc2023/day04/Day04Test.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class Day04Test {
2727
@JvmStatic
2828
private fun getDataForTestSolvePart1(): Stream<Arguments> =
2929
Stream.of(
30-
Arguments.of("part1sample.txt", 13L)
30+
Arguments.of("sample.txt", 13L)
3131
)
3232

3333
@JvmStatic
3434
private fun getDataForTestSolvePart2(): Stream<Arguments> =
3535
Stream.of(
36-
Arguments.of("part2sample1.txt", 0L)
36+
Arguments.of("sample.txt", 30L)
3737
)
3838
}
3939
}

day04/src/test/resources/part2sample1.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)