2018 day 2
This commit is contained in:
parent
da03fe8314
commit
1968911351
3 changed files with 32 additions and 1 deletions
|
@ -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
|
||||
|
||||
}
|
||||
|
|
22
aoc/src/main/scala/aoc/y2018/Day02.scala
Normal file
22
aoc/src/main/scala/aoc/y2018/Day02.scala
Normal 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
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,8 @@ package aoc
|
|||
package object y2018 extends Year {
|
||||
|
||||
override def days: Map[String, Day] = Map(
|
||||
"1" -> Day01
|
||||
"1" -> Day01,
|
||||
"2" -> Day02
|
||||
)
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue