commit cd6bd4b488dc3eb8700b1e3643a58aa6400fbaaf Author: kageru Date: Tue Nov 26 10:50:38 2019 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..085478b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.gradle/ +build +*.log diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..352ea89 --- /dev/null +++ b/build.gradle.kts @@ -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" +} diff --git a/settings.gradle.kts b/settings.gradle.kts new file mode 100644 index 0000000..c9fb7c4 --- /dev/null +++ b/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "advent-of-code" diff --git a/src/main/kotlin/moe/kageru/spektacles/IoHandler.kt b/src/main/kotlin/moe/kageru/spektacles/IoHandler.kt new file mode 100644 index 0000000..671cc92 --- /dev/null +++ b/src/main/kotlin/moe/kageru/spektacles/IoHandler.kt @@ -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 \ No newline at end of file diff --git a/src/main/kotlin/moe/kageru/spektacles/ParsedLine.kt b/src/main/kotlin/moe/kageru/spektacles/ParsedLine.kt new file mode 100644 index 0000000..2471a18 --- /dev/null +++ b/src/main/kotlin/moe/kageru/spektacles/ParsedLine.kt @@ -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, + val userMode: Option, + val message: String, + val isSystemMessage: Boolean, + private val originalMessage: String +) { + override fun toString() = originalMessage + + companion object { + // 18 04 26 19:40:47 Hi + private val lineRegex = Regex("""(?\d\d)\s(?\d\d)\s(?\d\d)\s(?\d\d):(?\d\d):(?\d\d)\s(<(?[~&@%+])?(?\w+)>\t)?(?.*)""") + + 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("+"); +} \ No newline at end of file diff --git a/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt b/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt new file mode 100644 index 0000000..8df69fd --- /dev/null +++ b/src/main/kotlin/moe/kageru/spektacles/Spektacles.kt @@ -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(source: Iterable) : Iterable { + val counts = mutableMapOf().apply { + for (element in source) { + this[element] = this.getOrDefault(element, 0) + 1 + } + } + + override fun iterator(): Iterator { + return counts.keys.iterator() + } +} + +fun Iterable.toCounter(): Counter = Counter(this)