From d26212b747a4c84b5f34f19aafa0ba7430dc52ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20R=C3=B8dland?= Date: Tue, 3 Dec 2024 09:20:17 +0100 Subject: [PATCH] 2024 Day 03 Part 1 --- .../kotlin/no/rodland/advent_2024/Day03.kt | 30 +++++++ src/main/script/download_aoc_input.sh | 5 +- .../no/rodland/advent_2024/Day03Test.kt | 82 +++++++++++++++++++ src/test/resources/2024/input_03_test.txt | 1 + 4 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/no/rodland/advent_2024/Day03.kt create mode 100644 src/test/kotlin/no/rodland/advent_2024/Day03Test.kt diff --git a/src/main/kotlin/no/rodland/advent_2024/Day03.kt b/src/main/kotlin/no/rodland/advent_2024/Day03.kt new file mode 100644 index 00000000..ce74c399 --- /dev/null +++ b/src/main/kotlin/no/rodland/advent_2024/Day03.kt @@ -0,0 +1,30 @@ +package no.rodland.advent_2024 + +import no.rodland.advent.Day + +// template generated: 03/12/2024 +// Fredrik Rødland 2024 + +class Day03(val input: List) : Day>> { + private val multRegEx = "mul\\((\\d{1,3}),(\\d{1,3})\\)".toRegex() + + private val parsed = input.parse() + + override fun partOne(): Long { + return parsed.sumOf { (a, b) -> a * b }.toLong() + } + + override fun partTwo(): Long { + return 2 + } + + override fun List.parse(): List> { + return flatMap { line -> + multRegEx.findAll(line).toList().map { mr -> + mr.groupValues[1].toInt() to mr.groupValues[2].toInt() + } + } + } + + override val day = "03".toInt() +} diff --git a/src/main/script/download_aoc_input.sh b/src/main/script/download_aoc_input.sh index b2ee91ee..1a0d84a8 100755 --- a/src/main/script/download_aoc_input.sh +++ b/src/main/script/download_aoc_input.sh @@ -22,4 +22,7 @@ git add "src/test/resources/${YEAR}/input_${DAY}_test.txt" git commit -a -m "${YEAR} - Day ${DAY} - input/init" cd - || exit -tail -10 "$PUZZLE_FILE" \ No newline at end of file +tail -10 "$PUZZLE_FILE" +echo +echo "TEST-FILE: $PUZZLE_FILE_TEST" +echo "FILE: $PUZZLE_FILE" diff --git a/src/test/kotlin/no/rodland/advent_2024/Day03Test.kt b/src/test/kotlin/no/rodland/advent_2024/Day03Test.kt new file mode 100644 index 00000000..3a704b11 --- /dev/null +++ b/src/test/kotlin/no/rodland/advent_2024/Day03Test.kt @@ -0,0 +1,82 @@ +package no.rodland.advent_2024 + +import no.rodland.advent.* +import org.junit.jupiter.api.Nested +import org.junit.jupiter.api.Test +import readFile + +// +// run: download_aoc_input.sh to download input +// + +@Suppress("ClassName") +@DisableSlow +internal class Day03Test { + private val data03 = "2024/input_03.txt".readFile() + private val test03 = "2024/input_03_test.txt".readFile() + + private val resultTestOne = 161L + private val resultTestTwo = 2L + private val resultOne = 187825547L + private val resultTwo = 2L + + val test = defaultTestSuiteParseOnInit( + Day03(data03), + Day03(test03), + resultTestOne, + resultOne, + resultTestTwo, + resultTwo, + { Day03(data03) }, + { Day03(test03) }, + ) + + @Nested + inner class Init { + @Test + fun `03,-,example,1`() { + report(AOCTest({ "123".toInt() }, Unit, 123, 5, "03".toInt(), Part.TWO, false, "example")) + } + + @Test + fun `03,-,example,2`() { + report(test.initTest.copy()) + } + + @Test + fun `03,-,test,init`() { + report(test.initTest) + } + + @Test + fun `03,-,live,init`() { + report(test.initLive) + } + } + + @Nested + inner class `Part 1` { + @Test + fun `03,1,test`() { + report(test.testPart1) + } + + @Test + fun `03,1,live,1`() { + report(test.livePart1) + } + } + + @Nested + inner class `Part 2` { + @Test + fun `03,2,test`() { + report(test.testPart2) + } + + @Test + fun `03,2,live,1`() { + report(test.livePart2) + } + } +} diff --git a/src/test/resources/2024/input_03_test.txt b/src/test/resources/2024/input_03_test.txt index e69de29b..2e1a90a5 100644 --- a/src/test/resources/2024/input_03_test.txt +++ b/src/test/resources/2024/input_03_test.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) \ No newline at end of file