Skip to content

Instantly share code, notes, and snippets.

@ambuc ambuc/oracle.hs
Created Apr 29, 2017

Embed
What would you like to do?
Aaronson Oracle
import Data.Map as M
import Data.Maybe
import Numeric
learn :: [Bool] -> Map [Bool] Double -> Map [Bool] Double
learn hist brain = Prelude.foldr learn' brain [3..5]
where learn' n = M.insertWithKey (const (+)) (take n hist) 1.0
guess :: Map [Bool] Double -> [Bool] -> Bool
guess brain hist = wAvg (Prelude.map guess' [2..4]) >= 0.5
where wAvg xs = sum (Prelude.map (uncurry (*)) xs) / sum (Prelude.map snd xs)
guess' n = (occ True / (occ True + occ False), occ True + occ False)
where occ val = fromMaybe 0.0 $ M.lookup (val : take n hist) brain
play :: Int -> (Double, Double) -> [Bool] -> Map [Bool] Double -> IO a
play turn (wons, total) hist brain = do
press <- getLine
let key = press == "f"
let right = key == guess brain hist
let wins = (if right then succ else id) wons
print $ "I guessed " ++ (if right then "RIGHT!" else "wrong.")
++ " My avg: " ++ showFFloat (Just 2) (wins / succ total) ""
play (succ turn) (wins, succ total) (key:hist) (learn (key:hist) brain)
main = do
print "aaronson oracle | Press 'f' or 'd' over and over (followed by enter)"
print " | and we'll try to predict which you'll press next."
play 0 (0.0, 0.0) [] M.empty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.