Day 5 finaLLY
This commit is contained in:
parent
3a4b38f80f
commit
4ae0f0c1b3
2 changed files with 41 additions and 0 deletions
40
aoc/src/main/scala/aoc/y2018/Day05.scala
Normal file
40
aoc/src/main/scala/aoc/y2018/Day05.scala
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ package object y2018 extends Year {
|
||||||
"2" -> Day02,
|
"2" -> Day02,
|
||||||
"3" -> Day03,
|
"3" -> Day03,
|
||||||
"4" -> Day04,
|
"4" -> Day04,
|
||||||
|
"5" -> Day05
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue