Skip to content

Commit

Permalink
Merge pull request #6 from jwnace/day03-add-tests
Browse files Browse the repository at this point in the history
added tests for day 3
  • Loading branch information
jwnace authored Dec 9, 2023
2 parents cc1034c + 8782e3d commit db7292a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 85 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/main/kotlin/day03/PartNumber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class PartNumber(val number: Int, val row: Int, val startCol: Int, val endC

fun isAdjacentTo(position: Pair<Int, Int>) =
row >= position.first - 1 &&
row <= position.first + 1 &&
endCol >= position.second - 1 &&
startCol <= position.second + 1
row <= position.first + 1 &&
endCol >= position.second - 1 &&
startCol <= position.second + 1
}
81 changes: 81 additions & 0 deletions src/main/kotlin/day03/day03.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package day03

import java.io.File

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

fun solve1() = part1(input)

fun solve2() = part2(input)

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

return partNumbers.sumOf { it.number }
}

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

return grid.filter { it.value.isSymbol() }
.filter { symbol -> symbolIsAdjacentToTwoPartNumbers(partNumbers, symbol) }
.map { symbol -> getAdjacentPartNumbers(partNumbers, symbol) }
.sumOf { it[0].number * it[1].number }
}

fun buildGrid(input: List<String>): Map<Pair<Int, Int>, Char> {
val grid = mutableMapOf<Pair<Int, Int>, Char>()

for (row in input.indices) {
for (col in input[row].indices) {
grid[Pair(row, col)] = input[row][col]
}
}

return grid
}

fun getPartNumbers(grid: Map<Pair<Int, Int>, Char>): List<PartNumber> {
val partNumbers = mutableListOf<PartNumber>()
val maxRow = grid.maxOf { it.key.first }
val maxCol = grid.maxOf { it.key.second }

for (row in 0..maxRow) {
var col = 0

while (col <= maxCol) {
var currentNumber = 0
var startCol = 0
var endCol = 0

while (grid[Pair(row, col)]?.isDigit() == true) {
if (currentNumber == 0) {
currentNumber = grid[Pair(row, col)]!!.digitToInt()
startCol = col
endCol = col
} else {
currentNumber = currentNumber * 10 + grid[Pair(row, col)]!!.digitToInt()
endCol = col
}

col++
}

if (currentNumber != 0) {
partNumbers.add(PartNumber(currentNumber, row, startCol, endCol))
}

col++
}
}

return partNumbers.filter { it.isAdjacentToSymbol(grid) }
}

fun symbolIsAdjacentToTwoPartNumbers(partNumbers: List<PartNumber>, symbol: Map.Entry<Pair<Int, Int>, Char>) =
partNumbers.count { it.isAdjacentTo(symbol.key) } == 2

fun getAdjacentPartNumbers(partNumbers: List<PartNumber>, symbol: Map.Entry<Pair<Int, Int>, Char>) =
partNumbers.filter { it.isAdjacentTo(symbol.key) }
82 changes: 0 additions & 82 deletions src/main/kotlin/day03/main.kt

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
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()}")
}
62 changes: 62 additions & 0 deletions src/test/kotlin/day03/Day03Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package day03

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

class Day03Tests {
@Test
fun `part1 example`() {
// arrange
val input = listOf(
"467..114..",
"...*......",
"..35..633.",
"......#...",
"617*......",
".....+.58.",
"..592.....",
"......755.",
"...$.*....",
".664.598..",
)

// act
val actual = part1(input)

// assert
assertEquals(4361, actual)
}

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

@Test
fun `part2 example`() {
// arrange
val input = listOf(
"467..114..",
"...*......",
"..35..633.",
"......#...",
"617*......",
".....+.58.",
"..592.....",
"......755.",
"...$.*....",
".664.598..",
)

// act
val actual = part2(input)

// assert
assertEquals(467835, actual)
}

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

0 comments on commit db7292a

Please sign in to comment.