-
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
112 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
31 changes: 31 additions & 0 deletions
31
kt/aoc2024-exe/src/commonBench/kotlin/com/github/ephemient/aoc2024/exe/Day9Bench.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,31 @@ | ||
package com.github.ephemient.aoc2024.exe | ||
|
||
import com.github.ephemient.aoc2024.Day9 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Blackhole | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day9Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(9) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day9(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day9(input).part2() | ||
|
||
@Benchmark | ||
fun solve(bh: Blackhole) { | ||
val day9 = Day9(input) | ||
bh.consume(day9.part1()) | ||
bh.consume(day9.part2()) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day9.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,56 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day9(input: String) { | ||
private val input = input.mapNotNull { it.digitToIntOrNull() } | ||
|
||
fun part1(): Long { | ||
val chunks = input.toIntArray() | ||
var total = 0L | ||
var i = 0 | ||
var j = chunks.lastIndex | ||
var offset = 0 | ||
while (i <= j) { | ||
if (i % 2 == 0) { | ||
val size = chunks[i] | ||
total += i / 2L * triRange(offset, size) | ||
offset += size | ||
i++ | ||
} else if (j % 2 == 0) { | ||
val free = chunks[i] | ||
val size = chunks[j] | ||
total += j / 2L * triRange(offset, minOf(free, size)) | ||
offset += minOf(free, size) | ||
if (free <= size) i++ else chunks[i] = free - size | ||
if (free >= size) j-- else chunks[j] = size - free | ||
} else { | ||
j-- | ||
} | ||
} | ||
return total | ||
} | ||
|
||
fun part2(): Long { | ||
val files = IntArray((input.size + 1) / 2) { input[2 * it] } | ||
val frees = IntArray(input.size / 2) { input[2 * it + 1] } | ||
val offsets = IntArray(input.size) | ||
for (i in 0..offsets.size - 2) offsets[i + 1] = offsets[i] + input[i] | ||
var total = 0L | ||
outer@for (i in files.lastIndex downTo 0) { | ||
val size = files[i] | ||
for (j in 0 until i) { | ||
if (frees[j] >= size) { | ||
total += i.toLong() * triRange(offsets[2 * j + 1], size) | ||
frees[j] -= size | ||
offsets[2 * j + 1] += size | ||
continue@outer | ||
} | ||
} | ||
total += i.toLong() * triRange(offsets[2 * i], size) | ||
} | ||
return total | ||
} | ||
|
||
companion object { | ||
private fun triRange(offset: Int, size: Int) = (2 * offset + size - 1) * size / 2 | ||
} | ||
} |
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-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day9Test.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 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day9Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(1928L, Day9(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(2858L, Day9(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|2333133121414131402 | ||
|""".trimMargin() | ||
} | ||
} |