Skip to content

Commit 5e54bc4

Browse files
committed
day 19
1 parent e36131a commit 5e54bc4

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

day19.fsx

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#r "nuget: Flips"
2+
23
open Flips
34
open Flips.Types
45
open Flips.SliceMap
@@ -28,64 +29,79 @@ type Robot =
2829
| GeodeRobot
2930

3031
let readBlueprint (s: string) =
31-
match s with
32-
| Match
33-
"Each ore robot costs (.*) ore. Each clay robot costs (.*) ore. Each obsidian robot costs (.*) ore and (.*) clay. Each geode robot costs (.*) ore and (.*) obsidian."
34-
[ oo; co; bo; bc; go; gb ] -> [
35-
OreRobot, [ Ore, int oo ]
36-
ClayRobot, [ Ore, int co ]
37-
ObsidianRobot, [ Ore, int bo; Clay, int bc ]
38-
GeodeRobot, [ Ore, int go; Obsidian, int gb ]
39-
]
32+
match s with
33+
| Match "Each ore robot costs (.*) ore. Each clay robot costs (.*) ore. Each obsidian robot costs (.*) ore and (.*) clay. Each geode robot costs (.*) ore and (.*) obsidian." [ oo
34+
co
35+
bo
36+
bc
37+
go
38+
gb ] ->
39+
[ OreRobot, [ Ore, int oo ]
40+
ClayRobot, [ Ore, int co ]
41+
ObsidianRobot, [ Ore, int bo; Clay, int bc ]
42+
GeodeRobot, [ Ore, int go; Obsidian, int gb ] ]
4043

4144
let blueprints = lines |> Array.map readBlueprint
4245

4346
let model blueprint totalSteps =
44-
let steps = [1..totalSteps]
47+
let steps = [ 1..totalSteps ]
4548
let robotTypes = [ OreRobot; ClayRobot; ObsidianRobot; GeodeRobot ]
4649
let resourceTypes = [ Ore; Clay; Obsidian; Geode ]
47-
let costs = blueprint |> List.collect (fun (r, res) -> res |> List.map (fun (o, x) -> (r, o), float x)) |> SMap2.ofList
48-
let robots =
50+
51+
let costs =
52+
blueprint
53+
|> List.collect (fun (r, res) -> res |> List.map (fun (o, x) -> (r, o), float x))
54+
|> SMap2.ofList
55+
56+
let robots =
4957
DecisionBuilder "robot" {
5058
for _ in steps do
51-
for t in robotTypes ->
52-
Boolean
53-
}
54-
|> SMap2.ofSeq
55-
let robotsCons =
59+
for t in robotTypes -> Boolean
60+
}
61+
|> SMap2.ofSeq
62+
63+
let robotsCons =
5664
ConstraintBuilder "factoryLimit" {
5765
for i in steps do
5866
yield sum robots[i, All] <== 1.0
59-
}
60-
let collectMult i = [1..i-1] |> List.map (fun j -> j, float (i-j)) |> SMap.ofList
61-
let buys =
67+
}
68+
69+
let collectMult i =
70+
[ 1 .. i - 1 ] |> List.map (fun j -> j, float (i - j)) |> SMap.ofList
71+
72+
let buys =
6273
ConstraintBuilder "buy" {
6374
for i in steps do
64-
for (t,r) in Seq.zip robotTypes resourceTypes ->
65-
sum (robots[LessThan (i-1), t] .* (collectMult (i-1))) + (if t=OreRobot && i > 1 then float (i-1) else 0.0) >== sum (robots[LessOrEqual i,All] .* costs[All,r])
66-
}
75+
for (t, r) in Seq.zip robotTypes resourceTypes ->
76+
sum (robots[LessThan(i - 1), t] .* (collectMult (i - 1)))
77+
+ (if t = OreRobot && i > 1 then float (i - 1) else 0.0)
78+
>== sum (robots[LessOrEqual i, All] .* costs[All, r])
79+
}
80+
6781
let objExpr = sum (robots.[All, GeodeRobot] .* collectMult totalSteps)
6882
let objective = Objective.create "Obj" Maximize objExpr
83+
6984
let model =
7085
Model.create objective
7186
|> Model.addConstraints robotsCons
7287
|> Model.addConstraints buys
88+
7389
match Solver.solve Settings.basic model with
7490
| Optimal sol ->
7591
//sol.DecisionResults |> Map.filter (fun _ x -> x > 0.0) |> Map.toSeq |> Seq.map (fun (k, x) -> k.Name, x) |> Seq.sort |> Seq.iter (printfn "%A")
7692
sol.ObjectiveResult |> int
7793

7894

79-
let part1() =
95+
let part1 () =
8096
let results = blueprints |> Array.mapi (fun i b -> i, model b 24)
8197
printfn "%A" results
82-
results |> Seq.map (fun (i, x) -> (i+1) * x) |> Seq.sum
98+
results |> Seq.map (fun (i, x) -> (i + 1) * x) |> Seq.sum
8399

84-
printfn $"PART1: {part1()}"
100+
printfn $"PART1: {part1 ()}"
85101

86-
let part2() =
102+
let part2 () =
87103
let results = blueprints[..2] |> Array.map (fun b -> model b 32)
88104
printfn "%A" results
89105
results |> Seq.reduce (*)
90106

91-
printfn $"PART2: {part2()}"
107+
printfn $"PART2: {part2 ()}"

0 commit comments

Comments
 (0)