Day 8
This commit is contained in:
parent
f248ccde17
commit
e8c4abc6a3
2 changed files with 101 additions and 0 deletions
52
day8/part1.kts
Executable file
52
day8/part1.kts
Executable file
|
@ -0,0 +1,52 @@
|
||||||
|
#!/usr/bin/env kotlin
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
val frequencies = mutableMapOf<Char, MutableList<Pair<Int, Int>>>()
|
||||||
|
var gridWidth = 0
|
||||||
|
var gridHeight = 0
|
||||||
|
File("./input.txt").readLines().also {
|
||||||
|
gridHeight = it.size
|
||||||
|
}.forEachIndexed { i, line ->
|
||||||
|
line.also { gridWidth = it.length }.forEachIndexed chars@{ j, freq ->
|
||||||
|
if (freq == '.') return@chars
|
||||||
|
val curValue = frequencies.putIfAbsent(
|
||||||
|
freq, mutableListOf(Pair(i, j))
|
||||||
|
)
|
||||||
|
if (curValue !== null) {
|
||||||
|
curValue.add(Pair(i, j))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val antiNodes = hashSetOf<Pair<Int, Int>>()
|
||||||
|
frequencies.forEach { (_, points) ->
|
||||||
|
points.forEach outer@{ point1 ->
|
||||||
|
points.forEach { point2 ->
|
||||||
|
if (point1 == point2) return@outer
|
||||||
|
val dRow = (point1.first - point2.first)
|
||||||
|
val dCol = (point1.second - point2.second)
|
||||||
|
|
||||||
|
val antiNode1 = Pair(point1.first + dRow, point1.second + dCol)
|
||||||
|
val antiNode2 = Pair(point2.first - dRow, point2.second - dCol)
|
||||||
|
|
||||||
|
if (
|
||||||
|
antiNode1.first >= 0
|
||||||
|
&& antiNode1.second >= 0
|
||||||
|
&& antiNode1.first < gridHeight
|
||||||
|
&& antiNode1.second < gridWidth) {
|
||||||
|
antiNodes.add(antiNode1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
antiNode2.first >= 0
|
||||||
|
&& antiNode2.second >= 0
|
||||||
|
&& antiNode2.first < gridHeight
|
||||||
|
&& antiNode2.second < gridWidth) {
|
||||||
|
antiNodes.add(antiNode2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println(antiNodes.size)
|
49
day8/part2.kts
Executable file
49
day8/part2.kts
Executable file
|
@ -0,0 +1,49 @@
|
||||||
|
#!/usr/bin/env kotlin
|
||||||
|
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
val frequencies = mutableMapOf<Char, MutableList<Pair<Int, Int>>>()
|
||||||
|
var gridWidth = 0
|
||||||
|
var gridHeight = 0
|
||||||
|
File("./input.txt").readLines().also {
|
||||||
|
gridHeight = it.size
|
||||||
|
}.forEachIndexed { i, line ->
|
||||||
|
line.also { gridWidth = it.length }.forEachIndexed chars@{ j, freq ->
|
||||||
|
if (freq == '.') return@chars
|
||||||
|
val curValue = frequencies.putIfAbsent(
|
||||||
|
freq, mutableListOf(Pair(i, j))
|
||||||
|
)
|
||||||
|
if (curValue !== null) {
|
||||||
|
curValue.add(Pair(i, j))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val antiNodes = hashSetOf<Pair<Int, Int>>()
|
||||||
|
frequencies.forEach { (_, points) ->
|
||||||
|
points.forEach outer@{ point1 ->
|
||||||
|
points.forEach { point2 ->
|
||||||
|
if (point1 == point2) return@outer
|
||||||
|
val dRow = (point1.first - point2.first)
|
||||||
|
val dCol = (point1.second - point2.second)
|
||||||
|
|
||||||
|
var curRow = point1.first
|
||||||
|
var curCol = point1.second
|
||||||
|
while (curRow in 0..<gridHeight && curCol in 0..<gridWidth) {
|
||||||
|
antiNodes.add(Pair(curRow, curCol))
|
||||||
|
curRow += dRow
|
||||||
|
curCol += dCol
|
||||||
|
}
|
||||||
|
|
||||||
|
curRow = point2.first
|
||||||
|
curCol = point2.second
|
||||||
|
while (curRow in 0..<gridHeight && curCol in 0..<gridWidth) {
|
||||||
|
antiNodes.add(Pair(curRow, curCol))
|
||||||
|
curRow -= dRow
|
||||||
|
curCol -= dCol
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println(antiNodes.size)
|
Loading…
Reference in a new issue