Skip to content

Instantly share code, notes, and snippets.

@jwosty
Created August 6, 2019 19:14
Show Gist options
  • Save jwosty/a105707360359816dcba9d40b5ae1b59 to your computer and use it in GitHub Desktop.
Save jwosty/a105707360359816dcba9d40b5ae1b59 to your computer and use it in GitHub Desktop.
splendor
type Color = Blue | Red | Green | White | Black
type Token = Color * int
type Bank = {balance:Token list}
type Card = {cost:Token list}
let canBuyThisOne oneBalance oneCost =
match oneBalance,oneCost with
| (bcolor,b), (ccolor,c) -> bcolor = ccolor && b>=c
[
canBuyThisOne (Blue,2) (Blue,2) = true
canBuyThisOne (Blue,2) (Blue,1) = true
canBuyThisOne (Blue,2) (Blue,3) = false
canBuyThisOne (Red,2) (Blue,2) = false
canBuyThisOne (Red,2) (Red,2) = true
]
let rec canBuy {balance=allb} {cost=allc} =
match allc with
| [] -> true
| [onec] -> match (allb |> List.tryFind (fun oneb -> canBuyThisOne oneb onec )) with
| Some _ -> true
| None -> false
| onec::rest -> match (allb |> List.tryFind (fun oneb -> canBuyThisOne oneb onec )) with
| Some _ -> canBuy {balance=allb} {cost=rest}
| None -> false
[
canBuy {balance=[]} {cost=[]} = true
canBuy {balance=[Blue,1]} {cost=[Blue,1]} = true
canBuy {balance=[Blue,1]} {cost=[Blue,2]} = false
canBuy {balance=[Red,1]} {cost=[Blue,1]} = false
canBuy {balance=[Red,1]} {cost=[Red,1]} = true
canBuy {balance=[Red,1;Blue,1]} {cost=[Blue,1]} = true
canBuy {balance=[Red,1]} {cost=[Blue,1;Red,1]} = false
canBuy {balance=[Red,1;Blue,2]} {cost=[Blue,1;Red,1]} = true
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment