-
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
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
kt/aoc2023-exe/src/commonTest/kotlin/com/github/ephemient/aoc2023/exe/Day3Bench.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.aoc2023.exe | ||
|
||
import com.github.ephemient.aoc2023.Day3 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day3Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(3) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day3(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day3(input).part2() | ||
} |
32 changes: 32 additions & 0 deletions
32
kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day3.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,32 @@ | ||
package com.github.ephemient.aoc2023 | ||
|
||
class Day3(input: String) { | ||
private val parts: Map<Pair<Int, Int>, List<Int>> = mutableMapOf<Pair<Int, Int>, MutableList<Int>>().apply { | ||
val lines = input.trimEnd().lines() | ||
for ((y, line) in lines.withIndex()) { | ||
for (match in NUMBER.findAll(line)) { | ||
val number = match.value.toInt() | ||
for (y2 in (y - 1).coerceAtLeast(0)..(y + 1).coerceAtMost(lines.lastIndex)) { | ||
val line2 = lines[y2] | ||
val x0 = match.range.first - 1 | ||
val x1 = match.range.last + 1 | ||
for (x in x0.coerceAtLeast(0)..x1.coerceAtMost(line2.lastIndex)) { | ||
if (line2[x].isSymbol()) { | ||
getOrPut(x to y2) { mutableListOf() } += number | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun part1(): Int = parts.values.sumOf { it.sum() } | ||
|
||
fun part2(): Int = parts.values.sumOf { if (it.size == 2) it[0] * it[1] else 0 } | ||
|
||
companion object { | ||
private val NUMBER = """\d+""".toRegex() | ||
|
||
private fun Char.isSymbol(): Boolean = this != '.' && !isWhitespace() && !isDigit() | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
kt/aoc2023-lib/src/commonTest/kotlin/com/github/ephemient/aoc2023/Day3Test.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,32 @@ | ||
package com.github.ephemient.aoc2023 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day3Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(4361, Day3(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(467835, Day3(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|467..114.. | ||
|...*...... | ||
|..35..633. | ||
|......#... | ||
|617*...... | ||
|.....+.58. | ||
|..592..... | ||
|......755. | ||
|...$.*.... | ||
|.664.598.. | ||
|""".trimMargin() | ||
} | ||
} |