2018 day 2

This commit is contained in:
Aly 2018-12-01 21:17:45 -08:00
parent da03fe8314
commit 1968911351
No known key found for this signature in database
GPG Key ID: 555B7346639DDAC3
3 changed files with 32 additions and 1 deletions

View File

@ -30,4 +30,12 @@ package object aoc {
go(as, Set.empty)
}
def levenshtein(a: String, b: String): Int =
((0 to b.length).toList /: a)(
(prev, x) =>
(prev zip prev.tail zip b).scanLeft(prev.head + 1) {
case (h, ((d, v), y)) => Math.min(Math.min(h + 1, v + 1), d + (if (x == y) 0 else 1))
}
).last
}

View File

@ -0,0 +1,22 @@
package aoc.y2018
import aoc._
object Day02 extends Day {
override def part1(input: String): String = {
val lines = input.lines.toList
val groups = lines.map(_.groupBy(identity))
val twos = groups.count(_.values.exists(_.length == 2))
val threes = groups.count(_.values.exists(_.length == 3))
(twos * threes).toString
}
override def part2(input: String): String = {
val lines = input.lines.toList
val distanceMap = lines.flatMap(s => lines.map(c => ((s, c), levenshtein(s, c)))).toMap
val ((s, c), _) = distanceMap.filter {case (_, d) => d > 0 }.minBy { case (_, d) => d }
s.intersect(c).mkString
}
}

View File

@ -3,7 +3,8 @@ package aoc
package object y2018 extends Year {
override def days: Map[String, Day] = Map(
"1" -> Day01
"1" -> Day01,
"2" -> Day02
)
}