Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements to speed, custom miniflak mode
- Loading branch information
wheatwizard
committed
Jun 13, 2017
1 parent
26b4c11
commit af2a6d9
Showing
5 changed files
with
146 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ cabal.sandbox.config | |
.stack-work/ | ||
cabal.project.local | ||
.HTF/ | ||
BrainHack | ||
*.flk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module Helpers (pop,rest,topadd,interior,exterior) where | ||
|
||
pop :: (Integral a) => [a] -> a | ||
pop [] = 0 | ||
pop (x:_) = x | ||
|
||
rest :: (Integral a) => [a] -> [a] | ||
rest [] = [] | ||
rest (_:x) = x | ||
|
||
topadd :: [Integer] -> Integer -> [Integer] | ||
topadd [] x = [x] | ||
topadd (a:[]) x = [a+x] | ||
topadd (a:b) x = (a+x):b | ||
|
||
ir :: [Char] -> Integer -> [Char] | ||
ir x 0 = "" | ||
ir ('{':x) y = "{" ++ (ir x (y+1)) | ||
ir ('}':x) y = "}" ++ (ir x (y-1)) | ||
ir (a:x) y = [a] ++ (ir x y ) | ||
|
||
interior :: [Char] -> [Char] | ||
interior x = init (ir x 1) | ||
|
||
ex :: [Char] -> Integer -> [Char] | ||
ex x 0 = x | ||
ex ('{':x) y = ex x (y+1) | ||
ex ('}':x) y = ex x (y-1) | ||
ex (a:x) y = ex x y | ||
|
||
exterior :: [Char] -> [Char] | ||
exterior x = ex x 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
module MiniInterpreter (miniflak) where | ||
|
||
import Helpers | ||
|
||
bf :: [Char] -> ([Integer],[Integer]) -> ([Integer],[Integer]) | ||
bf [] (x,z)= (x,z) | ||
bf ('(':')':a) (x,z)= bf a (x,((pop z+1):rest z)) | ||
bf ('{':'}':a) (x,z)= bf a ((rest x),(topadd z (pop x))) | ||
bf ('[':']':a) (x,z)= bf a (x,(topadd z (toInteger (length x)))) | ||
bf ('(':a) (x,z)= bf a (x,(0:z)) | ||
bf ('[':a) (x,z)= bf a (x,(0:z)) | ||
bf (')':a) (x,(h:z))= bf a ((h:x),(topadd z h)) | ||
bf (']':a) (x,(h:z))= bf a (x,(topadd z (-h))) | ||
bf ('{':a) t = bf (exterior a) (run (interior a) t) | ||
bf (_:a) t = bf a t | ||
|
||
run :: [Char] -> ([Integer],[Integer]) -> ([Integer],[Integer]) | ||
run s ([],z) = ([],z) | ||
run s (0:x,z) = (0:x,z) | ||
run s x = run s (bf s x) | ||
|
||
--- xbf is a version of bf that counts cycles --- | ||
|
||
xbf :: [Char] -> ([Integer],[Integer],Int) -> ([Integer],[Integer],Int) | ||
xbf _ (_,_,c) | c `seq` False = undefined | ||
xbf [] (x,z,c)= (x,z,c+1) | ||
xbf ('(':')':a) (x,z,c)= xbf a (x,((pop z+1):rest z),c+1) | ||
xbf ('{':'}':a) (x,z,c)= xbf a ((rest x),(topadd z (pop x)),c+1) | ||
xbf ('[':']':a) (x,z,c)= xbf a (x,(topadd z (toInteger (length x))),c+1) | ||
xbf ('(':a) (x,z,c)= xbf a (x,(0:z),c+1) | ||
xbf ('[':a) (x,z,c)= xbf a (x,(0:z),c+1) | ||
xbf (')':a) (x,(h:z),c)= xbf a ((h:x),(topadd z h),c+1) | ||
xbf (']':a) (x,(h:z),c)= xbf a (x,(topadd z (-h)),c+1) | ||
xbf ('{':a) t = xbf (exterior a) (xrun (interior a) t) | ||
xbf (_:a) t = xbf a t | ||
|
||
xrun :: [Char] -> ([Integer],[Integer],Int) -> ([Integer],[Integer],Int) | ||
xrun s ([],z,c) = ([],z,c) | ||
xrun s (0:x,z,c) = (0:x,z,c) | ||
xrun s x = xrun s (xbf s x) | ||
|
||
bl :: [Char] -> [Char] -> Bool | ||
bl [] [] = True | ||
bl [] _ = False | ||
bl ('(':x) y = bl x (')':y) | ||
bl ('[':x) y = bl x (']':y) | ||
bl ('<':x) y = bl x ('>':y) | ||
bl ('{':x) y = bl x ('}':y) | ||
bl (a:x) [] | ||
| elem a ")]>}" = False | ||
| otherwise = bl x [] | ||
bl (a:x) (b:y) | ||
| elem a ")]>}" = (a == b) && (bl x y) | ||
| otherwise = bl x (b:y) | ||
|
||
balanced :: [Char] -> Bool | ||
balanced x = bl x [] | ||
|
||
clean :: [Char] -> [Char] | ||
clean [] = [] | ||
clean ('#':'{':xs) = clean (exterior xs) | ||
clean ('[':']':xs) = clean xs | ||
clean (x:xs) | ||
| elem x "()[]{}" = x:(clean xs) | ||
| otherwise = clean xs | ||
|
||
miniflak :: [Char] -> [Integer] -> Bool -> ([Integer],Int) | ||
miniflak s x c | ||
| balance && c = (\(a,_,c) -> (a,c)) (xbf (clean s) (x,[],0)) | ||
| balance && (not c) = (\(a,_) -> (a,0)) (bf (clean s) (x,[] )) | ||
| otherwise = error "Unbalanced braces." | ||
where balance = balanced s |