Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day 3: Mull It Over #3

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Advent of Code is an Advent calendar of small programming puzzles by [Eric Wastl

- Day 1: [Historian Hysteria](https://adventofcode.com/2024/day/1) -- [Day1.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day1.kt)
- Day 2: [Red-Nosed Reports](https://adventofcode.com/2024/day/2) -- [Day2.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day2.kt)
- Day 3: [Mull It Over](https://adventofcode.com/2024/day/3) -- [Day3.kt](https://github.com/andilau/advent-of-code-2024/blob/main/src/main/kotlin/days/Day3.kt)

### Features

Expand Down
36 changes: 36 additions & 0 deletions src/main/kotlin/days/Day3.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package days

@AdventOfCodePuzzle(
name = "Mull It Over",
url = "https://adventofcode.com/2024/day/3",
date = Date(day = 3, year = 2024)
)
class Day3(val memory: List<String>) : Puzzle {

val regex = """mul\((\d{1,3}+),(\d{1,3}+)\)""".toRegex()
val regex2 = """mul\((\d{1,3}+),(\d{1,3}+)\)|(do\(\))|(don't\(\))""".toRegex()

override fun partOne(): Int = regex
.findAll(memory.joinToString())
.map { it.groupValues.drop(1).map { it.toInt() }.fold(1) { a, b -> a * b } }
.sum()

override fun partTwo(): Int = regex2
.findAll(memory.joinToString())
.map {
val what = it.groupValues.first()
when (what) {
"do()" -> 1 to 0
"don't()" -> 0 to 0
else -> 1 to it.groupValues.drop(1).take(2).map { it.toInt() }.fold(1) { a, b -> a * b }
}
}
.fold(1 to 0) { acc, p ->
Pair(
if (p.second == 0) p.first else acc.first,
acc.second + acc.first * p.second
)
}
.second

}
Loading
Loading