-
Notifications
You must be signed in to change notification settings - Fork 0
/
day03_part02.fs
36 lines (28 loc) · 1.04 KB
/
day03_part02.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module day03_part02
open AdventOfCode_2024.Modules
open System.Text.RegularExpressions
let solveline (input: string) =
let pattern = @"mul\(\d+,\d+\)|do\(\)|don't\(\)"
let matches = Regex.Matches(input, pattern) |> Seq.cast<Match>
let folder (acc: int * bool) (m': Match) =
let result, shouldAdd = acc
let section = input.Substring(m'.Index, m'.Length)
match section with
| "do()" ->
result, true
| "don't()" ->
result, false
| _ when not shouldAdd ->
result, shouldAdd
| _ ->
let parts = section.Replace("mul(", "").Replace(")", "").Split(",")
let firstValue = int parts.[0]
let secondValue = int parts.[1]
result + (firstValue * secondValue), shouldAdd
let initialState = (0, true)
let finalResult, _ = Seq.fold folder initialState matches
finalResult
let execute() =
let path = "day03/day03_input.txt"
let content = LocalHelper.GetContentFromFile path
solveline content