-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.fs
42 lines (36 loc) · 1.07 KB
/
Day01.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
37
38
39
40
41
42
module Day01
open System
open System.Text.RegularExpressions
let wordToDigit rightToLeft (txt: string) =
let pattern = "one|two|three|four|five|six|seven|eight|nine"
let eval (m: Match) =
match m.Value with
| "one" -> "1"
| "two" -> "2"
| "three" -> "3"
| "four" -> "4"
| "five" -> "5"
| "six" -> "6"
| "seven" -> "7"
| "eight" -> "8"
| "nine" -> "9"
| _ -> failwith "oops"
let options =
if rightToLeft then RegexOptions.RightToLeft else RegexOptions.None
Regex(pattern, options).Replace(txt, eval, 1)
let calVal replaceFirst replaceLast text =
[
text |> replaceFirst |> Seq.find(Char.IsNumber)
text |> replaceLast |> Seq.findBack(Char.IsNumber)
]
|> String.Concat
|> int
let sumCalVals firstReplace lastReplace =
List.map (calVal firstReplace lastReplace)
>> List.sum
let part1 getLines =
getLines "input"
|> sumCalVals id id
let part2 getLines =
getLines "input"
|> sumCalVals (wordToDigit false) (wordToDigit true)