-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay16.fs
79 lines (65 loc) · 1.7 KB
/
Day16.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module Year2015Day16
open AdventOfCode.FSharp.Common
type Sue = { Number : int; Items : Map<string, int> }
let parseSue line =
let toks = splitBy " " id line
let num = toks.[1].TrimEnd(':') |> int
let items =
toks
|> Seq.skip 2
|> Seq.chunkBySize 2
|> Seq.map (fun (entry : string []) ->
let item = entry.[0].TrimEnd(':')
let amount = int (entry.[1].TrimEnd(','))
item, amount)
|> Map.ofSeq
{ Number = num; Items = items }
let parse = parseEachLine parseSue
let amounts =
[
3, "children"
7, "cats"
2, "samoyeds"
3, "pomeranians"
0, "akitas"
0, "vizslas"
5, "goldfish"
3, "trees"
2, "cars"
1, "perfumes"
]
let scoreSue1 sue =
let tryGet item =
if Map.containsKey item sue.Items then Some sue.Items.[item]
else None
amounts
|> Seq.exists (fun (a, i) ->
match tryGet i with
| Some x -> a <> x
| None -> false)
|> not
let solvePart1 sues =
sues
|> Seq.filter scoreSue1
|> Seq.head
|> (fun s -> s.Number)
let scoreSue2 sue =
let tryGet item =
if Map.containsKey item sue.Items then Some sue.Items.[item]
else None
amounts
|> Seq.exists (fun (a, i) ->
match tryGet i with
| Some x ->
match i with
| "cats" | "trees" -> x <= a
| "pomeranians" | "goldfish" -> x >= a
| _ -> x <> a
| None -> false)
|> not
let solvePart2 sues =
sues
|> Seq.filter scoreSue2
|> Seq.head
|> (fun s -> s.Number)
let solver = { parse = parse; part1 = solvePart1; part2 = solvePart2 }