Skip to content

Commit

Permalink
added tests for day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnace committed Dec 9, 2023
1 parent 336fba8 commit fc51f0b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
File renamed without changes.
22 changes: 22 additions & 0 deletions src/main/kotlin/day02/day02.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package day02

import java.io.File

const val RED_CUBES = 12
const val GREEN_CUBES = 13
const val BLUE_CUBES = 14

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

fun solve1() = part1(input)

fun solve2() = part2(input)

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

fun part2(input: List<String>): Int =
input.map { Game.parse(it) }
.sumOf { it.power }
23 changes: 0 additions & 23 deletions src/main/kotlin/day02/main.kt

This file was deleted.

52 changes: 52 additions & 0 deletions src/test/kotlin/day02/Day02Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package day02

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class Day02Tests {
@Test
fun `part1 example`() {
// arrange
val input = listOf(
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green",
"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue",
"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red",
"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red",
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green",
)

// act
val actual = part1(input)

// assert
assertEquals(8, actual)
}

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

@Test
fun `part2 example`() {
// arrange
val input = listOf(
"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green",
"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue",
"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red",
"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red",
"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green",
)

// act
val actual = part2(input)

// assert
assertEquals(2286, actual)
}

@Test
fun `part2 solution`() {
assertEquals(76008, solve2())
}
}

0 comments on commit fc51f0b

Please sign in to comment.