-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from jwnace/day03-add-tests
added tests for day 3
- Loading branch information
Showing
6 changed files
with
155 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |