Done several tasks
[cses-programming-tasks.git] / app / cses2205.hs
diff --git a/app/cses2205.hs b/app/cses2205.hs
new file mode 100644 (file)
index 0000000..e9fa3d5
--- /dev/null
@@ -0,0 +1,20 @@
+import Data.Word
+import Data.Bits
+import Numeric (showIntAtBase)
+import Data.Char (intToDigit)
+
+main :: IO ()
+main = do
+  line1 <- getLine
+  let nBits = read line1 :: Int
+  let n = fromIntegral $ 2 ^ nBits
+  let codes = [toGray x | x <- [0..(n - 1)]]
+  putStrLn $ unlines $ map (showBits nBits) codes
+  
+toGray :: Word16 -> Word16
+toGray n = n `xor` (n `shiftR` 1)
+
+showBits :: Int -> Word16 -> String
+showBits k n = prefix ++ suffix
+  where suffix = showIntAtBase 2 intToDigit n ""
+        prefix = replicate (k - length suffix) '0'
\ No newline at end of file