-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3.fs
More file actions
47 lines (42 loc) · 1.29 KB
/
Copy pathDay3.fs
File metadata and controls
47 lines (42 loc) · 1.29 KB
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
module Day3
open System
let priority =
let chars: seq<char> = Seq.append(seq { 'a'..'z' }) (seq { 'A'..'Z' })
let numbers = seq { 1..52 }
Seq.zip chars numbers
|> Map
let Part1 (lines:string array) =
lines
|> Seq.map (fun s ->
// each line, split in half, find common letters
let split =
let first = s |> Seq.take (s.Length/2) |> Set
let second = s |> Seq.skip (s.Length/2) |> Set
let common = Set.intersect first second
let p =
common
|> Seq.map (fun f -> priority[f])
|> Seq.reduce (+)
p
split)
let Part2 (lines:string array) =
let intersect (s: string array) =
match s.Length with
| 3 -> Set.intersect(Set s[0]) (Set s[1]) |> Set.intersect (Set s[2])
| 2 -> Set.intersect(Set s[0]) (Set s[1])
| _ -> Set.empty
lines
|> Array.chunkBySize 3
|> Seq.map (fun chunk ->
let common = intersect chunk
let p =
common
|> Seq.map (fun f -> priority[f])
|> Seq.reduce (+)
p
)
let RucksackPriority (i:string) (seperator:string) priorityScoring =
let lines = i.Split seperator |> Array.where (fun f -> not(String.IsNullOrEmpty f))
lines
|> priorityScoring
|> Seq.reduce (+)