diff --git a/day8/part1.kts b/day8/part1.kts new file mode 100755 index 0000000..f9f26e8 --- /dev/null +++ b/day8/part1.kts @@ -0,0 +1,52 @@ +#!/usr/bin/env kotlin + +import java.io.File + +val frequencies = mutableMapOf>>() +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>() +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) diff --git a/day8/part2.kts b/day8/part2.kts new file mode 100755 index 0000000..ee6810c --- /dev/null +++ b/day8/part2.kts @@ -0,0 +1,49 @@ +#!/usr/bin/env kotlin + +import java.io.File + +val frequencies = mutableMapOf>>() +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>() +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..