Created
December 3, 2021 20:21
-
-
Save 8bitreid/b209b2021b11ac721150c0d2668764cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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