Initial commit
This commit is contained in:
commit
cd6bd4b488
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.gradle/
|
||||
build
|
||||
*.log
|
21
build.gradle.kts
Normal file
21
build.gradle.kts
Normal file
@ -0,0 +1,21 @@
|
||||
val arrowVersion = "0.10.3"
|
||||
|
||||
plugins {
|
||||
id("org.jetbrains.kotlin.jvm") version "1.3.60"
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
maven("https://dl.bintray.com/arrow-kt/arrow-kt/")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
|
||||
implementation("io.arrow-kt:arrow-core:$arrowVersion")
|
||||
implementation("io.arrow-kt:arrow-fx:$arrowVersion")
|
||||
}
|
||||
|
||||
application {
|
||||
mainClassName = "moe.kageru.spektacle.SpektacleKt"
|
||||
}
|
1
settings.gradle.kts
Normal file
1
settings.gradle.kts
Normal file
@ -0,0 +1 @@
|
||||
rootProject.name = "advent-of-code"
|
11
src/main/kotlin/moe/kageru/spektacles/IoHandler.kt
Normal file
11
src/main/kotlin/moe/kageru/spektacles/IoHandler.kt
Normal file
@ -0,0 +1,11 @@
|
||||
package moe.kageru.spektacles
|
||||
|
||||
import java.io.File
|
||||
|
||||
object IoHandler {
|
||||
fun readFile(name: String): Lines = File(name).readLines().asSequence()
|
||||
.filter { it.isNotBlank() }
|
||||
.filter { !it.startsWith("****") }
|
||||
}
|
||||
|
||||
typealias Lines = Sequence<String>
|
48
src/main/kotlin/moe/kageru/spektacles/ParsedLine.kt
Normal file
48
src/main/kotlin/moe/kageru/spektacles/ParsedLine.kt
Normal file
@ -0,0 +1,48 @@
|
||||
package moe.kageru.spektacles
|
||||
|
||||
import arrow.core.Option
|
||||
import arrow.core.toOption
|
||||
import java.time.LocalDateTime
|
||||
|
||||
class ParsedLine(val time: LocalDateTime,
|
||||
val user: Option<String>,
|
||||
val userMode: Option<UserMode>,
|
||||
val message: String,
|
||||
val isSystemMessage: Boolean,
|
||||
private val originalMessage: String
|
||||
) {
|
||||
override fun toString() = originalMessage
|
||||
|
||||
companion object {
|
||||
// 18 04 26 19:40:47 <kageru_> Hi
|
||||
private val lineRegex = Regex("""(?<year>\d\d)\s(?<month>\d\d)\s(?<day>\d\d)\s(?<hour>\d\d):(?<minute>\d\d):(?<second>\d\d)\s(<(?<mode>[~&@%+])?(?<author>\w+)>\t)?(?<message>.*)""")
|
||||
|
||||
fun parse(line: String): ParsedLine? = lineRegex
|
||||
.matchEntire(line)
|
||||
?.let {
|
||||
ParsedLine(
|
||||
LocalDateTime.of(
|
||||
it.groups["year"]!!.value.toInt(),
|
||||
it.groups["month"]!!.value.toInt(),
|
||||
it.groups["day"]!!.value.toInt(),
|
||||
it.groups["hour"]!!.value.toInt(),
|
||||
it.groups["minute"]!!.value.toInt(),
|
||||
it.groups["second"]!!.value.toInt()
|
||||
),
|
||||
it.groups["author"]?.value.toOption(),
|
||||
it.groups["mode"]?.value?.let { UserMode.values().first { mode -> mode.icon == it } }.toOption(),
|
||||
it.groups["message"]!!.value,
|
||||
it.groups["author"] == null,
|
||||
it.value
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class UserMode(val icon: String) {
|
||||
ADMIN("~"),
|
||||
BOT("&"),
|
||||
OP("@"),
|
||||
HOP("%"),
|
||||
VOICE("+");
|
||||
}
|
32
src/main/kotlin/moe/kageru/spektacles/Spektacles.kt
Normal file
32
src/main/kotlin/moe/kageru/spektacles/Spektacles.kt
Normal file
@ -0,0 +1,32 @@
|
||||
package moe.kageru.spektacles
|
||||
|
||||
import kotlin.system.measureTimeMillis
|
||||
|
||||
|
||||
fun main() {
|
||||
val res = measureTimeMillis {
|
||||
IoHandler.readFile("test.log")
|
||||
.map { ParsedLine.parse(it) }
|
||||
.filterNotNull()
|
||||
.filter { line -> line.user.exists { it == "kageru_" } }
|
||||
.flatMap { it.message.split(' ').asSequence() }
|
||||
//.flatMap { it.message.split(' ').k() }
|
||||
//.toCounter()
|
||||
//.counts["selphyDango"]
|
||||
}
|
||||
println(res)
|
||||
}
|
||||
|
||||
class Counter<T>(source: Iterable<T>) : Iterable<T> {
|
||||
val counts = mutableMapOf<T, Int>().apply {
|
||||
for (element in source) {
|
||||
this[element] = this.getOrDefault(element, 0) + 1
|
||||
}
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> {
|
||||
return counts.keys.iterator()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> Iterable<T>.toCounter(): Counter<T> = Counter(this)
|
Loading…
Reference in New Issue
Block a user