forked from ss7m/malbolge-hs
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TWord.hs
66 lines (48 loc) · 1.4 KB
/
TWord.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module TWord (TWord, twordMax, crazy, rotR) where
import Data.Word
import Data.Ratio
import Data.Ix
newtype TWord = TW Word16 deriving (Eq, Ord, Real, Integral, Ix)
-- Maximum value of a word
twordMax :: TWord
twordMax = TW 59048
instance Show TWord where
show (TW x) = show x
instance Num TWord where
(TW x) + (TW y) = TW ((x + y) `mod` 59049)
(TW x) - (TW y) = TW ((x - y) `mod` 59049)
fromInteger = TW . fromInteger
(TW x) * (TW y) = TW ((x * y) `mod` 59049)
abs = id
signum _ = fromInteger 1
instance Enum TWord where
toEnum x = TW (toEnum (x `mod` 59049))
fromEnum (TW x) = fromEnum x
-- go from wrod to trits
toTrits :: TWord -> [TWord]
toTrits = reverse . take 10 . toTrits'
where
toTrits' x = (x `mod` 3) : toTrits' (x `div` 3)
-- Go from trits to word
fromTrits :: [TWord] -> TWord
fromTrits = foldl (\x y -> 3 * x + y) 0
-- Tritwise rotation right of a tword
rotR :: TWord -> TWord
rotR x = fromTrits (last trits : init trits)
where
trits = toTrits x
-- Tritwise definition of crazy operation
crazyTrit :: Integral a => a -> a -> a
crazyTrit 0 0 = 1
crazyTrit 0 1 = 0
crazyTrit 0 2 = 0
crazyTrit 1 0 = 1
crazyTrit 1 1 = 0
crazyTrit 1 2 = 2
crazyTrit 2 0 = 2
crazyTrit 2 1 = 2
crazyTrit 2 2 = 1
-- "Crazy" operation
crazy :: TWord -> TWord -> TWord
crazy x y = fromTrits $ map (uncurry crazyTrit) (zip (toTrits x) (toTrits y))