Compare commits

...

2 Commits

2 changed files with 76 additions and 2 deletions

56
src/aoc2025/Day03.kt Normal file
View File

@ -0,0 +1,56 @@
package aoc2025
import println
import readInput
/*
--- Day 3: Lobby ---
https://adventofcode.com/2025/day/3
*/
fun main() {
val inlineTestInput = """
987654321111111
811111111111119
234234234234278
818181911112111
"""
val powTenTable = generateSequence(1L) { it * 10L }.take(20).toList().toLongArray()
fun rec(i: String, si: Int, left: Int): Long {
for (dig in 9 downTo 1) {
val dp = i.indexOf('0' + dig, si)
if (dp >= 0) {
if (left == 0) {
return dig.toLong()
} else {
val lastDigits = rec(i, dp + 1, left - 1)
if (lastDigits >= 0) return lastDigits + dig * powTenTable[left]
}
}
}
return -1L
}
fun part1(input: List<String>): Long {
return input.sumOf { rec(it, 0, 1) }
}
fun part2(input: List<String>): Long {
return input.sumOf { rec(it, 0, 11) }
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
//val testInput = readInput("aoc2025/Day03_test")
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 357L)
check(testInputPart2Result == 3121910778619L)
val input = readInput("aoc2025/Day03")
part1(input).println()
part2(input).println()
}

View File

@ -4,15 +4,19 @@ import com.mohamedrejeb.ksoup.html.parser.KsoupHtmlHandler
import com.mohamedrejeb.ksoup.html.parser.KsoupHtmlParser
import fuel.Fuel
import fuel.method
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.io.FileNotFoundException
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Paths
import java.time.LocalDate
import java.time.LocalTime
import java.time.Month
import java.time.ZoneId
import java.util.*
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
fun main() {
var cookie = "<insert your session cookie here or store in gradle.properties>"
@ -42,7 +46,7 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri
if (!Files.exists(targetDir)) {
Files.createDirectories(targetDir)
}
val now = LocalDate.now(ZoneId.of("UTC-1"))
val now = LocalDate.now(ZoneId.of("UTC+1"))
val maxPuzzles = if (year < 2025) 25 else 12
val lastDay = if (now.isBefore(LocalDate.of(year, Month.DECEMBER, maxPuzzles))) {
if (now.isAfter(LocalDate.of(year, Month.NOVEMBER, 30))) {
@ -60,6 +64,20 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri
val descriptionFile = targetDir.resolve(DESC_FILENAME.format(day))
val genClassFile = targetDir.resolve(GENCLASS_FILENAME.format(day))
if (!Files.exists(inputFile)) {
if (day == lastDay && LocalTime.now(ZoneId.of("UTC+1")).hour < 5) {
println("Puzzle will not be available within the next hour. Skipping.")
break
}
while (day == lastDay && LocalTime.now(ZoneId.of("UTC+1")).hour == 5) {
println("Waiting until puzzle is available...")
runBlocking {
if (LocalTime.now(ZoneId.of("UTC+1")).minute < 59) {
delay(1.minutes)
} else {
delay(1.seconds)
}
}
}
println("Attempting to download input for day $day")
val (code, data) = downloadInput(day)
if (code != 200) {
@ -157,7 +175,7 @@ class Downloader(val year: Int, val packageName: String, val sessionCookie: Stri
url = "https://adventofcode.com/$year/day/$day$suffix",
method = "GET",
headers = mapOf(
"User-Agent" to "git.platon42.de/chrisly42/advent-of-code",
"User-Agent" to "git.platon42.de/chrisly42/advent-of-code (chrisly@platon42.de)",
"Cookie" to "session=$sessionCookie"
)
)