-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.fs
49 lines (41 loc) · 1.1 KB
/
Day2.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
43
44
45
46
47
48
49
module Day2
open FSharpPlus
type Policy = {
Lower: int
Upper: int
Char: char
}
type InputLine = {
Policy: Policy
Password: string
}
open System.Text.RegularExpressions
let parseLine input =
let regex = Regex.Match (input, "(\d+)\-(\d+) (.)\: (.+)")
let min = regex.Groups.[1].Value |> int
let max = regex.Groups.[2].Value |> int
let letter = regex.Groups.[3].Value |> char
let password = regex.Groups.[4].Value
{ Policy = {
Lower = min
Upper = max
Char = letter }
Password = password }
let part1valid policy password =
let count = Regex.Matches(password, policy.Char |> string).Count
count >= policy.Lower && count <= policy.Upper
let part2valid policy (password: string) =
let check pos = password.[pos - 1] = policy.Char
(check policy.Lower) <> (check policy.Upper)
let countValid validator =
Seq.where (fun line -> validator line.Policy line.Password)
>> Seq.length
open System
open System.IO
open Microsoft.FSharp.Core.Operators
let run =
"input/Day2.txt"
|> File.ReadAllLines :> _ seq
|> Seq.map parseLine
|> countValid part2valid
|> Console.WriteLine