Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,29 @@
// version 1.1.4-3
import java.io.File
val r = Regex("[ ]+")
fun main(args: Array<String>) {
val lines = File("days_of_week.txt").readLines()
for ((i, line) in lines.withIndex()) {
if (line.trim().isEmpty()) {
println()
continue
}
val days = line.trim().split(r)
if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}")
if (days.distinct().size < 7) { // implies some days have the same name
println(" ∞ $line")
continue
}
var len = 1
while (true) {
if (days.map { it.take(len) }.distinct().size == 7) {
println("${"%2d".format(len)} $line")
break
}
len++
}
}
}

View file

@ -0,0 +1,34 @@
import java.io.File
import kotlin.math.max
fun getMinimumPrefixLength(distinctWords: Iterable<String>): Int {
return distinctWords
.sorted()
.fold(Pair("", 0),
{ (previousWord, minLength), currentWord ->
val firstNonMatchingIdx = previousWord
.asSequence()
.zip(currentWord.asSequence())
.indexOfFirst { it.first != it.second }
return@fold Pair(
currentWord,
max(
// firstNonMatchingIdx would be -1 iff previousWord is a prefix of currentWord
if (firstNonMatchingIdx != -1) firstNonMatchingIdx + 1 else previousWord.length + 1,
minLength)
)
})
.second
}
fun getMinimumPrefixLength(whitespaceSeparatedDistinctWords: String) =
getMinimumPrefixLength(whitespaceSeparatedDistinctWords.split(Regex("\\s+")))
fun main() {
File("/tmp/input.txt").useLines {
it.map(String::trim)
.filter(String::isNotEmpty)
.map({ "${getMinimumPrefixLength(it).toString().padEnd(5)} ${it}" })
.forEach(::println)
}
}