1
0

Day01 2021

This commit is contained in:
Hubert Van De Walle 2021-12-01 12:42:10 +01:00
parent ae327a4928
commit ccfcf5a259
7 changed files with 2062 additions and 6 deletions

18
2021/build.gradle.kts Normal file
View File

@ -0,0 +1,18 @@
plugins {
id("kotlin-convention")
kotlin("kapt")
}
dependencies {
implementation(project(":utils"))
kapt(Libs.Micronaut.processor)
implementation(Libs.Slf4J.api)
runtimeOnly(Libs.Slf4J.simple)
implementation(Libs.eclipseCollections)
testImplementation(Libs.Jmh.core)
kaptTest(Libs.Jmh.processor)
}

View File

@ -0,0 +1,28 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.input.Day
import be.vandewalleh.aoc.utils.input.Input
import be.vandewalleh.aoc.utils.input.Lines
@Day(1)
class Day01(@Lines input: Input<IntArray>) {
private val items = input.value
fun part1(): Int {
var count = 0
for (i in 0 until items.size - 1) {
if (items[i] < items[i + 1]) count++
}
return count
}
fun part2(): Int {
var count = 0
for (i in 0 until items.size - 3) {
val a = items.drop(i).take(3).sum()
val b = items.drop(i + 1).take(3).sum()
if (b > a) count++
}
return count
}
}

View File

@ -0,0 +1,7 @@
package be.vandewalleh.aoc.days
import be.vandewalleh.aoc.utils.factory.createDay
fun main() = with(createDay<Day01>()) {
println(part2())
}

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,5 @@ plugins {
}
adventOfCode {
year = 2020
session = file(".session").readText().trim()
}

View File

@ -10,7 +10,6 @@ import org.gradle.kotlin.dsl.getByType
open class AdventOfCodeExtension {
var session: String? = null
var year: Int = 2020
}
class AdventOfCodeDownloaderPlugin : Plugin<Project> {
@ -26,8 +25,12 @@ class AdventOfCodeDownloaderPlugin : Plugin<Project> {
throw GradleException("advent of code session not set")
}
val now = LocalDateTime.now()
val year = now.year
val currentDay = now.dayOfMonth.toString()
val resourceDir: File = project
.project(":days")
.project(":$year")
.extensions
.getByType<SourceSetContainer>()
.getByName("main")
@ -35,9 +38,9 @@ class AdventOfCodeDownloaderPlugin : Plugin<Project> {
.srcDirs
.first()
val currentDay = LocalDateTime.now().dayOfMonth.toString()
val outFile = File(resourceDir, "day" + currentDay.padStart(length = 2, padChar = '0') + ".txt")
val url = "https://adventofcode.com/${extension.year}/day/$currentDay/input"
val url = "https://adventofcode.com/$year/day/$currentDay/input"
Unirest.get(url)
.cookie("session", extension.session)

View File

@ -1,4 +1,5 @@
rootProject.name = "Advent of Code"
include("utils")
include("2020")
include("2019")
include("2020")
include("2021")