diff --git a/app/src/main/kotlin/day03/Day03.kt b/app/src/main/kotlin/day03/Day03.kt index c073bdd..f5cda17 100644 --- a/app/src/main/kotlin/day03/Day03.kt +++ b/app/src/main/kotlin/day03/Day03.kt @@ -1,13 +1,8 @@ - package day03 import kotlin.io.println class Day03 { - fun parse(input: String): String { - throw NotImplementedError() - } - fun part1(input: String): Int { val regex = Regex("mul\\((\\d{1,3}),(\\d{1,3})\\)") val muls = regex.findAll(input) @@ -47,6 +42,5 @@ class Day03 { .sum() return product - } } diff --git a/app/src/main/kotlin/day04/Day04.kt b/app/src/main/kotlin/day04/Day04.kt new file mode 100644 index 0000000..40077f5 --- /dev/null +++ b/app/src/main/kotlin/day04/Day04.kt @@ -0,0 +1,68 @@ +package day04 + +import kotlin.io.println + +class Day04 { + fun part1(input: String): Int { + val lines = input.lines().map { it.toList() } + var count = 0 + for (x in 0..>): Int { + val maxX = lines.count() - 1 + val maxY = lines[0].count() - 1 + var count = 0 + + for (dX in -1..1) { + if (x+(dX*3) < 0 || x+(dX*3) > maxX) { continue } + for (dY in -1..1) { + if (y+(dY*3) < 0 || y+(dY*3) > maxY) { continue } + if (lines[x+dX][y+dY] == 'M' && lines[x+(dX*2)][y+(dY*2)] == 'A' && lines[x+(dX*3)][y+(dY*3)] == 'S') { + count += 1 + } + } + } + return count + } + + fun part2(input: String): Int { + val lines = input.lines().map { it.toList() } + var count = 0 + for (x in 0..>): Int { + val maxX = lines.count() - 1 + val maxY = lines[0].count() - 1 + if (x-1 < 0 || x+1 > maxX) { return 0 } + if (y-1 < 0 || y+1 > maxY) { return 0 } + val topLeft = lines[x-1][y-1] + val bottomRight = lines[x+1][y+1] + if (topLeft != bottomRight && (bottomRight == 'M' || bottomRight == 'S') && (topLeft == 'M' || topLeft == 'S')) { + val topRight = lines[x-1][y+1] + val bottomLeft = lines[x+1][y-1] + if (topRight != bottomLeft && (bottomLeft == 'M' || bottomLeft == 'S') && (topRight == 'M' || topRight == 'S')) { + return 1 + } + } + + return 0 + } + +} + diff --git a/app/src/test/kotlin/day03/Day03Test.kt b/app/src/test/kotlin/day03/Day03Test.kt index 5b37406..81eb4b7 100644 --- a/app/src/test/kotlin/day03/Day03Test.kt +++ b/app/src/test/kotlin/day03/Day03Test.kt @@ -1,4 +1,3 @@ - package day03 import kotlin.test.Test diff --git a/app/src/test/kotlin/day04/Day04Test.kt b/app/src/test/kotlin/day04/Day04Test.kt new file mode 100644 index 0000000..4c22334 --- /dev/null +++ b/app/src/test/kotlin/day04/Day04Test.kt @@ -0,0 +1,49 @@ +package day04 + +import kotlin.test.Test +import util.InputDownloader + +class Day04Test { + val dayNum = 4 + val day = Day04() + + val input = InputDownloader().getInput(dayNum) + val example = """MMMSXXMASM +MSAMXMSMSA +AMXSXMAAMM +MSAMASMSMX +XMASAMXAMM +XXAMMXXAMA +SMSMSASXSS +SAXAMASAAA +MAMMMXMMMM +MXMXAXMASX""" + + @Test fun part1Example() { + part1(example, 18) + } + + @Test fun part1Solution() { + part1(input, 2545) + } + + @Test fun part2Example() { + part2(example, 9) + } + + @Test fun part2Solution() { + part2(input, 1886) + } + + fun part1(input: String, expected: Int) { + val output = day.part1(input) + println("output: $output") + assert(output == expected) + } + + fun part2(input: String, expected: Int) { + val output = day.part2(input) + println("output: $output") + assert(output == expected) + } +}