aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2021-12-08 10:40:06 +0100
committertchojnacki <tomaszchojnacki2001@gmail.com>2021-12-08 10:40:06 +0100
commit67852df6b6b9bb816dceae94493457764a8a272d (patch)
tree1ebe871ee815eb7eaa50280619d74ec383bc97f6
parent7b2fce9c718eae56dc8a06aa4b4c4830dbd45e38 (diff)
downloadgleam_aoc2020-67852df6b6b9bb816dceae94493457764a8a272d.tar.gz
gleam_aoc2020-67852df6b6b9bb816dceae94493457764a8a272d.zip
Complete day 8
-rw-r--r--README.md6
-rw-r--r--src/Day08.kt50
2 files changed, 53 insertions, 3 deletions
diff --git a/README.md b/README.md
index c8b7ad3..1f33ce2 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
# Advent of Code 2021 in Kotlin
![Kotlin](https://img.shields.io/badge/Kotlin-grey?logo=Kotlin)
-![](https://img.shields.io/badge/⭐%20stars-14-yellow)
-![](https://img.shields.io/badge/📅%20days-7-blue)
+![](https://img.shields.io/badge/⭐%20stars-16-yellow)
+![](https://img.shields.io/badge/📅%20days-8-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.
@@ -15,7 +15,7 @@ Welcome to the Advent of Code[^aoc] Kotlin project created by [tchojnacki][githu
| Day 5: Hydrothermal Venture | 🌟 | 🌟 |
| Day 6: Lanternfish | 🌟 | 🌟 |
| Day 7: The Treachery of Whales | 🌟 | 🌟 |
-| Day 8: ??? | | |
+| Day 8: Seven Segment Search | 🌟 | 🌟 |
| Day 9: ??? | | |
| Day 10: ??? | | |
| Day 11: ??? | | |
diff --git a/src/Day08.kt b/src/Day08.kt
new file mode 100644
index 0000000..d0f050e
--- /dev/null
+++ b/src/Day08.kt
@@ -0,0 +1,50 @@
+fun main() {
+ fun part1(input: List<String>): Int =
+ input.sumOf {
+ it
+ .split("|")[1]
+ .trim()
+ .split(" ")
+ .map(String::length)
+ .count { len -> len in listOf(2, 3, 4, 7) }
+ }
+
+ fun part2(input: List<String>): Long = input.sumOf { line ->
+ val (patternString, outputString) = line.split("|").map(String::trim)
+ val patterns = patternString.split(" ").map(String::toSet)
+
+ val one = patterns.first { it.size == 2 }
+ val four = patterns.first { it.size == 4 }
+ val seven = patterns.first { it.size == 3 }
+ val eight = patterns.first { it.size == 7 }
+
+ val top = seven - one
+ val middle = patterns.filter { it.size == 5 }.reduce(Set<Char>::intersect) intersect four
+ val five = patterns.filter { it.size == 6 }.reduce(Set<Char>::intersect) + middle
+ val bottom = five - (four + top)
+ val nine = four + top + bottom
+ val lowerLeft = eight - nine
+ val six = five + lowerLeft
+ val lowerRight = one intersect six
+ val three = one + top + middle + bottom
+ val zero = eight - middle
+ val upperLeft = nine - three
+ val two = eight - (upperLeft + lowerRight)
+
+ val encodings = listOf(zero, one, two, three, four, five, six, seven, eight, nine)
+
+ outputString
+ .split(" ")
+ .joinToString("") { encodings.indexOf(it.toSet()).toString() }
+ .toLong()
+ }
+
+
+ val testInput = readInputAsLines("Day08_test")
+ check(part1(testInput) == 26)
+ check(part2(testInput) == 61229L)
+
+ val input = readInputAsLines("Day08")
+ println(part1(input))
+ println(part2(input))
+}