From 90ec9a469625f802501b6054941a8799ac6d2370 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Thu, 5 Dec 2024 13:58:07 -0500 Subject: [PATCH] Improve total time by splitting input data for parts 1 and 2 --- .../github/ephemient/aoc2024/exe/Day5Bench.kt | 8 +++++ .../com/github/ephemient/aoc2024/Day5.kt | 30 +++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt index 3216d3b3..6a94f929 100644 --- a/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt +++ b/kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt @@ -2,6 +2,7 @@ package com.github.ephemient.aoc2024.exe import com.github.ephemient.aoc2024.Day5 import kotlinx.benchmark.Benchmark +import kotlinx.benchmark.Blackhole import kotlinx.benchmark.Scope import kotlinx.benchmark.Setup import kotlinx.benchmark.State @@ -20,4 +21,11 @@ class Day5Bench { @Benchmark fun part2() = Day5(input).part2() + + @Benchmark + fun both(bh: Blackhole) { + val day5 = Day5(input) + bh.consume(day5.part1()) + bh.consume(day5.part2()) + } } diff --git a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt index ef8e6340..61ab1ca9 100644 --- a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt +++ b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt @@ -2,7 +2,8 @@ package com.github.ephemient.aoc2024 class Day5(input: String) { private val rdeps: Map> - private val updates: List> + private val correct: List> + private val incorrect: List> init { val (deps, updates) = input.split("\n\n") @@ -12,31 +13,30 @@ class Day5(input: String) { val value = element.substringBefore('|').toInt() accumulator?.apply { add(value) } ?: mutableSetOf(value) } - this.updates = updates.lines().mapNotNull { line -> + val (correct, incorrect) = updates.lines().mapNotNull { line -> line.ifEmpty { return@mapNotNull null }.split(',').map { it.toInt() } - } - } - - fun part1() = updates.sumOf { pages -> - if ( + }.partition { pages -> pages.withIndex().all { (i, x) -> rdeps[x]?.let { pages.subList(i + 1, pages.size).any(it::contains) } != true } - ) pages[pages.size / 2] else 0 + } + this.correct = correct + this.incorrect = incorrect } - fun part2(): Int = updates.sumOf { update -> - val pages = update.toMutableList() + fun part1() = correct.sumOf { it[it.size / 2] } + + fun part2(): Int = incorrect.sumOf { + val pages = it.toMutableList() for (i in pages.indices) { while (true) { val x = pages[i] val j = pages.subList(i + 1, pages.size).indexOfFirst(rdeps[x].orEmpty()::contains) - if (j >= 0) { - pages[i] = pages[i + 1 + j] - pages[i + 1 + j] = x - } else break + if (j < 0) break + pages[i] = pages[i + 1 + j] + pages[i + 1 + j] = x } } - if (update != pages) pages[pages.size / 2] else 0 + pages[pages.size / 2] } }