Skip to content

Commit

Permalink
Add Day8
Browse files Browse the repository at this point in the history
  • Loading branch information
derNiklaas committed Dec 8, 2024
1 parent cc7b68b commit fc85cd9
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/kotlin/Day08.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import kotlin.math.max
import utils.AoCDay
import utils.Vec2D

class Day08 : AoCDay() {
val map = mutableMapOf<Char, List<Vec2D>>()

init {
for (y in input.indices) {
for (x in input[0].indices) {
val char = input[y][x]
if (char == '.') continue
if (char !in map) map[char] = mutableListOf()
map[char] = map[char]!! + Vec2D(x, y)
}
}
}

override fun part1(): Any {
return solve()
}

override fun part2(): Any {
return solve(true)
}

private fun solve(partB: Boolean = false): Int {
val antiNodes = mutableSetOf<Vec2D>()
val maxSize = if (partB) max(input.size, input[0].length) else 1

for ((_, positions) in map) {
for (posA in positions) {
for (posB in positions) {
if (posA == posB) continue

repeat(maxSize + 1) { i ->
if (!partB && i == 0) return@repeat
antiNodes += posA + (posA - posB) * i
antiNodes += posB + (posB - posA) * i
}

}
}
}
return antiNodes.filter { it.y in input.indices && it.x in input[0].indices }.size
}
}

fun main() {
Day08().execute()
}

0 comments on commit fc85cd9

Please sign in to comment.