Add more types
This commit is contained in:
parent
7e31325d16
commit
dc4160ca73
@ -1,12 +1,9 @@
|
|||||||
@file:Suppress("unused")
|
|
||||||
|
|
||||||
package be.vandewalleh.aoc.days
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
import be.vandewalleh.aoc.utils.input.Day
|
import be.vandewalleh.aoc.utils.input.Day
|
||||||
import be.vandewalleh.aoc.utils.input.Input
|
import be.vandewalleh.aoc.utils.input.Input
|
||||||
import be.vandewalleh.aoc.utils.input.Lines
|
import be.vandewalleh.aoc.utils.input.Lines
|
||||||
import io.micronaut.context.BeanContext
|
import be.vandewalleh.aoc.utils.input.createDay
|
||||||
import io.micronaut.context.getBean
|
|
||||||
|
|
||||||
@Day(1)
|
@Day(1)
|
||||||
class Day01(@Lines input: Input<IntArray>) {
|
class Day01(@Lines input: Input<IntArray>) {
|
||||||
@ -34,8 +31,7 @@ class Day01(@Lines input: Input<IntArray>) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main() {
|
fun main() = with(createDay<Day01>()) {
|
||||||
val day = BeanContext.run().getBean<Day01>()
|
println(part1())
|
||||||
println(day.part1())
|
println(part2())
|
||||||
println(day.part2())
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,23 +17,39 @@ import kotlin.io.path.readText
|
|||||||
class InputFactory(private val resourceLoader: ResourceLoader) {
|
class InputFactory(private val resourceLoader: ResourceLoader) {
|
||||||
|
|
||||||
@Csv
|
@Csv
|
||||||
fun csvIntArray(injectionPoint: InjectionPoint<*>): Input<IntArray> = injectionPoint
|
fun csv(injectionPoint: InjectionPoint<*>): Input<*> =
|
||||||
|
when (val param = injectionPoint.typeNameOfAnnotation<Csv>()) {
|
||||||
|
INT_ARRAY -> injectionPoint
|
||||||
.read()
|
.read()
|
||||||
.split(",")
|
.split(",")
|
||||||
.map { it.toInt() }
|
.map { it.toInt() }
|
||||||
.toIntArray()
|
.toIntArray()
|
||||||
.wrap()
|
.wrap()
|
||||||
|
LONG_ARRAY -> injectionPoint
|
||||||
|
.read()
|
||||||
|
.split(",")
|
||||||
|
.map { it.toLong() }
|
||||||
|
.toLongArray()
|
||||||
|
.wrap()
|
||||||
|
else -> error("Unsupported type $param")
|
||||||
|
}
|
||||||
|
|
||||||
@Lines
|
@Lines
|
||||||
fun linesIntArray(injectionPoint: InjectionPoint<*>): Input<*> =
|
fun lines(injectionPoint: InjectionPoint<*>): Input<*> =
|
||||||
when (val param = injectionPoint.typeNameOfAnnotation<Lines>()) {
|
when (val param = injectionPoint.typeNameOfAnnotation<Lines>()) {
|
||||||
"int[]" -> injectionPoint
|
INT_ARRAY -> injectionPoint
|
||||||
.lines()
|
.lines()
|
||||||
.map { it.toInt() }
|
.map { it.toInt() }
|
||||||
.toList()
|
.toList()
|
||||||
.toIntArray()
|
.toIntArray()
|
||||||
.wrap()
|
.wrap()
|
||||||
"java.util.List<java.lang.String>" -> injectionPoint
|
LONG_ARRAY -> injectionPoint
|
||||||
|
.lines()
|
||||||
|
.map { it.toLong() }
|
||||||
|
.toList()
|
||||||
|
.toLongArray()
|
||||||
|
.wrap()
|
||||||
|
STRING_LIST -> injectionPoint
|
||||||
.lines()
|
.lines()
|
||||||
.toList()
|
.toList()
|
||||||
.wrap()
|
.wrap()
|
||||||
@ -41,7 +57,7 @@ class InputFactory(private val resourceLoader: ResourceLoader) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Text
|
@Text
|
||||||
fun string(injectionPoint: InjectionPoint<*>): Input<String> =
|
fun text(injectionPoint: InjectionPoint<*>): Input<String> =
|
||||||
injectionPoint.read().wrap()
|
injectionPoint.read().wrap()
|
||||||
|
|
||||||
|
|
||||||
@ -73,4 +89,10 @@ class InputFactory(private val resourceLoader: ResourceLoader) {
|
|||||||
return dayAnnotation.get().intValue("day").asInt
|
return dayAnnotation.get().intValue("day").asInt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val INT_ARRAY = "int[]"
|
||||||
|
private const val LONG_ARRAY = "long[]"
|
||||||
|
private const val STRING_LIST = "java.util.List<java.lang.String>"
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package be.vandewalleh.aoc.utils.input
|
package be.vandewalleh.aoc.utils.input
|
||||||
|
|
||||||
|
import io.micronaut.context.BeanContext
|
||||||
|
import io.micronaut.context.getBean
|
||||||
import io.micronaut.core.annotation.AnnotationMetadataDelegate
|
import io.micronaut.core.annotation.AnnotationMetadataDelegate
|
||||||
import io.micronaut.core.annotation.AnnotationMetadataProvider
|
import io.micronaut.core.annotation.AnnotationMetadataProvider
|
||||||
import io.micronaut.core.annotation.AnnotationValue
|
import io.micronaut.core.annotation.AnnotationValue
|
||||||
@ -17,3 +19,5 @@ internal inline fun <reified T : Annotation> AnnotationMetadataProvider.findAnno
|
|||||||
|
|
||||||
internal inline fun <reified T : Annotation> AnnotationMetadataProvider.hasAnnotation(): Boolean =
|
internal inline fun <reified T : Annotation> AnnotationMetadataProvider.hasAnnotation(): Boolean =
|
||||||
findAnnotation(T::class.java).isPresent
|
findAnnotation(T::class.java).isPresent
|
||||||
|
|
||||||
|
inline fun <reified T> createDay() = BeanContext.run().getBean<T>()
|
||||||
|
|||||||
@ -1,9 +1,7 @@
|
|||||||
package be.vandewalleh.aoc.utils.input
|
package be.vandewalleh.aoc.utils.input
|
||||||
|
|
||||||
import com.google.common.jimfs.Jimfs
|
import com.google.common.jimfs.Jimfs
|
||||||
import io.micronaut.context.BeanContext
|
|
||||||
import io.micronaut.context.annotation.Replaces
|
import io.micronaut.context.annotation.Replaces
|
||||||
import io.micronaut.context.getBean
|
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
@ -55,8 +53,6 @@ class DayTest {
|
|||||||
@Day(4)
|
@Day(4)
|
||||||
class StringLinesExample(@Lines val input: Input<List<String>>)
|
class StringLinesExample(@Lines val input: Input<List<String>>)
|
||||||
|
|
||||||
private inline fun <reified T> createDay() = BeanContext.run().getBean<T>()
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `check @Text String`() {
|
fun `check @Text String`() {
|
||||||
val day = createDay<TextStringExample>()
|
val day = createDay<TextStringExample>()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user