Skip to content

Commit

Permalink
Merge pull request #11 from ephemient/kt/day2
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 2, 2024
2 parents 06e30b0 + 26a1900 commit 77061f2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ Development occurs in language-specific directories:
|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg)|[Python](py) ![Python CI](https://github.com/ephemient/aoc2024/workflows/Python%20CI/badge.svg)|[Rust](rs) ![Rust CI](https://github.com/ephemient/aoc2024/workflows/Rust%20CI/badge.svg)|
|--:|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)||||
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|||
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()
}
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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.ephemient.aoc2024

val days: List<Day> = listOf(
Day(1, ::Day1, Day1::part1, Day1::part2),
Day(2, ::Day2, Day2::part1, Day2::part2),
)

data class Day(
Expand Down
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()
}
}

0 comments on commit 77061f2

Please sign in to comment.