-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16_part01.fs
94 lines (83 loc) · 2.78 KB
/
day16_part01.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
module day16_part01
open System.Collections.Generic
open System.Text.RegularExpressions
open AdventOfCode_2015.Modules
type AuntSue = {
Id: int
Children: int option
Cats: int option
Samoyeds: int option
Pomeranians: int option
Akitas: int option
Vizslas: int option
Goldfish: int option
Trees: int option
Cars: int option
Perfumes: int option
}
let getValue(dic: Dictionary<string, int>)(attr: string) =
if dic.ContainsKey(attr) then
Some((int)(dic[attr]))
else
None
let parseContent(lines: string array) =
let regexpattern = @"Sue (?<id>\d+):(?:(?:\s(?<attribute>\w+): (?<value>\d+),?))+"
lines
|> Array.map(fun l ->
let matche = Regex.Match(l, regexpattern)
let attributes = Dictionary<string, int>()
for attr, value in Seq.zip matche.Groups.["attribute"].Captures matche.Groups.["value"].Captures do
attributes.Add(attr.Value, int value.Value)
{
Id = (int)(matche.Groups["id"].Value);
Children = getValue attributes "children";
Cats = getValue attributes "cats";
Samoyeds = getValue attributes "samoyeds";
Pomeranians = getValue attributes "pomeranians";
Akitas = getValue attributes "akitas";
Vizslas = getValue attributes "vizslas";
Goldfish = getValue attributes "goldfish";
Trees = getValue attributes "trees";
Cars = getValue attributes "cars";
Perfumes = getValue attributes "perfumes";
}
)
let MFCSAM = {
Id = -1;
Children = Some(3);
Cats = Some(7);
Samoyeds = Some(2);
Pomeranians = Some(3);
Akitas = Some(0);
Vizslas = Some(0);
Goldfish = Some(5);
Trees = Some(3);
Cars = Some(2);
Perfumes = Some(1)
}
let findCompatible(giftlist: AuntSue) (auntlist: AuntSue array) =
let fits((left, right): int option*int option) =
if right.IsSome then
left.Value = right.Value
else
true
let auntFits((left, right): AuntSue*AuntSue) =
fits(left.Children, right.Children) &&
fits(left.Cats, right.Cats) &&
fits(left.Samoyeds, right.Samoyeds) &&
fits(left.Pomeranians, right.Pomeranians) &&
fits(left.Akitas, right.Akitas) &&
fits(left.Vizslas, right.Vizslas) &&
fits(left.Goldfish, right.Goldfish) &&
fits(left.Trees, right.Trees) &&
fits(left.Cars, right.Cars) &&
fits(left.Perfumes, right.Perfumes)
let foundAunt =
auntlist
|> Array.filter(fun a -> auntFits(giftlist, a))
foundAunt[0].Id
let execute =
let input = "./day16/day16_input.txt"
let content = LocalHelper.GetLinesFromFile input
let sueAunts = parseContent content
findCompatible MFCSAM sueAunts