Skip to content

Commit

Permalink
Day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
CraigSiemens committed Dec 3, 2024
1 parent 79c6efe commit c3c1236
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/AdventOfCode2024/AdventOfCode2024.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public struct AdventOfCode2024: Year {
public let days: [any Day] = [
Day1(),
Day2(),
Day3(),
// {DAYS}
]

Expand Down
50 changes: 50 additions & 0 deletions Sources/AdventOfCode2024/Day3/Day3.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import Algorithms
import Foundation
import Utilities
import RegexBuilder

public struct Day3: Day {
let mulRegex = Regex {
"mul("
Capture {
OneOrMore(.digit)
} transform: { Int($0)! }
","
Capture {
OneOrMore(.digit)
} transform: { Int($0)! }
")"
}

public func part1Solution(for input: Input) -> Int {
var total = 0
for match in input.raw.matches(of: mulRegex) {
total += match.1 * match.2
}
return total
}

public func part2Solution(for input: Input) -> Int {
let regex = Regex {
ChoiceOf {
mulRegex
"don't()"
"do()"
}
}

var enabled = true

var total = 0
for match in input.raw.matches(of: regex) {
if match.0 == "do()" {
enabled = true
} else if match.0 == "don't()" {
enabled = false
} else if enabled {
total += match.1! * match.2!
}
}
return total
}
}
Loading

0 comments on commit c3c1236

Please sign in to comment.