-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from jwnace/day04
Day04
- Loading branch information
Showing
11 changed files
with
345 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package day04 | ||
|
||
import kotlin.math.pow | ||
|
||
class ScratchCard(val cardNumber: Int, winningNumbersList: Set<Int>, numbersYouHaveList: Set<Int>) { | ||
companion object { | ||
fun parse(line: String): ScratchCard { | ||
val (left, right) = line.split(": ") | ||
val cardNumber = left.substring(5).trim().toInt() | ||
val (winningNumbers, numbersYouHave) = right.split(" | ").map { numbers -> | ||
numbers.split(" ").filter { it.isNotEmpty() }.map { it.toInt() }.toSet() | ||
} | ||
|
||
return ScratchCard(cardNumber, winningNumbers, numbersYouHave) | ||
} | ||
} | ||
|
||
val winCount = winningNumbersList.intersect(numbersYouHaveList).count() | ||
|
||
val score = (2.0).pow((winCount - 1).toDouble()).toInt() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package day04 | ||
|
||
import java.io.File | ||
|
||
val input = File("src/main/kotlin/day04/input.txt").readLines() | ||
|
||
fun part1() = solve1(input) | ||
|
||
fun part2() = solve2(input) | ||
|
||
fun solve1(input: List<String>): Int = input.map { ScratchCard.parse(it) }.sumOf { it.score } | ||
|
||
fun solve2(input: List<String>): Int { | ||
val cards = input.map { ScratchCard.parse(it) } | ||
val cardCounts = cards.associate { it.cardNumber to 1 }.toMutableMap() | ||
val minCardNumber = cards.minOf { it.cardNumber } | ||
val maxCardNumber = cards.maxOf { it.cardNumber } | ||
|
||
for (cardNumber in minCardNumber..maxCardNumber) { | ||
val card = cards.find { it.cardNumber == cardNumber } ?: continue | ||
val cardCount = cardCounts[cardNumber] ?: continue | ||
|
||
for (j in 1..card.winCount) { | ||
cardCounts[cardNumber + j] = cardCounts[cardNumber + j]!! + cardCount | ||
} | ||
} | ||
|
||
return cardCounts.values.sum() | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
fun main() { | ||
println("day 01, part 1: ${day01.solve1()}") | ||
println("day 01, part 2: ${day01.solve2()}") | ||
println("day 02, part 1: ${day02.solve1()}") | ||
println("day 02, part 2: ${day02.solve2()}") | ||
println("day 03, part 1: ${day03.solve1()}") | ||
println("day 03, part 2: ${day03.solve2()}") | ||
println("day 01, part 1: ${day01.part1()}") | ||
println("day 01, part 2: ${day01.part2()}") | ||
println("day 02, part 1: ${day02.part1()}") | ||
println("day 02, part 2: ${day02.part2()}") | ||
println("day 03, part 1: ${day03.part1()}") | ||
println("day 03, part 2: ${day03.part2()}") | ||
println("day 04, part 1: ${day04.part1()}") | ||
println("day 04, part 2: ${day04.part2()}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package day04 | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class Day04Tests { | ||
@Test | ||
fun `part1 example`() { | ||
// arrange | ||
val input = listOf( | ||
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", | ||
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", | ||
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", | ||
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", | ||
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", | ||
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", | ||
) | ||
|
||
// act | ||
val actual = solve1(input) | ||
|
||
// assert | ||
assertEquals(13, actual) | ||
} | ||
|
||
@Test | ||
fun `part1 solution`() { | ||
assertEquals(21158, part1()) | ||
} | ||
|
||
@Test | ||
fun `part2 example`() { | ||
// arrange | ||
val input = listOf( | ||
"Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53", | ||
"Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19", | ||
"Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1", | ||
"Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83", | ||
"Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36", | ||
"Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11", | ||
) | ||
|
||
// act | ||
val actual = solve2(input) | ||
|
||
// assert | ||
assertEquals(30, actual) | ||
} | ||
|
||
@Test | ||
fun `part2 solution`() { | ||
assertEquals(6050769, part2()) | ||
} | ||
} |