From 22d7ba6a6cd0a3485178c6b09a2ab3518bc45a93 Mon Sep 17 00:00:00 2001 From: Daniel Lin Date: Tue, 3 Dec 2024 08:00:45 -0500 Subject: [PATCH] Reuse part 1 by literal string splitting --- .../com/github/ephemient/aoc2024/Day3.kt | 31 +++++-------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt index 9790ea1..391e5b5 100644 --- a/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt +++ b/kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt @@ -1,31 +1,16 @@ 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 part1(): Int = part1(input) - 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() - } - } + fun part2(): Int = input.splitToSequence("do()").sumOf { part1(it.substringBefore("don't()")) } companion object { - private val regex1 = """mul\((\d+),(\d+)\)""".toRegex() - private val regex2 = """(do\(\))|(don't\(\))|mul\((\d+),(\d+)\)""".toRegex() + private val regex = """mul\((\d+),(\d+)\)""".toRegex() + + private fun part1(input: String): Int = regex.findAll(input).sumOf { match -> + val (x, y) = match.destructured + x.toInt() * y.toInt() + } } }