Doháním deficit a zde je řešení pro den 2.
A:
fun hasSameLettersCount(id: String, occurrences: Int): Boolean {
val sameLetterCounts = id.groupBy { it }
.values
.map { it.count() }
return occurrences in sameLetterCounts
}
fun main() {
val ids = File("input2.txt").readLines()
val twoCounts = ids.filter { hasSameLettersCount(it, 2) }.count()
val threeCounts = ids.filter { hasSameLettersCount(it, 3) }.count()
println(twoCounts * threeCounts)
}
B:
fun main() {
val ids = File("input2.txt").readLines().toSet()
for (idA in ids) {
for (idB in ids) {
val diff = getDiff(idA, idB)
if (diff.length == idA.length - 1) {
println(diff)
return
}
}
}
}
fun getDiff(first: String, second: String) = first.zip(second) { a, b ->
if (a == b) a.toString() else ""
}.joinToString("")