Skip to content

Instantly share code, notes, and snippets.

@8bitreid
Created December 3, 2021 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 8bitreid/b209b2021b11ac721150c0d2668764cd to your computer and use it in GitHub Desktop.
Save 8bitreid/b209b2021b11ac721150c0d2668764cd to your computer and use it in GitHub Desktop.
def findMax(xs: List[String], index: Int): Char = {
xs.groupBy(line => line(index)).maxBy(map => map._2.length)._1
}
def lifeSupportRating(report: List[String]): Int = {
val oxygenPred: (Char, Char) => Boolean = (left, right) => left == right
val co2Pred: (Char, Char) => Boolean = (left, right) => left != right
@tailrec
def filterUntil(ratings: List[String], index: Int)(pred: (Char, Char) => Boolean): String = {
if (ratings.length == 1) ratings.head
else {
val maxForFiltered = findMax(ratings, index)
val cleaned = ratings.filter(line => pred(line(index), maxForFiltered))
filterUntil(cleaned, index + 1)(pred)
}
}
List(oxygenPred, co2Pred)
.map(filterUntil(report, 0)(_))
.map(Integer.parseInt(_, 2))
.product
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment