aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2021-12-14 18:43:35 +0100
committertchojnacki <tomaszchojnacki2001@gmail.com>2021-12-14 18:43:35 +0100
commitc37a69ec2c26ad025956bb14a4c88c15e295b36d (patch)
tree2a3dd7f4a3c860da87766678dd8d4d2cdcbb8cd6
parentd5e470e26936dbdb321ad8b30a44728f2512c378 (diff)
downloadgleam_aoc2020-c37a69ec2c26ad025956bb14a4c88c15e295b36d.tar.gz
gleam_aoc2020-c37a69ec2c26ad025956bb14a4c88c15e295b36d.zip
Finish day 14
-rw-r--r--README.md58
-rw-r--r--src/Day14.kt45
2 files changed, 74 insertions, 29 deletions
diff --git a/README.md b/README.md
index 4ef203e..cdd7198 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-24-yellow)
-![](https://img.shields.io/badge/📅%20days-12-blue)
+![](https://img.shields.io/badge/⭐%20stars-26-yellow)
+![](https://img.shields.io/badge/📅%20days-13-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: The Treachery of Whales | 🌟 | 🌟 |
-| Day 8: Seven Segment Search | 🌟 | 🌟 |
-| Day 9: Smoke Basin | 🌟 | 🌟 |
-| Day 10: Syntax Scoring | 🌟 | 🌟 |
-| Day 11: Dumbo Octopus | 🌟 | 🌟 |
-| Day 12: Passage Pathing | 🌟 | 🌟 |
-| 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: Seven Segment Search | 🌟 | 🌟 |
+| Day 9: Smoke Basin | 🌟 | 🌟 |
+| Day 10: Syntax Scoring | 🌟 | 🌟 |
+| Day 11: Dumbo Octopus | 🌟 | 🌟 |
+| Day 12: Passage Pathing | 🌟 | 🌟 |
+| Day 13: Transparent Origami | | |
+| Day 14: Extended Polymerization | 🌟 | 🌟 |
+| 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/Day14.kt b/src/Day14.kt
new file mode 100644
index 0000000..fb6a43e
--- /dev/null
+++ b/src/Day14.kt
@@ -0,0 +1,45 @@
+fun getPolymerLetterCounts(input: List<String>, iterations: Int): Long {
+ val template = input.first()
+ val rules = input.drop(2).associate {
+ val (pattern, replacement) = it.split(" -> ")
+ (pattern[0] to pattern[1]) to replacement.first()
+ }
+
+ var pairCounts = template
+ .zipWithNext()
+ .groupingBy { it }
+ .eachCount()
+ .mapValues { (_, v) -> v.toLong() }
+
+ repeat(iterations) {
+ val newCounts = mutableMapOf<Pair<Char, Char>, Long>()
+
+ pairCounts.forEach { (pair, count) ->
+ newCounts.merge(rules[pair]!! to pair.second, count, Long::plus)
+ newCounts.merge(pair.first to rules[pair]!!, count, Long::plus)
+ }
+
+ pairCounts = newCounts
+ }
+
+ val letterCounts = mutableMapOf<Char, Long>()
+ pairCounts.forEach { (pair, count) -> letterCounts.merge(pair.second, count, Long::plus) }
+ letterCounts.merge(template.first(), 1, Long::plus)
+
+ return letterCounts.values.let { it.maxOrNull()!! - it.minOrNull()!! }
+}
+
+fun main() {
+ fun part1(input: List<String>): Long = getPolymerLetterCounts(input, 10)
+
+ fun part2(input: List<String>): Long = getPolymerLetterCounts(input, 40)
+
+
+ val testInput = readInputAsLines("Day14_test")
+ check(part1(testInput) == 1588L)
+ check(part2(testInput) == 2188189693529L)
+
+ val input = readInputAsLines("Day14")
+ println(part1(input))
+ println(part2(input))
+}