diff --git a/src/main/kotlin/day01/Day01.kt b/src/main/kotlin/day01/day01.kt similarity index 100% rename from src/main/kotlin/day01/Day01.kt rename to src/main/kotlin/day01/day01.kt diff --git a/src/main/kotlin/day02/day02.kt b/src/main/kotlin/day02/day02.kt new file mode 100644 index 0000000..2a33168 --- /dev/null +++ b/src/main/kotlin/day02/day02.kt @@ -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): Int = + input.map { line -> Game.parse(line) } + .filter { it.isPossible } + .sumOf { it.id } + +fun part2(input: List): Int = + input.map { Game.parse(it) } + .sumOf { it.power } diff --git a/src/main/kotlin/day02/main.kt b/src/main/kotlin/day02/main.kt deleted file mode 100644 index 88ccf88..0000000 --- a/src/main/kotlin/day02/main.kt +++ /dev/null @@ -1,23 +0,0 @@ -package day02 - -import java.io.File - -const val RED_CUBES = 12 -const val GREEN_CUBES = 13 -const val BLUE_CUBES = 14 - -fun main() { - val input = File("src/main/kotlin/day02/input.txt").readLines() - - fun part1(): Int = - input.map { line -> Game.parse(line) } - .filter { it.isPossible } - .sumOf { it.id } - - fun part2(): Int = - input.map { Game.parse(it) } - .sumOf { it.power } - - println("Day 02, Part 1: ${part1()}") - println("Day 02, Part 2: ${part2()}") -} diff --git a/src/test/kotlin/day02/Day02Tests.kt b/src/test/kotlin/day02/Day02Tests.kt new file mode 100644 index 0000000..e59be5a --- /dev/null +++ b/src/test/kotlin/day02/Day02Tests.kt @@ -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()) + } +}