diff options
author | tchojnacki <tomaszchojnacki2001@gmail.com> | 2021-12-05 18:06:12 +0100 |
---|---|---|
committer | tchojnacki <tomaszchojnacki2001@gmail.com> | 2021-12-05 18:06:12 +0100 |
commit | a6434013b218a21cfaac50cb1665fdef08531992 (patch) | |
tree | 53f9f471329520fb7e641f70e97b6c39fca7bcbb | |
parent | e6fb00d890b406c074bc9c1e271fcb623d583165 (diff) | |
download | gleam_aoc2020-a6434013b218a21cfaac50cb1665fdef08531992.tar.gz gleam_aoc2020-a6434013b218a21cfaac50cb1665fdef08531992.zip |
Initial day 5 implementation
-rw-r--r-- | README.md | 58 | ||||
-rw-r--r-- | src/Day05.kt | 111 |
2 files changed, 140 insertions, 29 deletions
@@ -1,38 +1,38 @@ # Advent of Code 2021 in Kotlin  - - + + Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][github] using the [Advent of Code Kotlin Template][template] delivered by JetBrains. ## Progress -| Day | Part 1 | Part 2 | -|--------------------------|:------:|:------:| -| Day 1: Sonar Sweep | 🌟 | 🌟 | -| Day 2: Dive! | 🌟 | 🌟 | -| Day 3: Binary Diagnostic | 🌟 | 🌟 | -| Day 4: Giant Squid | 🌟 | 🌟 | -| Day 5: ??? | | | -| Day 6: ??? | | | -| Day 7: ??? | | | -| Day 8: ??? | | | -| Day 9: ??? | | | -| Day 10: ??? | | | -| Day 11: ??? | | | -| Day 12: ??? | | | -| Day 13: ??? | | | -| Day 14: ??? | | | -| Day 15: ??? | | | -| Day 16: ??? | | | -| Day 17: ??? | | | -| Day 18: ??? | | | -| Day 19: ??? | | | -| Day 20: ??? | | | -| Day 21: ??? | | | -| Day 22: ??? | | | -| Day 23: ??? | | | -| Day 24: ??? | | | -| Day 25: ??? | | | +| Day | Part 1 | Part 2 | +|------------------------------|:------:|:------:| +| Day 1: Sonar Sweep | 🌟 | 🌟 | +| Day 2: Dive! | 🌟 | 🌟 | +| Day 3: Binary Diagnostic | 🌟 | 🌟 | +| Day 4: Giant Squid | 🌟 | 🌟 | +| Day 5: Hydrothermal Venture | 🌟 | 🌟 | +| Day 6: ??? | | | +| Day 7: ??? | | | +| Day 8: ??? | | | +| Day 9: ??? | | | +| Day 10: ??? | | | +| Day 11: ??? | | | +| Day 12: ??? | | | +| Day 13: ??? | | | +| Day 14: ??? | | | +| Day 15: ??? | | | +| Day 16: ??? | | | +| Day 17: ??? | | | +| Day 18: ??? | | | +| Day 19: ??? | | | +| Day 20: ??? | | | +| Day 21: ??? | | | +| Day 22: ??? | | | +| Day 23: ??? | | | +| Day 24: ??? | | | +| Day 25: ??? | | | [^aoc]: diff --git a/src/Day05.kt b/src/Day05.kt new file mode 100644 index 0000000..fece98e --- /dev/null +++ b/src/Day05.kt @@ -0,0 +1,111 @@ +import kotlin.math.absoluteValue +import kotlin.math.sign + +class Line(val start: Pair<Int, Int>, val end: Pair<Int, Int>) { + companion object { + fun fromString(input: String): Line { + val parts = input.split(" -> ").map { part -> + val coordinates = part.split(",").map { it.toInt() } + coordinates[0] to coordinates[1] + } + + return Line(parts[0], parts[1]) + } + } +} + +fun main() { + fun part1(input: List<String>): Int { + val lines = input.map(Line::fromString) + + val positions = mutableMapOf<Pair<Int, Int>, Int>() + lines.forEach { + when { + it.start.first == it.end.first -> { + if (it.start.second <= it.end.second) { + for (y in it.start.second..it.end.second) { + positions[it.start.first to y] = 1 + positions.getOrDefault(it.start.first to y, 0) + } + } else { + for (y in it.end.second..it.start.second) { + positions[it.start.first to y] = 1 + positions.getOrDefault(it.start.first to y, 0) + } + } + } + it.start.second == it.end.second -> { + if (it.start.first<= it.end.first) { + for (x in it.start.first..it.end.first) { + positions[x to it.start.second] = 1 + positions.getOrDefault(x to it.start.second, 0) + } + } else { + for (x in it.end.first..it.start.first) { + positions[x to it.start.second] = 1 + positions.getOrDefault(x to it.start.second, 0) + } + } + } + + } + } + + return positions.values.count { it >= 2 } + } + + + fun part2(input: List<String>): Int { + val lines = input.map(Line::fromString) + + val positions = mutableMapOf<Pair<Int, Int>, Int>() + lines.forEach { + when { + it.start.first == it.end.first -> { + if (it.start.second <= it.end.second) { + for (y in it.start.second..it.end.second) { + positions[it.start.first to y] = 1 + positions.getOrDefault(it.start.first to y, 0) + } + } else { + for (y in it.end.second..it.start.second) { + positions[it.start.first to y] = 1 + positions.getOrDefault(it.start.first to y, 0) + } + } + } + it.start.second == it.end.second -> { + if (it.start.first<= it.end.first) { + for (x in it.start.first..it.end.first) { + positions[x to it.start.second] = 1 + positions.getOrDefault(x to it.start.second, 0) + } + } else { + for (x in it.end.first..it.start.first) { + positions[x to it.start.second] = 1 + positions.getOrDefault(x to it.start.second, 0) + } + } + } + (it.end.first - it.start.first).absoluteValue == (it.end.second - it.start.second).absoluteValue -> { + val xOffset = it.end.first - it.start.first + val yOffset = it.end.second - it.start.second + + val xSign = xOffset.sign + val ySign = yOffset.sign + + val length = xOffset.absoluteValue + + for (i in 0..length) { + val x = it.start.first + xSign * i + val y = it.start.second + ySign * i + positions[x to y] = 1 + positions.getOrDefault(x to y, 0) + } + + } + } + } + + return positions.values.count { it >= 2 } + } + + val testInput = readInputAsLines("Day05_test") + check(part1(testInput) == 5) + check(part2(testInput) == 12) + + val input = readInputAsLines("Day05") + println(part1(input)) + println(part2(input)) +} |