-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
51 lines (40 loc) · 1.77 KB
/
Program.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
50
51
module Day05
open System
open System.IO
let isNice (input: string) =
// It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou.
let rule1 =
let vowels = ['a';'e';'i';'o';'u']
Seq.filter (fun c -> List.contains c vowels) input |> Seq.length >= 3
// It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd).
let rule2 = input |> Seq.pairwise |> Seq.exists (fun (a, b) -> a = b)
// It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
let rule3 =
["ab";"cd";"pq";"xy"]
|> List.map (fun str -> input.Contains(str))
|> List.forall not
[rule1; rule2; rule3] |> List.forall id
let isNice2 (input: string) =
// It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
let rule1 =
Seq.windowed 2 input
|> Seq.map (Seq.toArray >> String)
|> Seq.indexed
|> Seq.filter (fun (i, s) -> input.IndexOf(s, i + 2) >= 0)
|> Seq.length > 0
// It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
let rule2 =
Seq.windowed 3 input
|> Seq.map Seq.toArray
|> Seq.exists (fun chars -> chars.[0] = chars.[2])
[rule1; rule2] |> List.forall id
let partOne = List.filter isNice >> List.length
let partTwo = List.filter isNice2 >> List.length
[<EntryPoint>]
let main argv =
let input =
File.ReadLines("Input.txt")
|> Seq.toList
partOne input |> printfn "Part one: %d"
partTwo input |> printfn "Part two: %d"
0