1
0
This commit is contained in:
2021-12-03 12:34:02 +01:00
parent b9a8e7585b
commit 811a6a0af0
11 changed files with 66 additions and 187 deletions
+5 -4
View File
@@ -1,12 +1,13 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.BaseDay
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Lines
@Day
class Day01(@Lines val items: IntArray) {
class Day01 : BaseDay() {
private val items by lazy { input.lines.ints }
fun part1(): Int {
override fun part1(): Int {
var count = 0
for (i in 0 until items.size - 1) {
if (items[i] < items[i + 1]) count++
@@ -14,7 +15,7 @@ class Day01(@Lines val items: IntArray) {
return count
}
fun part2(): Int {
override fun part2(): Int {
var count = 0
for (i in 0 until items.size - 3) {
val a = items.drop(i).take(3).sum()
+17 -17
View File
@@ -1,39 +1,39 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.BaseDay
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Text
@Day
class Day02(@Text input: String) {
private val lines = input.lines().map { it.split(' ').let { it[0] to it[1].toInt() } }
class Day02 : BaseDay() {
private val lines by lazy { input.lines.value.map { it.split(' ').let { it[0] to it[1].toInt() } } }
fun part1(): Any {
override fun part1(): Int {
var horizontalPosition = 0
var depth = 0
lines.forEach { (direction, value) ->
when (direction) {
"forward" -> horizontalPosition += value
"down" -> depth += value
"up" -> depth -= value
}
when (direction) {
"forward" -> horizontalPosition += value
"down" -> depth += value
"up" -> depth -= value
}
}
return horizontalPosition * depth
}
fun part2(): Any {
override fun part2(): Any {
var horizontalPosition = 0
var depth = 0
var aim = 0
lines.forEach { (direction, value) ->
when (direction) {
"forward" -> {
horizontalPosition += value
depth += aim * value
}
"down" -> aim += value
"up" -> aim -= value
when (direction) {
"forward" -> {
horizontalPosition += value
depth += aim * value
}
"down" -> aim += value
"up" -> aim -= value
}
}
return horizontalPosition * depth
}
+6 -6
View File
@@ -23,7 +23,7 @@ abstract class BaseDayTest(day: Int) {
BeanContext.run()
}
val instance: Any by lazy { ctx.value.getBean(ctx.value.findDayDefinition(day)) }
val instance by lazy { ctx.value.getBean(ctx.value.findDayDefinition(day)) }
private val exampleCtx = lazy {
@@ -32,7 +32,7 @@ abstract class BaseDayTest(day: Int) {
.start()
}
val exampleInstance: Any by lazy { exampleCtx.value.getBean(exampleCtx.value.findDayDefinition(day)) }
val exampleInstance by lazy { exampleCtx.value.getBean(exampleCtx.value.findDayDefinition(day)) }
@AfterAll
fun `after all`() {
@@ -44,22 +44,22 @@ abstract class BaseDayTest(day: Int) {
@Test
fun `part1 example result`() {
Assertions.assertThat(ctx.value.run(exampleInstance, "part1")).isEqualTo(part1Example)
Assertions.assertThat(exampleInstance.part1()).isEqualTo(part1Example)
}
@Test
fun `part1 result`() {
Assertions.assertThat(ctx.value.run(instance, "part1")).isEqualTo(part1Answer)
Assertions.assertThat(instance.part1()).isEqualTo(part1Answer)
}
@Test
fun `part2 example result`() {
Assertions.assertThat(ctx.value.run(exampleInstance, "part2")).isEqualTo(part2Example)
Assertions.assertThat(exampleInstance.part2()).isEqualTo(part2Example)
}
@Test
fun `part2 result`() {
Assertions.assertThat(ctx.value.run(instance, "part2")).isEqualTo(part2Answer)
Assertions.assertThat(instance.part2()).isEqualTo(part2Answer)
}
}