Day 3, meh for having multiple lines of input and meh for having two different inputs and meh for being lazy.

This commit is contained in:
Chris Hodges 2024-12-03 06:23:12 +01:00
parent 79797d8ea2
commit 7695a005c9

52
src/aoc2024/Day03.kt Normal file
View File

@ -0,0 +1,52 @@
package aoc2024
import println
import readInput
/*
--- Day 3: Mull It Over ---
"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look.
The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?"
The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up!
It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a result of 2024. Similarly, mul(123,4) would multiply 123 by 4.
However, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or mul ( 2 , 4 ) do nothing.
For example, consider the following section of corrupted memory:
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 (2*4 + 5*5 + 11*8 + 8*5).
Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications?
*/
fun main() {
val inlineTestInput = """
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
"""
val inlineTestInput2 = """
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)
"""
fun part1(input: List<String>): Int {
return "mul\\(([0-9]+),([0-9]+)\\)".toRegex().findAll(input.joinToString("")).map { it.groupValues[1].toInt() * it.groupValues[2].toInt() }.sum()
}
fun part2(input: List<String>): Int {
val unp = input.joinToString("")
val proc = unp.replace("(don't\\(\\).*?)+do\\(\\)".toRegex(), "")
return "mul\\(([0-9]+),([0-9]+)\\)".toRegex().findAll(proc).map { it.groupValues[1].toInt() * it.groupValues[2].toInt() }.sum()
}
// test if implementation meets criteria from the description, like:
val testInput = inlineTestInput.trim().reader().readLines()
val testInput2 = inlineTestInput2.trim().reader().readLines()
//val testInput = readInput("aoc2024/Day03_test")
val testInputPart1Result = part1(testInput)
println("Part 1 Test: $testInputPart1Result")
val testInputPart2Result = part2(testInput2)
println("Part 2 Test: $testInputPart2Result")
check(testInputPart1Result == 161)
check(testInputPart2Result == 48)
val input = readInput("aoc2024/Day03")
part1(input).println()
part2(input).println()
}