-
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.
Merge pull request #11 from ephemient/kt/day2
- Loading branch information
Showing
5 changed files
with
92 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/Day2Bench.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.Day2 | ||
import kotlinx.benchmark.Benchmark | ||
import kotlinx.benchmark.Scope | ||
import kotlinx.benchmark.Setup | ||
import kotlinx.benchmark.State | ||
|
||
@State(Scope.Benchmark) | ||
class Day2Bench { | ||
private lateinit var input: String | ||
|
||
@Setup | ||
fun setup() { | ||
input = getDayInput(1) | ||
} | ||
|
||
@Benchmark | ||
fun part1() = Day2(input).part1() | ||
|
||
@Benchmark | ||
fun part2() = Day2(input).part2() | ||
} |
39 changes: 39 additions & 0 deletions
39
kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.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,39 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
class Day2(input: String) { | ||
private val reports = input.lineSequence().mapNotNull { line -> | ||
val words = line.split(splitter) | ||
IntArray(words.size) { words[it].toIntOrNull() ?: return@mapNotNull null } | ||
}.toList() | ||
|
||
fun part1() = reports.count(::isSafe1) | ||
|
||
fun part2() = reports.count(::isSafe2) | ||
|
||
companion object { | ||
private val splitter = """\s+""".toRegex() | ||
|
||
private fun isSafe1(report: IntArray): Boolean { | ||
var decreasing = false | ||
var increasing = false | ||
for (i in 0..report.size - 2) { | ||
when (report[i + 1] - report[i]) { | ||
in -3..-1 -> decreasing = true | ||
in 1..3 -> increasing = true | ||
else -> return false | ||
} | ||
} | ||
return !(decreasing && increasing) | ||
} | ||
|
||
private fun isSafe2(report: IntArray): Boolean { | ||
if (report.isEmpty()) return true | ||
val report2 = report.copyOf(report.size - 1) | ||
for (i in report2.lastIndex downTo 0) { | ||
if (isSafe1(report2)) return true | ||
report2[i] = report[i + 1] | ||
} | ||
return isSafe1(report2) | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
kt/aoc2024-lib/src/commonTest/kotlin/com/github/ephemient/aoc2024/Day2Test.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,28 @@ | ||
package com.github.ephemient.aoc2024 | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class Day2Test { | ||
@Test | ||
fun part1() { | ||
assertEquals(2, Day2(example).part1()) | ||
} | ||
|
||
@Test | ||
fun part2() { | ||
assertEquals(4, Day2(example).part2()) | ||
} | ||
|
||
companion object { | ||
private val example = | ||
""" | ||
|7 6 4 2 1 | ||
|1 2 7 8 9 | ||
|9 7 6 2 1 | ||
|1 3 2 4 5 | ||
|8 6 4 4 1 | ||
|1 3 6 7 9 | ||
|""".trimMargin() | ||
} | ||
} |