Tackled problem 1625
[cses-programming-tasks.git] / app / cses2205.hs
1 import Data.Word
2 import Data.Bits
3 import Numeric (showIntAtBase)
4 import Data.Char (intToDigit)
5
6 main :: IO ()
7 main = do
8 line1 <- getLine
9 let nBits = read line1 :: Int
10 let n = fromIntegral $ 2 ^ nBits
11 let codes = [toGray x | x <- [0..(n - 1)]]
12 putStrLn $ unlines $ map (showBits nBits) codes
13
14 toGray :: Word16 -> Word16
15 toGray n = n `xor` (n `shiftR` 1)
16
17 showBits :: Int -> Word16 -> String
18 showBits k n = prefix ++ suffix
19 where suffix = showIntAtBase 2 intToDigit n ""
20 prefix = replicate (k - length suffix) '0'