aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2021-12-07 13:02:22 +0100
committertchojnacki <tomaszchojnacki2001@gmail.com>2021-12-07 13:02:22 +0100
commit7b2fce9c718eae56dc8a06aa4b4c4830dbd45e38 (patch)
treef53e29ba7c44260af026c75128ee5312165822e1
parent4db920b62c8d9f611d18d4c5402c1957ba9f162d (diff)
downloadgleam_aoc2020-7b2fce9c718eae56dc8a06aa4b4c4830dbd45e38.tar.gz
gleam_aoc2020-7b2fce9c718eae56dc8a06aa4b4c4830dbd45e38.zip
Complete day 7
-rw-r--r--README.md58
-rw-r--r--src/Day07.kt25
2 files changed, 54 insertions, 29 deletions
diff --git a/README.md b/README.md
index 7112fc4..c8b7ad3 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-12-yellow)
-![](https://img.shields.io/badge/📅%20days-6-blue)
+![](https://img.shields.io/badge/⭐%20stars-14-yellow)
+![](https://img.shields.io/badge/📅%20days-7-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: Hydrothermal Venture | 🌟 | 🌟 |
-| Day 6: Lanternfish | 🌟 | 🌟 |
-| 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: Lanternfish | 🌟 | 🌟 |
+| Day 7: The Treachery of Whales | 🌟 | 🌟 |
+| 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/Day07.kt b/src/Day07.kt
new file mode 100644
index 0000000..204dca4
--- /dev/null
+++ b/src/Day07.kt
@@ -0,0 +1,25 @@
+import kotlin.math.absoluteValue
+
+fun main() {
+ fun part1(input: String): Int {
+ val numbers = input.trim().split(",").map(String::toInt)
+ val range = numbers.minOrNull()!!..numbers.maxOrNull()!!
+
+ return range.minOf { n -> numbers.sumOf { (it - n).absoluteValue } }
+ }
+
+ fun part2(input: String): Int {
+ val numbers = input.trim().split(",").map(String::toInt)
+ val range = numbers.minOrNull()!!..numbers.maxOrNull()!!
+
+ return range.minOf { n -> numbers.map { (it - n).absoluteValue }.sumOf { (it * (it + 1)) / 2 } }
+ }
+
+ val testInput = readInputAsString("Day07_test")
+ check(part1(testInput) == 37)
+ check(part2(testInput) == 168)
+
+ val input = readInputAsString("Day07")
+ println(part1(input))
+ println(part2(input))
+}