Day 5 finaLLY

This commit is contained in:
Aly 2018-12-04 22:24:35 -08:00
parent 3a4b38f80f
commit 4ae0f0c1b3
No known key found for this signature in database
GPG key ID: 555B7346639DDAC3
2 changed files with 41 additions and 0 deletions

View file

@ -0,0 +1,40 @@
package aoc.y2018
import aoc.Day
object Day05 extends Day {
def pair(c1: Char, c2: Char): Boolean = {
val c1l = c1.isLower
val c2l = c2.isLower
(c1l && !c2l && c1 == c2.toLower) || (!c1l && c2l && c1.toLower == c2)
}
def simpleLength(s: String): Int = {
s.foldLeft("") {
case (stack, ne) => {
if (stack.isEmpty) {
ne +: stack
} else {
val peek = stack.head
if (pair(ne, peek)) {
stack.tail
} else {
ne +: stack
}
}
}
}
.length
}
override def part1(input: String): String = {
simpleLength(input).toString
}
override def part2(input: String): String = {
val allChars = input.map(_.toLower).distinct.sortBy(c => input.count(_ == c))
val mapped = allChars.par.map(c => simpleLength(input.filter(_.toLower != c)))
mapped.min.toString
}
}

View file

@ -7,6 +7,7 @@ package object y2018 extends Year {
"2" -> Day02, "2" -> Day02,
"3" -> Day03, "3" -> Day03,
"4" -> Day04, "4" -> Day04,
"5" -> Day05
) )
} }