Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 6: Guard Gallivant #51

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Development occurs in language-specific directories:
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt)|[day3.py](py/aoc2024/day3.py)|[day3.rs](rs/src/day3.rs)|
|[Day4.hs](hs/src/Day4.hs)|[Day4.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day4.kt)|[day4.py](py/aoc2024/day4.py)|[day4.rs](rs/src/day4.rs)|
|[Day5.hs](hs/src/Day5.hs)|[Day5.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day5.kt)|[day5.py](py/aoc2024/day5.py)|[day5.rs](rs/src/day5.rs)|
|[Day6.hs](hs/src/Day6.hs)||||
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day6.kt)|||
12 changes: 9 additions & 3 deletions kt/aoc2024-exe/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ kotlin {
applyHierarchyTemplate {
withSourceSetTree(KotlinSourceSetTree("bench"))
common {
withJvm()
withWasmJs()
withNative()
group("blocking") {
withJvm()
group("native") {
withNative()
}
}
group("nonblocking") {
withWasmJs()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.ephemient.aoc2024.exe

import com.github.ephemient.aoc2024.Day6
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking

@State(Scope.Benchmark)
class Day6Bench {
private lateinit var input: String

@Setup
fun setup() {
input = getDayInput(6)
}

@Benchmark
fun part1() = Day6(input).part1()

@Benchmark
fun part2() = runBlocking(Dispatchers.Default) {
Day6(input).part2()
}

@Benchmark
fun both(bh: Blackhole) = runBlocking(Dispatchers.Default) {
val day6 = Day6(input)
bh.consume(day6.part1())
bh.consume(day6.part2())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.ephemient.aoc2024.exe

import com.github.ephemient.aoc2024.Day6
import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Scope
import kotlinx.benchmark.Setup
import kotlinx.benchmark.State

@State(Scope.Benchmark)
class Day6Bench {
private lateinit var input: String

@Setup
fun setup() {
input = getDayInput(6)
}

@Benchmark
fun part1() = Day6(input).part1()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.github.ephemient.aoc2024

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.count
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flowOf

class Day6(input: String) {
private val lines = input.lines()
private val initialPosition = lines.withIndex().firstNotNullOf { (y, line) ->
val x = line.indexOf('^')
if (x >= 0) y to x else null
}

fun part1() = lines.walk(initialPosition).mapTo(mutableSetOf()) { it.first }.size

@OptIn(ExperimentalCoroutinesApi::class)
suspend fun part2() = lines.walk(initialPosition)
.mapTo(mutableSetOf()) { it.first }
.apply { remove(initialPosition) }
.asFlow()
.flatMapMerge { (y, x) ->
val lines = lines.toMutableList()
lines[y] = StringBuilder(lines[y]).apply { set(x, '#') }.toString()
flowOf(Unit).filter { !lines.walk(initialPosition).all(mutableSetOf<Any?>()::add) }
}
.count()

companion object {
private fun List<String>.walk(position: IntPair) = sequence {
var (y, x) = position
var dy = -1
var dx = 0
while (true) {
yield(Pair(y to x, dy to dx))
val nextY = y + dy
val nextX = x + dx
when (getOrNull(nextY)?.getOrNull(nextX)) {
null -> break
'#' -> dy = dx.also { dx = -dy }
else -> {
y = nextY
x = nextX
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ val days: List<Day> = listOf(
Day(3, ::Day3, Day3::part1, Day3::part2),
Day(4, ::Day4, Day4::part1, Day4::part2),
Day(5, ::Day5, Day5::part1, Day5::part2),
Day(6, ::Day6, Day6::part1, Day6::part2),
)

data class Day(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.ephemient.aoc2024

import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertEquals

class Day6Test {
@Test
fun part1() {
assertEquals(41, Day6(example).part1())
}

@Test
fun part2() = runTest{
assertEquals(6, Day6(example).part2())
}

companion object {
private val example =
"""
|....#.....
|.........#
|..........
|..#.......
|.......#..
|..........
|.#..^.....
|........#.
|#.........
|......#...
|""".trimMargin()
}
}
Loading