Skip to content

Commit

Permalink
Merge pull request #8 from jwnace/day04
Browse files Browse the repository at this point in the history
Day04
  • Loading branch information
jwnace authored Dec 11, 2023
2 parents cec3644 + 2b173f6 commit 39cb9dc
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/main/kotlin/day01/day01.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ private val digitStrings = mapOf(
"nine" to 9,
)

fun solve1() = part1(input)
fun part1() = solve1(input)

fun solve2() = part2(input)
fun part2() = solve2(input)

fun part1(input: List<String>): Int =
fun solve1(input: List<String>): Int =
input.map { line -> line.filter { c -> c.isDigit() } }
.sumOf { "${it.first()}${it.last()}".toInt() }

fun part2(input: List<String>): Int {
fun solve2(input: List<String>): Int {
var sum = 0

for (line in input) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/day02/day02.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ const val BLUE_CUBES = 14

val input = File("src/main/kotlin/day02/input.txt").readLines()

fun solve1() = part1(input)
fun part1() = solve1(input)

fun solve2() = part2(input)
fun part2() = solve2(input)

fun part1(input: List<String>): Int =
fun solve1(input: List<String>): Int =
input.map { line -> Game.parse(line) }
.filter { it.isPossible }
.sumOf { it.id }

fun part2(input: List<String>): Int =
fun solve2(input: List<String>): Int =
input.map { Game.parse(it) }
.sumOf { it.power }
8 changes: 4 additions & 4 deletions src/main/kotlin/day03/day03.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import java.io.File

val input = File("src/main/kotlin/day03/input.txt").readLines()

fun solve1() = part1(input)
fun part1() = solve1(input)

fun solve2() = part2(input)
fun part2() = solve2(input)

fun part1(input: List<String>): Int {
fun solve1(input: List<String>): Int {
val grid = buildGrid(input)
val partNumbers = getPartNumbers(grid)

return partNumbers.sumOf { it.number }
}

fun part2(input: List<String>): Int {
fun solve2(input: List<String>): Int {
val grid = buildGrid(input)
val partNumbers = getPartNumbers(grid)

Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/day04/ScratchCard.kt
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()
}
29 changes: 29 additions & 0 deletions src/main/kotlin/day04/day04.kt
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()
}
209 changes: 209 additions & 0 deletions src/main/kotlin/day04/input.txt

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions src/main/kotlin/main.kt
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()}")
}
8 changes: 4 additions & 4 deletions src/test/kotlin/day01/Day01Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class Day01Tests {
)

// act
val actual = part1(input)
val actual = solve1(input)

// assert
assertEquals(142, actual)
}

@Test
fun `part1 solution`() {
assertEquals(55208, solve1())
assertEquals(55208, part1())
}

@Test
Expand All @@ -40,14 +40,14 @@ class Day01Tests {
)

// act
val actual = part2(input)
val actual = solve2(input)

// assert
assertEquals(281, actual)
}

@Test
fun `part2 solution`() {
assertEquals(54578, solve2())
assertEquals(54578, part2())
}
}
8 changes: 4 additions & 4 deletions src/test/kotlin/day02/Day02Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Day02Tests {
)

// act
val actual = part1(input)
val actual = solve1(input)

// assert
assertEquals(8, actual)
}

@Test
fun `part1 solution`() {
assertEquals(2348, solve1())
assertEquals(2348, part1())
}

@Test
Expand All @@ -39,14 +39,14 @@ class Day02Tests {
)

// act
val actual = part2(input)
val actual = solve2(input)

// assert
assertEquals(2286, actual)
}

@Test
fun `part2 solution`() {
assertEquals(76008, solve2())
assertEquals(76008, part2())
}
}
8 changes: 4 additions & 4 deletions src/test/kotlin/day03/Day03Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ class Day03Tests {
)

// act
val actual = part1(input)
val actual = solve1(input)

// assert
assertEquals(4361, actual)
}

@Test
fun `part1 solution`() {
assertEquals(514969, solve1())
assertEquals(514969, part1())
}

@Test
Expand All @@ -49,14 +49,14 @@ class Day03Tests {
)

// act
val actual = part2(input)
val actual = solve2(input)

// assert
assertEquals(467835, actual)
}

@Test
fun `part2 solution`() {
assertEquals(78915902, solve2())
assertEquals(78915902, part2())
}
}
54 changes: 54 additions & 0 deletions src/test/kotlin/day04/Day04Tests.kt
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())
}
}

0 comments on commit 39cb9dc

Please sign in to comment.