-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_1.fsx
54 lines (41 loc) · 1.27 KB
/
day_1.fsx
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
43
44
45
46
47
48
49
50
51
52
53
#load "common.fsx"
open System.Text.RegularExpressions
let readInput () = Input.readAllLines "input_1.txt"
let simpleDigitRegex = @"(\d)"
let complexDigitRegex = @"(\d|one|two|three|four|five|six|seven|eight|nine)"
let getDigitValue (digit: string) =
match digit.ToLower() with
| "one" -> 1
| "two" -> 2
| "three" -> 3
| "four" -> 4
| "five" -> 5
| "six" -> 6
| "seven" -> 7
| "eight" -> 8
| "nine" -> 9
| _ -> digit |> int
let findDigit (line: string) regex =
let digit = Regex.Match(line, regex).Value
getDigitValue digit
let findLastDigit (line: string) regex =
let lastDigit = Regex.Match(line, regex, RegexOptions.RightToLeft).Value
getDigitValue lastDigit
let getFirstAndLastDigits regex (line: string) : (int * int) =
let firstDigit = findDigit line regex
let lastDigit = findLastDigit line regex
(firstDigit, lastDigit)
// Part 1
let part1 =
readInput()
|> Seq.map (getFirstAndLastDigits simpleDigitRegex)
|> Seq.map (fun (first, last) -> first * 10 + last)
|> Seq.sum
printfn "Part1: %d" part1
// Part 2
let part2 =
readInput()
|> Seq.map (getFirstAndLastDigits complexDigitRegex)
|> Seq.map (fun (first, last) -> first * 10 + last)
|> Seq.sum
printfn "Part2: %d" part2