Skip to content

Instantly share code, notes, and snippets.

@Hawkuro
Last active March 10, 2017 13:08
Show Gist options
  • Save Hawkuro/9f2aaf577251e7c6cd939d836bd70a7e to your computer and use it in GitHub Desktop.
Save Hawkuro/9f2aaf577251e7c6cd939d836bd70a7e to your computer and use it in GitHub Desktop.
Calculating the optimal number of inventory slot upgrades in TLoZ:BotW
let totalSeeds = 900
let Fibonacci =
Seq.unfold (fun (pp,p) ->
let n = p+pp
Some((n),(p,n))
) (0,1)
let FiboSumMap =
Seq.unfold (fun (f,sum,i) ->
let n = sum + Seq.head f
Some((n, i),(Seq.tail f,n,i+1))
) (Fibonacci,0,1)
|> Seq.takeWhile (fst >> (>=) totalSeeds)
|> Map.ofSeq
let FiboSums =
FiboSumMap
|> Map.toArray
|> Array.map fst
let minFibo = Seq.min FiboSums
let (|FiboNum|_|) t = FiboSumMap.TryFind t
let FiboSource ta tb tc =
match ta, tb, tc with
| (FiboNum a, FiboNum b, FiboNum c) -> Some(a,b,c)
| _ -> None
let U900Sources =
let l = (Array.length FiboSums) - 1
let findFC fa fb =
// Finds the largest FiboSum that brings the total under (or equal to) the total number of seeds
// Other FiboSums can be ignored as they will hve smaller sources/slots
FiboSums
|> Array.findBack (fun fc -> fa+fb+fc <= totalSeeds)
seq {for b in 0..l -> seq {for a in b..l -> (FiboSums.[a],FiboSums.[b])}}
|> Seq.collect id
|> Seq.filter (fun (fa,fb) -> fa+fb+minFibo <= totalSeeds)
|> Seq.map (fun (fa,fb) -> (fa,fb,findFC fa fb))
|> Seq.choose (fun (fa,fb,fc) -> FiboSource fa fb fc)
let maxSum =
U900Sources
|> Seq.map (fun (a,b,c) -> a+b+c)
|> Seq.max
U900Sources
|> Seq.filter (fun (a,b,c) -> a+b+c = maxSum)
|> Seq.iter (printfn "%A")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment