-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
79c6efe
commit c3c1236
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.