generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.kt
30 lines (25 loc) · 997 Bytes
/
Day05.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import kotlin.math.abs
fun main() {
fun countCells(input: List<String>, countDiagonals: Boolean): Int {
val grid = Array(1000) { Array(1000) { 0 } }
input.forEach {
val (x1, y1, x2, y2) = it.split(Regex("[^0-9]+")).map(String::toInt)
val (dx, dy) = x2 - x1 to y2 - y1
for (i in 0..maxOf(abs(dx), abs(dy))) {
val x = x1 + i * (if (dx == 0) 0 else dx / abs(dx))
val y = y1 + i * (if (dy == 0) 0 else dy / abs(dy))
if (dx == 0 || dy == 0 || countDiagonals)
grid[x][y]++
}
}
return grid.flatten().count { it >= 2 }
}
fun part1(input: List<String>) = countCells(input, false)
fun part2(input: List<String>) = countCells(input, true)
val testInput = readInput("Day05_test")
check(part1(testInput) == 5)
check(part2(testInput) == 12)
val input = readInput("Day05")
println(part1(input))
println(part2(input))
}