-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04_part01.fs
30 lines (21 loc) · 1.03 KB
/
day04_part01.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
module day04_part01
open System.IO
let filepath = __SOURCE_DIRECTORY__ + @"../../day04_input.txt"
let input = File.ReadAllText(filepath).Split('-') |> Array.map int
let isSixDigitNumber(pass: int) : bool =
pass.ToString().Length = 6
let isInRange(range:int array, pass: int) : bool =
pass >= range.[0] && pass <= range.[1]
let hasDuplicatedAdjacentDigit(pass: int) : bool =
pass.ToString().ToCharArray() |> Array.map (fun _c -> int(System.Char.GetNumericValue(_c)))
|> Array.toSeq |> Seq.pairwise |> Seq.exists (fun (a, b) -> a = b)
let neverDecrease(pass: int) : bool =
pass.ToString().ToCharArray() |> Array.map (fun _c -> int(System.Char.GetNumericValue(_c)))
|> Array.toSeq |> Seq.pairwise |> Seq.forall (fun (a, b) -> a <= b)
let isValidPassword(pass: int) : bool =
isSixDigitNumber pass && isInRange(input, pass) && hasDuplicatedAdjacentDigit pass && neverDecrease pass
let validPasswords =
[input.[0]..input.[1]]
|> List.filter (fun pass -> isValidPassword pass)
let execute =
validPasswords.Length