-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.fsx
56 lines (48 loc) · 1.13 KB
/
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
54
55
56
let parseInt c =
try
Some(int c)
with :? System.FormatException ->
None
"1.input.txt"
|> System.IO.File.ReadAllLines
|> Array.map (fun l ->
l.ToCharArray()
|> Array.choose (fun c -> c.ToString() |> parseInt)
|> fun a -> a[0] * 10 + a[a.Length - 1])
|> Array.sum // Part 1
let validDigits =
[| "zero"
"one"
"two"
"three"
"four"
"five"
"six"
"seven"
"eight"
"nine" |]
// Edge case warning
// 4nine7oneighthm -> 48
// oneight should be read as 8 and not 1
"inputs/1.txt"
|> System.IO.File.ReadAllLines
|> Array.map (fun l ->
let mutable pos = 0
let mutable v = []
while pos < l.Length do
match parseInt (l[pos].ToString()) with
| Some i ->
v <- i :: v
pos <- pos + 1
| None ->
let mutable newPos = 0
validDigits
|> Array.iteri (fun idx digit ->
if newPos = 0 then
if (l[pos..].StartsWith(digit)) then
v <- idx :: v
newPos <- pos + 1)
if newPos > pos then pos <- newPos else pos <- pos + 1
v <- v |> List.rev
v |> List.toArray |> (fun a -> a[0] * 10 + a[a.Length - 1]))
|> Array.sum // Part2