aboutsummaryrefslogtreecommitdiff
path: root/aoc-2021-kotlin/src/Day08.kt
diff options
context:
space:
mode:
authortchojnacki <tomaszchojnacki2001@gmail.com>2022-08-11 19:24:23 +0200
committertchojnacki <tomaszchojnacki2001@gmail.com>2022-08-11 19:24:23 +0200
commit0f1e145b80813ae2331b7dac5ace0c589654ad2a (patch)
tree25483b8239436dd5aed2fee8811caf0ba893c0bb /aoc-2021-kotlin/src/Day08.kt
parent85fb0396bed6a2129b12392941103924b1ab55be (diff)
downloadgleam_aoc2020-0f1e145b80813ae2331b7dac5ace0c589654ad2a.tar.gz
gleam_aoc2020-0f1e145b80813ae2331b7dac5ace0c589654ad2a.zip
Move subproject to avoid IntelliJ module name issues
Diffstat (limited to 'aoc-2021-kotlin/src/Day08.kt')
-rw-r--r--aoc-2021-kotlin/src/Day08.kt51
1 files changed, 51 insertions, 0 deletions
diff --git a/aoc-2021-kotlin/src/Day08.kt b/aoc-2021-kotlin/src/Day08.kt
new file mode 100644
index 0000000..b513352
--- /dev/null
+++ b/aoc-2021-kotlin/src/Day08.kt
@@ -0,0 +1,51 @@
+object Day08 {
+ 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()
+ }
+}
+
+fun main() {
+ val testInput = readInputAsLines("Day08_test")
+ check(Day08.part1(testInput) == 26)
+ check(Day08.part2(testInput) == 61229L)
+
+ val input = readInputAsLines("Day08")
+ println(Day08.part1(input))
+ println(Day08.part2(input))
+}