-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day19.fs
220 lines (169 loc) · 6.92 KB
/
Day19.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
module aoc22.Day19
// Ore * Clay * Obsidian * Geode
type Materials = int * int * int * int
module Materials =
let all (f: int -> int -> 'a) ((a1, a2, a3, a4): Materials) ((b1, b2, b3, b4): Materials) =
(f a1 b1, f a2 b2, f a3 b3, f a4 b4)
let toSeq ((a, b, c, d): 'a * 'a * 'a * 'a) =
seq {
a
b
c
d
}
let gte (a: Materials) (b: Materials) =
all (>=) a b |> toSeq |> Seq.reduce (&&)
let lte (a: Materials) (b: Materials) =
all (<=) a b |> toSeq |> Seq.reduce (&&)
let geodes ((_, _, _, g): Materials) = g
let zero: Materials = (0, 0, 0, 0)
type BlueprintEntry =
{ Needs: Materials
Produces: Materials }
type Robots = Robots of Materials
type Collected = Collected of Materials
type Blueprint = BlueprintEntry list
let harvest (Collected c) (Robots r) = Collected(Materials.all (+) c r)
let buildingOptions blueprints (Collected c) (waitingFor: int list) =
let canBuild =
blueprints
|> List.indexed
|> (fun l ->
match waitingFor with
| [] -> l
| _ -> l |> List.filter (fun (i, _) -> waitingFor |> List.contains i))
|> List.map (fun (i, e) ->
if Materials.gte c e.Needs then
let collectedAfterProduction = Materials.all (-) c e.Needs
Ok(Collected collectedAfterProduction, Robots e.Produces, [])
else
Error i)
let waitingFor =
canBuild
|> List.choose (function
| Error i -> Some i
| _ -> None)
let buildNothing =
match waitingFor with
| [] -> []
| _ -> [ (Collected c, Robots Materials.zero, waitingFor) ]
buildNothing @ (canBuild |> List.choose Result.toOption)
let simulateOneMinute blueprint (Collected c) (Robots r) waitingFor =
buildingOptions blueprint (Collected c) waitingFor
|> Seq.map (fun (Collected collectedAfterBuilding, Robots builtRobots, waitingFor) ->
let newCollected = Collected(Materials.all (+) collectedAfterBuilding r)
let newRobots = Robots(Materials.all (+) builtRobots r)
(newCollected, newRobots, waitingFor))
|> Array.ofSeq
let inline triangular (n: int) = n * (n + 1) / 2
let parallelFilter condition =
Array.Parallel.choose (fun x -> if condition x then Some x else None)
let rec simulateOneMinuteMany blueprint (states: (Collected * Robots * int list) array) i rounds =
let remainingMinutes = rounds + 1 - i
// remove configurations that can never reach the minimum goal
let minimumPossibleGeodes =
states
|> Array.Parallel.map (fun (Collected c, Robots r, _) ->
let openedGeodes = c |> Materials.geodes
let geodeRobots = r |> Materials.geodes
openedGeodes + geodeRobots * remainingMinutes)
|> Seq.max
let states' =
if minimumPossibleGeodes > 0 then
states
|> parallelFilter (fun (Collected(_, _, _, gc), Robots(_, _, _, gr), _) ->
gc + gr * remainingMinutes + (triangular remainingMinutes - 1)
>= minimumPossibleGeodes)
else
states
//printfn
// $"States reduced by {states.Length - states'.Length}, that don't match minimumPossibleGeodes: {minimumPossibleGeodes}"
let nextStates =
states'
|> Array.Parallel.collect (fun (c, r, w) -> simulateOneMinute blueprint c r w)
// don't build more lesser robots than we can use up materials of that type in one round
let robotLimit =
blueprint
|> Seq.map (fun e -> e.Needs)
|> Seq.reduce (fun a b -> Materials.all max a b)
let nextStates' =
nextStates
|> parallelFilter (fun (_, Robots(a, b, c, _), _) -> Materials.lte (a, b, c, 0) robotLimit)
|> Array.ofSeq
//printfn $"Round {i}, simulations: {nextStates'.Length}"
nextStates'
let simulateBlueprint rounds blueprint =
let start = (Collected Materials.zero, Robots(1, 0, 0, 0), [])
let possibleOutcomes =
([| start |], [ 1..rounds ])
||> Seq.fold (fun prevOutcomes i -> simulateOneMinuteMany blueprint prevOutcomes i rounds)
let maxGeodes =
possibleOutcomes
|> Seq.map (fun (Collected c, _, _) -> Materials.geodes c)
|> Seq.max
maxGeodes
let solve blueprints rounds =
blueprints |> List.map (simulateBlueprint rounds)
let part1 blueprints =
solve blueprints 24
|> List.indexed
|> List.map (fun (id, geodes) -> (id + 1) * geodes)
|> List.sum
let part2 blueprints =
solve (blueprints |> List.truncate 3) 32 |> Seq.reduce (*)
let parse str =
match tryRegexG "([0-9]+)" str with
| Some m when m.Length = 7 ->
let nums = m |> List.map int
[ // Each ore robot costs #1 ore
{ Needs = (nums.[1], 0, 0, 0)
Produces = (1, 0, 0, 0) }
// Each clay robot costs #2 ore
{ Needs = (nums.[2], 0, 0, 0)
Produces = (0, 1, 0, 0) }
// Each obsidian robot costs #3 ore and #4 clay
{ Needs = (nums.[3], nums.[4], 0, 0)
Produces = (0, 0, 1, 0) }
// Each geode robot costs #5 ore and #6 obsidian
{ Needs = (nums.[5], 0, nums.[6], 0)
Produces = (0, 0, 0, 1) } ]
| _ -> failwith $"can't parse blueprint: {str}"
let run () =
let input =
System.IO.File.ReadAllLines "inputs/day19.txt"
|> Array.map parse
|> List.ofArray
$"Part1: {input |> part1} Part2: {input |> part2}"
module tests =
open Xunit
open Swensen.Unquote
let example =
[ "Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian."
"Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian." ]
|> List.map parse
[<Fact>]
let gteTest () =
Materials.gte (1, 1, 1, 1) (0, 0, 0, 0) =! true
Materials.gte (1, 1, 1, 1) (1, 1, 1, 1) =! true
Materials.gte (2, 1, 1, 1) (1, 1, 1, 1) =! true
Materials.gte (1, 1, 1, 1) (1, 1, 1, 2) =! false
Materials.gte (0, 0, 0, 0) (1, 1, 1, 1) =! false
[<Fact>]
let ``Parse Test`` () =
example.[0]
=! [ // Each ore robot costs 4 ore
{ Needs = (4, 0, 0, 0)
Produces = (1, 0, 0, 0) }
// Each clay robot costs 2 ore
{ Needs = (2, 0, 0, 0)
Produces = (0, 1, 0, 0) }
// Each obsidian robot costs 3 ore and 14 clay
{ Needs = (3, 14, 0, 0)
Produces = (0, 0, 1, 0) }
// Each geode robot costs 2 ore and 7 obsidian
{ Needs = (2, 0, 7, 0)
Produces = (0, 0, 0, 1) } ]
[<Fact>]
let ``Part 1`` () = part1 example =! 33
[<Fact>]
let ``Part 2`` () = part2 example =! 56 * 62