-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day02.fs
48 lines (41 loc) · 1.25 KB
/
Day02.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
module Day02
let parseLine (line: string) =
let parseHF (txt: string) =
txt.Split(",")
|> List.ofArray
|> List.map (fun kind ->
let [| countTxt; colour |] = kind.Trim().Split(" ")
(colour, int countTxt))
let [| head; tail |] = line.Split(":")
let id = int <| head.Split(" ").[1]
let handfuls =
tail.Split(";")
|> Array.map parseHF
|> List.ofArray
(id, handfuls)
let possible red green blue handFulls =
let hfPossible hf =
match hf with
| "red", count -> count <= red
| "green", count -> count <= green
| "blue", count -> count <= blue
handFulls |> List.forall (List.forall hfPossible)
let power handFulls =
handFulls
|> List.collect id
|> List.fold
(fun (r, g, b) (colour, count) ->
match colour with
| "red" -> (max r count, g, b)
| "green" -> (r, max g count, b)
| "blue" -> (r, g, max b count))
(0, 0, 0)
|> (fun (r, g, b) -> r * g * b)
let part1 getLines =
getLines "input"
|> List.map parseLine
|> List.filter (snd >> (possible 12 13 14))
|> List.sumBy fst
let part2 getLines =
getLines "input"
|> List.sumBy (parseLine >> snd >> power)