-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day5Bench.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
import com.github.ephemient.aoc2024.Day5 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day5Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(5) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day5(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day5(input).part2() | ||
} |
40 changes: 40 additions & 0 deletions
40
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day5(input: String) { | ||
private val deps: Set<IntPair> | ||
private val updates: List<List<Int>> | ||
|
||
init { | ||
val (deps, updates) = input.split("\n\n") | ||
this.deps = deps.lines().mapTo(mutableSetOf()) { line -> | ||
val (first, second) = line.split('|', limit = 2) | ||
first.toInt() to second.toInt() | ||
} | ||
this.updates = updates.lines().mapNotNull { line -> | ||
line.ifEmpty { return@mapNotNull null }.split(',').map { it.toInt() } | ||
} | ||
} | ||
|
||
fun part1() = updates.sumOf { pages -> | ||
if ( | ||
pages.withIndex().all { (i, x) -> | ||
pages.subList(i + 1, pages.size).all { y -> y to x !in deps } | ||
} | ||
) pages[pages.size / 2] else 0 | ||
} | ||
|
||
fun part2(): Int = updates.sumOf { update -> | ||
val pages = update.toMutableList() | ||
for (i in pages.indices) { | ||
while (true) { | ||
val x = pages[i] | ||
val j = i + 1 + pages.subList(i + 1, pages.size).indexOfFirst { it to x in deps } | ||
if (j > i) { | ||
pages[i] = pages[j] | ||
pages[j] = x | ||
} else break | ||
} | ||
} | ||
if (update != pages) pages[pages.size / 2] else 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day5Test.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day5Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(143, Day5(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(123, Day5(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|47|53 | ||
|97|13 | ||
|97|61 | ||
|97|47 | ||
|75|29 | ||
|61|13 | ||
|75|53 | ||
|29|13 | ||
|97|29 | ||
|53|29 | ||
|61|53 | ||
|97|53 | ||
|61|29 | ||
|47|13 | ||
|75|47 | ||
|97|75 | ||
|47|61 | ||
|75|61 | ||
|47|29 | ||
|75|13 | ||
|53|13 | ||
| | ||
|75,47,61,53,29 | ||
|97,61,53,29,13 | ||
|75,29,13 | ||
|75,97,47,61,53 | ||
|61,13,29 | ||
|97,13,75,29,47 | ||
|""".trimMargin() | ||
} | ||
} |