Last active
December 1, 2023 12:31
-
-
Save Szer/e6a5c1c8abeb18c9e1be0f043b7d3ad9 to your computer and use it in GitHub Desktop.
Advent of Code 2023 1-2
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
let realFirstDigitRegex = Regex("one|two|three|four|five|six|seven|eight|nine|[0-9]") | |
let realLastDigitRegex = Regex("one|two|three|four|five|six|seven|eight|nine|[0-9]", RegexOptions.RightToLeft) | |
let matchToInt = function | |
| "one" -> 1 | |
| "two" -> 2 | |
| "three" -> 3 | |
| "four" -> 4 | |
| "five" -> 5 | |
| "six" -> 6 | |
| "seven" -> 7 | |
| "eight" -> 8 | |
| "nine" -> 9 | |
| x -> int x | |
let realSumOfCalibrationValues = | |
Seq.fold (fun acc (line: string) -> | |
let first = realFirstDigitRegex.Match(line).Value |> matchToInt | |
let last = realLastDigitRegex.Match(line).Value |> matchToInt | |
acc + 10*first + last | |
) 0 | |
["three1abc2" | |
"pqr3stu8vfourwx" | |
"a1b2c3d4e5f" | |
"treb7uche0t" ] | |
|> realSumOfCalibrationValues | |
|> System.Console.WriteLine // 151 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment