Edgars
12/04/2020, 6:52 PMEdgars
12/04/2020, 6:53 PMsplit { it.isBlank() }
to group items in a List<String>
to List<List<String>>
. But in the end I used fold
. https://github.com/edgars-supe/advent-of-code/blob/master/src/main/kotlin/lv/esupe/aoc/year2020/Day4.ktadamratzman
12/04/2020, 6:53 PMNir
12/04/2020, 6:54 PMEdgars
12/04/2020, 6:54 PMList<String>
.Nir
12/04/2020, 6:55 PMadamratzman
12/04/2020, 6:56 PMNir
12/04/2020, 6:57 PMNir
12/04/2020, 6:57 PMsplit
adamratzman
12/04/2020, 6:57 PMEdgars
12/04/2020, 6:58 PMEdgars
12/04/2020, 6:59 PMadamratzman
12/04/2020, 6:59 PMfun readInput(filename: String): String {
return File(ClassLoader.getSystemResource(filename).file).readText()
}
adamratzman
12/04/2020, 7:00 PMadamratzman
12/04/2020, 7:00 PMNir
12/04/2020, 7:01 PMNir
12/04/2020, 7:01 PMNir
12/04/2020, 7:02 PMNir
12/04/2020, 7:02 PMthis
Nir
12/04/2020, 7:03 PMfun<T, R> Sequence<T>.chunkedBy(key: (T) -> R) = sequence {
val seq = this@Sequence
}
Edgars
12/04/2020, 7:03 PMNir
12/04/2020, 7:03 PMadamratzman
12/04/2020, 7:03 PMNir
12/04/2020, 7:04 PMthis@Sequence
?Edgars
12/04/2020, 7:04 PMNir
12/04/2020, 7:04 PMadamratzman
12/04/2020, 7:04 PMNir
12/04/2020, 7:05 PMadamratzman
12/04/2020, 7:05 PMadamratzman
12/04/2020, 7:05 PMNir
12/04/2020, 7:05 PMNir
12/04/2020, 7:05 PMNir
12/04/2020, 7:05 PMadamratzman
12/04/2020, 7:06 PMNir
12/04/2020, 7:06 PMval aocDataDir = File("C:\\Users\\quick\\Downloads\\advent_of_code")
operator fun File.div(s: String) = resolve(s)
fun<K, V> MutableMap<K, V>.update(k: K, transform: (V?) -> V) { this[k] = transform(this[k]) }
fun String.popSuffix(suffix: String) = if (endsWith(suffix)) removeSuffix(suffix) else null
Nir
12/04/2020, 7:07 PMNir
12/04/2020, 7:07 PMEdgars
12/04/2020, 7:10 PMPair<Int, Int>
is not always a point. I mean, if you're the only dev, you know what's what and what a pair of Ints is for and whatever. Feels cleaner to me like this, though.todd.ginsberg
12/04/2020, 7:10 PMPoint2D
and Point3D
, but I kind of like rethinking them for the problem at hand each year.adamratzman
12/04/2020, 7:14 PMadamratzman
12/04/2020, 7:14 PMval lines = """not blank
|line
|
|^ that was blank
| so chunk
|
|chunk
|
""".trimMargin().split("\n")
println(lines.chunkedBy { it.isBlank() })
Outputs
[[not blank, line], [^ that was blank, so chunk], [chunk]]
Edgars
12/04/2020, 7:19 PMNir
12/04/2020, 7:19 PMNir
12/04/2020, 7:19 PMNir
12/04/2020, 7:19 PMfun<T, R> Sequence<T>.chunkedBy(key: (T) -> R) = sequence {
val iter = this@chunkedBy.iterator()
if (!iter.hasNext()) return@sequence
var currentList = mutableListOf(iter.next())
var currentKey = key(currentList.first())
for (t in iter) {
val nextKey = key(t)
if (nextKey == currentKey) {
currentList.add(t)
continue
}
yield(currentKey to currentList)
currentKey = nextKey
currentList = mutableListOf(t)
}
yield(currentKey to currentList)
}
adamratzman
12/04/2020, 7:20 PMfun<T> List<T>.chunkedBy(key: (T) -> Boolean): List<List<T>> {
val chunks = mutableListOf<List<T>>()
var startingIndex = 0
forEachIndexed { index, t ->
if (key(t)) {
chunks += subList(startingIndex, index)
startingIndex = index + 1
}
}
if (startingIndex < size) chunks += subList(startingIndex, size)
return chunks
}
adamratzman
12/04/2020, 7:20 PMNir
12/04/2020, 7:21 PMNir
12/04/2020, 7:22 PMNir
12/04/2020, 7:22 PMsequenceOf("hello", "there", "", "bye", "there").chunkedBy {it.isEmpty()}.toList().also { println(it) }
Nir
12/04/2020, 7:22 PM[(false, [hello, there]), (true, []), (false, [bye, there])]
Nir
12/04/2020, 7:22 PMNir
12/04/2020, 7:23 PMNir
12/04/2020, 7:23 PMadamratzman
12/04/2020, 7:24 PMNir
12/04/2020, 7:31 PMNir
12/04/2020, 7:31 PMadamratzman
12/04/2020, 7:33 PMfun <T, R> List<T>.chunkedBy(key: (T) -> R): List<Pair<R, List<T>>> {
val chunks = mutableListOf<Pair<R, List<T>>>()
var startingIndex = 0
forEachIndexed { index, t ->
val value = key(t)
if (value != key(this[startingIndex])) {
chunks += key(this[startingIndex]) to subList(startingIndex, index)
startingIndex = index
}
}
if (startingIndex < size) chunks += key(this[startingIndex]) to subList(startingIndex, size)
return chunks
}