-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17_part02.fs
42 lines (36 loc) · 1.3 KB
/
day17_part02.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
module day17_part02
open AdventOfCode_2015.Modules
let parseContent(lines: string array) =
lines |> Array.map int
let findSmallestCombinationsMemoized target numbers =
let memo = System.Collections.Generic.Dictionary<_, _>()
let rec inner target numbers =
match target, numbers with
| 0, _ -> [ [] ]
| _, [] -> []
| _, x :: xs ->
if x > target then
inner target xs
else
let key = (target, numbers)
if memo.ContainsKey(key) then
memo[key]
else
let withX = inner (target - x) xs |> List.map (fun subset -> x :: subset)
let withoutX = inner target xs
let result = withX @ withoutX
memo[key] <- result
result
let possibleCombinations = inner target numbers
let (_, smallest) =
possibleCombinations
|> List.groupBy _.Length
|> List.sort
|> List.head
smallest.Length
let execute =
let input = "./day17/day17_input.txt"
let content = LocalHelper.GetLinesFromFile input
let containers = parseContent content |> List.ofArray
let possibleways = findSmallestCombinationsMemoized 150 containers
possibleways