-
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.
- Loading branch information
1 parent
cc7b68b
commit fc85cd9
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
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,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() | ||
} |