Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Created January 1, 2013 18:55
Show Gist options
  • Save tos-kamiya/4429328 to your computer and use it in GitHub Desktop.
Save tos-kamiya/4429328 to your computer and use it in GitHub Desktop.
Komachi-zan 2013. Run as follows: $ runghc komachi2013gen.hs | runghc komachi2013chk.hs
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-}↲
import Control.Monad↲
import Text.Peggy↲
[peggy|↲
expr :: Double↲
= expr "+" fact { $1 + $2 }↲
/ expr "-" fact { $1 - $2 }↲
/ fact↲
fact :: Double↲
= fact "*" number { $1 * $2 }↲
/ number↲
number ::: Double↲
= [1-9] [0-9]* { read ($1 : $2) }↲
|]↲
main = do↲
inputLines <- getContents↲
-- print . lines $ inputLines↲
forM_ (lines inputLines) $ \line -> do↲
case parseString expr "<stdin>" line of↲
Right 2013 -> putStrLn line↲
_ -> return ()↲
import Data.Char↲
genexpr n s =↲
if (n == 10)↲
then do↲
putStrLn $ reverse s↲
else do↲
let c = chr $ n + ord '0'↲
let s1 = c:s↲
genexpr (n + 1) s1↲
let s2 = c:'-':s↲
genexpr (n + 1) s2↲
let s3 = c:'+':s↲
genexpr (n + 1) s3↲
let s4 = c:'*':s↲
genexpr (n + 1) s4↲
main = do↲
genexpr 2 "1"↲
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment