aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md58
-rw-r--r--src/Day05.kt111
2 files changed, 140 insertions, 29 deletions
diff --git a/README.md b/README.md
index d1fae65..6c7c829 100644
--- a/README.md
+++ b/README.md
@@ -1,38 +1,38 @@
# Advent of Code 2021 in Kotlin
![Kotlin](https://img.shields.io/badge/Kotlin-grey?logo=Kotlin)
-![](https://img.shields.io/badge/⭐%20stars-8-yellow)
-![](https://img.shields.io/badge/📅%20days-4-blue)
+![](https://img.shields.io/badge/⭐%20stars-10-yellow)
+![](https://img.shields.io/badge/📅%20days-5-blue)
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))
+}