Add some magic
This commit is contained in:
parent
322f8eb45a
commit
3e09eee5b7
@ -3,7 +3,7 @@ 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.Lines
|
import be.vandewalleh.aoc.utils.input.Lines
|
||||||
|
|
||||||
@Day(1)
|
@Day
|
||||||
class Day01(@Lines val items: IntArray) {
|
class Day01(@Lines val items: IntArray) {
|
||||||
|
|
||||||
fun part1(): Int {
|
fun part1(): Int {
|
||||||
|
|||||||
@ -1,9 +1,5 @@
|
|||||||
package be.vandewalleh.aoc.days
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
import be.vandewalleh.aoc.utils.factory.createDay
|
import be.vandewalleh.aoc.utils.runDay
|
||||||
|
|
||||||
fun main() {
|
fun main() = runDay()
|
||||||
with(createDay<Day01>()) {
|
|
||||||
println(part2())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
36
utils/src/main/kotlin/be/vandewalleh/aoc/utils/DayRunner.kt
Normal file
36
utils/src/main/kotlin/be/vandewalleh/aoc/utils/DayRunner.kt
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package be.vandewalleh.aoc.utils
|
||||||
|
|
||||||
|
import be.vandewalleh.aoc.utils.factory.findDayDefinition
|
||||||
|
import io.micronaut.context.BeanContext
|
||||||
|
import java.time.LocalDate
|
||||||
|
|
||||||
|
fun runDay(day: Int = LocalDate.now().dayOfMonth) {
|
||||||
|
val ctx = BeanContext.run()
|
||||||
|
val definition = ctx.findDayDefinition(day)
|
||||||
|
|
||||||
|
if (definition == null) {
|
||||||
|
println("Day not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val instance = ctx.getBean(definition)
|
||||||
|
val part1Handle = ctx.findExecutionHandle<Any, Any>(instance, "part1")
|
||||||
|
if (part1Handle.isEmpty) {
|
||||||
|
println("part1() not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
println("*** Day $day ***")
|
||||||
|
println()
|
||||||
|
println("part 1:")
|
||||||
|
println(part1Handle.get().invoke())
|
||||||
|
|
||||||
|
println()
|
||||||
|
val part2Handle = ctx.findExecutionHandle<Any, Any>(instance, "part2")
|
||||||
|
if (part2Handle.isEmpty) {
|
||||||
|
println("part2() not found")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
println("part 2:")
|
||||||
|
println(part2Handle.get().invoke())
|
||||||
|
}
|
||||||
@ -4,10 +4,14 @@ package be.vandewalleh.aoc.utils.factory
|
|||||||
|
|
||||||
import be.vandewalleh.aoc.utils.input.TempFileResourceLoader
|
import be.vandewalleh.aoc.utils.input.TempFileResourceLoader
|
||||||
import io.micronaut.context.BeanContext
|
import io.micronaut.context.BeanContext
|
||||||
|
import io.micronaut.inject.BeanDefinition
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
fun <T> createDay(beanType: Class<T>): T = BeanContext.run().getBean(beanType)
|
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')}" }
|
||||||
|
|
||||||
inline fun <reified T> createDay() = createDay(T::class.java)
|
inline fun <reified T> createDay() = createDay(T::class.java)
|
||||||
|
|
||||||
// A custom resourceLoader that returns a string should be more appropriate but ¯\_(ツ)_/¯
|
// A custom resourceLoader that returns a string should be more appropriate but ¯\_(ツ)_/¯
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
package be.vandewalleh.aoc.utils.input
|
package be.vandewalleh.aoc.utils.input
|
||||||
|
|
||||||
|
import io.micronaut.context.annotation.Executable
|
||||||
import io.micronaut.context.annotation.Prototype
|
import io.micronaut.context.annotation.Prototype
|
||||||
import io.micronaut.core.annotation.Introspected
|
import io.micronaut.core.annotation.Introspected
|
||||||
import jakarta.inject.Qualifier
|
import jakarta.inject.Qualifier
|
||||||
@ -12,7 +13,8 @@ import java.lang.annotation.Inherited
|
|||||||
@Introspected
|
@Introspected
|
||||||
@Target(AnnotationTarget.CLASS)
|
@Target(AnnotationTarget.CLASS)
|
||||||
@Retention(AnnotationRetention.RUNTIME)
|
@Retention(AnnotationRetention.RUNTIME)
|
||||||
annotation class Day(val value: Int)
|
@Executable
|
||||||
|
annotation class Day(val value: Int = -1)
|
||||||
|
|
||||||
@Qualifier
|
@Qualifier
|
||||||
@Prototype
|
@Prototype
|
||||||
|
|||||||
@ -54,7 +54,11 @@ class InputFactory(private val resourceLoader: ResourceLoader) {
|
|||||||
if (dayAnnotation.isEmpty)
|
if (dayAnnotation.isEmpty)
|
||||||
error("@DayInput cannot only be used on classes annotated with ${Day::class.qualifiedName}")
|
error("@DayInput cannot only be used on classes annotated with ${Day::class.qualifiedName}")
|
||||||
|
|
||||||
return dayAnnotation.get().intValue("value").asInt
|
val dayFromAnnotation = dayAnnotation.get().intValue("value").orElse(-1)
|
||||||
|
return if (dayFromAnnotation == -1)
|
||||||
|
injectionPoint.declaringBean.beanType.simpleName.replace("Day", "").toInt()
|
||||||
|
else
|
||||||
|
dayFromAnnotation
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user