things
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
package be.vandewalleh.aoc.utils
|
||||
|
||||
import be.vandewalleh.aoc.utils.input.Input
|
||||
import jakarta.inject.Inject
|
||||
|
||||
abstract class BaseDay {
|
||||
|
||||
@Inject
|
||||
lateinit var input: Input
|
||||
|
||||
abstract fun part1(): Any
|
||||
abstract fun part2(): Any
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
package be.vandewalleh.aoc.utils.factory
|
||||
|
||||
import be.vandewalleh.aoc.utils.BaseDay
|
||||
import be.vandewalleh.aoc.utils.input.TempFileResourceLoader
|
||||
import io.micronaut.context.BeanContext
|
||||
import io.micronaut.inject.BeanDefinition
|
||||
@@ -9,8 +10,8 @@ import java.io.File
|
||||
|
||||
fun <T> createDay(beanType: Class<T>): T = BeanContext.run().getBean(beanType)
|
||||
|
||||
fun BeanContext.findDayDefinition(day: Int): BeanDefinition<*>? = allBeanDefinitions
|
||||
.find { it.name == "be.vandewalleh.aoc.days.Day${day.toString().padStart(length = 2, '0')}" }
|
||||
fun BeanContext.findDayDefinition(day: Int): BeanDefinition<BaseDay>? = allBeanDefinitions
|
||||
.find { it.name == "be.vandewalleh.aoc.days.Day${day.toString().padStart(length = 2, '0')}" } as BeanDefinition<BaseDay>?
|
||||
|
||||
inline fun <reified T> createDay() = createDay(T::class.java)
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import io.micronaut.context.annotation.Executable
|
||||
import io.micronaut.context.annotation.Prototype
|
||||
import io.micronaut.core.annotation.Introspected
|
||||
import jakarta.inject.Qualifier
|
||||
import java.lang.annotation.Inherited
|
||||
|
||||
@Prototype
|
||||
@Qualifier
|
||||
@@ -15,33 +14,3 @@ import java.lang.annotation.Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@Executable
|
||||
annotation class Day(val value: Int = -1)
|
||||
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class DayInput
|
||||
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Csv
|
||||
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Text
|
||||
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Lines
|
||||
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Groups
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
class Input(private val value: String) {
|
||||
val text get() = value
|
||||
|
||||
val lines get() = Mapper(value.lines())
|
||||
val csv get() = Mapper(value.split(','))
|
||||
|
||||
class Mapper(val value: List<String>) {
|
||||
val ints get() = value.map { it.toInt() }
|
||||
val longs get() = value.map { it.toInt() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,55 +1,26 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import io.micronaut.context.annotation.Prototype
|
||||
import io.micronaut.inject.InjectionPoint
|
||||
import kotlin.io.path.ExperimentalPathApi
|
||||
import kotlin.io.path.readLines
|
||||
import kotlin.io.path.readText
|
||||
|
||||
@Factory
|
||||
@ExperimentalPathApi
|
||||
class InputFactory(private val resourceLoader: ResourceLoader) {
|
||||
|
||||
@Csv
|
||||
fun csvInts(injectionPoint: InjectionPoint<*>): IntArray =
|
||||
injectionPoint.csv().map { it.toInt() }.toIntArray()
|
||||
|
||||
@Csv
|
||||
fun csvLongs(injectionPoint: InjectionPoint<*>): LongArray =
|
||||
injectionPoint.csv().map { it.toLong() }.toLongArray()
|
||||
|
||||
private fun InjectionPoint<*>.csv() = read().split(",")
|
||||
|
||||
@Lines
|
||||
fun linesInts(injectionPoint: InjectionPoint<*>): IntArray =
|
||||
injectionPoint.lines().map { it.toInt() }.toList().toIntArray()
|
||||
|
||||
@Lines
|
||||
fun linesLongs(injectionPoint: InjectionPoint<*>): LongArray =
|
||||
injectionPoint.lines().map { it.toLong() }.toList().toLongArray()
|
||||
|
||||
@Lines
|
||||
fun lines(injectionPoint: InjectionPoint<*>): List<String> =
|
||||
injectionPoint.lines().toList()
|
||||
|
||||
@Text
|
||||
fun text(injectionPoint: InjectionPoint<*>): String =
|
||||
injectionPoint.read()
|
||||
|
||||
@Groups
|
||||
fun groups(injectionPoint: InjectionPoint<*>): List<List<String>> =
|
||||
injectionPoint.read().split("\n\n").map { it.lines() }
|
||||
|
||||
private fun InjectionPoint<*>.path() = resourceLoader.ofDay(getDay(this))
|
||||
private fun InjectionPoint<*>.read() = path().readText().trim()
|
||||
private fun InjectionPoint<*>.lines() = path().readLines()
|
||||
.asSequence()
|
||||
.filter { it.isNotBlank() }
|
||||
@Prototype
|
||||
fun input(injectionPoint: InjectionPoint<*>): Input {
|
||||
val day = getDay(injectionPoint)
|
||||
val path = resourceLoader.ofDay(day)
|
||||
return Input(path.readText().trim())
|
||||
}
|
||||
|
||||
private fun getDay(injectionPoint: InjectionPoint<*>): Int {
|
||||
val dayAnnotation = injectionPoint
|
||||
.declaringBean
|
||||
.findAnnotation<Day>()
|
||||
.findAnnotation(Day::class.java)
|
||||
|
||||
if (dayAnnotation.isEmpty)
|
||||
error("@DayInput cannot only be used on classes annotated with ${Day::class.qualifiedName}")
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
import io.micronaut.core.annotation.AnnotationMetadataProvider
|
||||
import io.micronaut.core.annotation.AnnotationValue
|
||||
import java.util.*
|
||||
|
||||
internal inline fun <reified T : Annotation> AnnotationMetadataProvider.findAnnotation(): Optional<AnnotationValue<T>> =
|
||||
findAnnotation(T::class.java)
|
||||
@@ -1,81 +0,0 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
import be.vandewalleh.aoc.utils.factory.createDay
|
||||
import com.google.common.jimfs.Jimfs
|
||||
import io.micronaut.context.annotation.Replaces
|
||||
import jakarta.inject.Singleton
|
||||
import kotlin.io.path.ExperimentalPathApi
|
||||
import kotlin.io.path.writeText
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class DayTest {
|
||||
|
||||
@Singleton
|
||||
@Replaces(ResourceLoaderImpl::class)
|
||||
@ExperimentalPathApi
|
||||
class FakeResourceLoader : ResourceLoader {
|
||||
private val inMemoryFs = Jimfs.newFileSystem().apply {
|
||||
getPath("1").writeText("blablabla")
|
||||
getPath("2").writeText("1,+2,3,4,-5")
|
||||
getPath("3")
|
||||
.writeText(
|
||||
"""
|
||||
1779
|
||||
1737
|
||||
1537
|
||||
1167
|
||||
1804
|
||||
1873
|
||||
""".trimIndent()
|
||||
)
|
||||
getPath("4")
|
||||
.also { println(it) }
|
||||
.writeText(
|
||||
"""
|
||||
a
|
||||
bb
|
||||
ccc
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
override fun ofDay(day: Int) = inMemoryFs.getPath(day.toString())
|
||||
}
|
||||
|
||||
@Day(1)
|
||||
class TextStringExample(@Text val input: String)
|
||||
|
||||
@Day(2)
|
||||
class CsvIntArrayExample(@Csv val input: IntArray)
|
||||
|
||||
@Day(3)
|
||||
class IntLinesExample(@Lines val input: IntArray)
|
||||
|
||||
@Day(4)
|
||||
class StringLinesExample(@Lines val input: List<String>)
|
||||
|
||||
@Test
|
||||
fun `check @Text String`() {
|
||||
val day = createDay<TextStringExample>()
|
||||
assertThat(day.input).isEqualTo("blablabla")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `check @Csv IntArray`() {
|
||||
val day = createDay<CsvIntArrayExample>()
|
||||
assertThat(day.input).containsExactly(1, +2, 3, 4, -5)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `check @Lines IntArray`() {
|
||||
val day = createDay<IntLinesExample>()
|
||||
assertThat(day.input).containsExactly(1779, 1737, 1537, 1167, 1804, 1873)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `check @Lines strings`() {
|
||||
val day = createDay<StringLinesExample>()
|
||||
assertThat(day.input).containsExactly("a", "bb", "ccc")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user