Skip to content

Commit

Permalink
Day 3: Mull It Over
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 3, 2024
1 parent 6a6a4c2 commit b787e79
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Development occurs in language-specific directories:
|--:|--:|--:|--:|
|[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.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)||||
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.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.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()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.ephemient.aoc2024

class Day3(private val input: String) {
fun part1(): Int = regex1.findAll(input).sumOf { match ->
val (x, y) = match.destructured
x.toInt() * y.toInt()
}

fun part2(): Int {
var enable = true
return regex2.findAll(input)
.filter { match ->
val (yes, no) = match.destructured
enable = when {
yes.isNotEmpty() -> true
no.isNotEmpty() -> false
else -> return@filter enable
}
false
}
.sumOf { match ->
val (_, _, x, y) = match.destructured
x.toInt() * y.toInt()
}
}

companion object {
private val regex1 = """mul\((\d+),(\d+)\)""".toRegex()
private val regex2 = """(do\(\))|(don't\(\))|mul\((\d+),(\d+)\)""".toRegex()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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),
Day(3, ::Day3, Day3::part1, Day3::part2),
)

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

import kotlin.test.Test
import kotlin.test.assertEquals

class Day3Test {
@Test
fun part1() {
assertEquals(161, Day3(example1).part1())
}

@Test
fun part2() {
assertEquals(48, Day3(example2).part2())
}

companion object {
private val example1 =
"""
|xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
|""".trimMargin()
private val example2 =
"""
|xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
|""".trimMargin()
}
}

0 comments on commit b787e79

Please sign in to comment.