Skip to content

Instantly share code, notes, and snippets.

@ambuc
Created April 29, 2017 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambuc/01187518b73c21029e8ef427cc9137be to your computer and use it in GitHub Desktop.
Save ambuc/01187518b73c21029e8ef427cc9137be to your computer and use it in GitHub Desktop.
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