aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2021-12-06 09:34:30 +0100
committertchojnacki <tomaszchojnacki2001@gmail.com>2021-12-06 09:34:30 +0100
commit8f84c868b8d65b714447155b301acc6db4e992b2 (patch)
tree0230ab81c75cb5dffdab26d79fbdfb4aedfd7dd3
parent803778486227cc337aec207b90e63e53968244e5 (diff)
downloadgleam_aoc2020-8f84c868b8d65b714447155b301acc6db4e992b2.tar.gz
gleam_aoc2020-8f84c868b8d65b714447155b301acc6db4e992b2.zip
Initial day 6 solution
-rw-r--r--README.md58
-rw-r--r--src/Day06.kt58
2 files changed, 87 insertions, 29 deletions
diff --git a/README.md b/README.md
index 6c7c829..7112fc4 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-10-yellow)
-![](https://img.shields.io/badge/📅%20days-5-blue)
+![](https://img.shields.io/badge/⭐%20stars-12-yellow)
+![](https://img.shields.io/badge/📅%20days-6-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: ??? | | |
-| 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: ??? | | |
+| 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/Day06.kt b/src/Day06.kt
new file mode 100644
index 0000000..d192c72
--- /dev/null
+++ b/src/Day06.kt
@@ -0,0 +1,58 @@
+fun main() {
+ fun part1(input: String): Int {
+ var fish = input.trim().split(",").map(String::toInt)
+
+ repeat(80) {
+ val pendingFish = mutableListOf<Int>()
+
+ fish = fish.map {
+ when (it) {
+ 0 -> {
+ pendingFish.add(8)
+ 6
+ }
+ else -> {
+ it - 1
+ }
+ }
+ } + pendingFish
+ }
+
+ return fish.size
+ }
+
+ fun part2(input: String): Long {
+ val fishCounts =
+ input
+ .trim()
+ .split(",")
+ .map(String::toInt)
+ .groupingBy { it }
+ .eachCount()
+ .mapValues { (_, v) -> v.toLong() }
+ .toMutableMap()
+
+ repeat(256) {
+ val readyToBirth = fishCounts.getOrDefault(0, 0)
+ fishCounts[0] = fishCounts.getOrDefault(1, 0)
+ fishCounts[1] = fishCounts.getOrDefault(2, 0)
+ fishCounts[2] = fishCounts.getOrDefault(3, 0)
+ fishCounts[3] = fishCounts.getOrDefault(4, 0)
+ fishCounts[4] = fishCounts.getOrDefault(5, 0)
+ fishCounts[5] = fishCounts.getOrDefault(6, 0)
+ fishCounts[6] = fishCounts.getOrDefault(7, 0) + readyToBirth
+ fishCounts[7] = fishCounts.getOrDefault(8, 0)
+ fishCounts[8] = readyToBirth
+ }
+
+ return fishCounts.values.sum()
+ }
+
+ val testInput = readInputAsString("Day06_test")
+ check(part1(testInput) == 5934)
+ check(part2(testInput) == 26984457539)
+
+ val input = readInputAsString("Day06")
+ println(part1(input))
+ println(part2(input))
+}