Created
July 1, 2017 15:38
-
-
Save matsubara0507/a7cf9b578610b91829fd7949a69d5abc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import Control.Applicative (liftA2) | |
| import Data.List (unfoldr) | |
| import Data.Tuple (swap) | |
| solve :: Int -> [Int] | |
| solve = fmap (radix10 3 . uncurry gen) . filter (uncurry check) | |
| . zip [(2,0),(1,0),(0,1),(0,2)] . repeat . radix 3 | |
| check :: (Int, Int) -> [Int] -> Bool | |
| check (x,y) = liftA2 (&&) (not . null) ((== x) . head) . dropWhile (== y) . reverse | |
| gen :: (Int, Int) -> [Int] -> [Int] | |
| gen (x,y) = reverse . (\(as,b:bs) -> fmap (const x) as ++ (y:bs)) . span (== y) . reverse | |
| radix :: Int -> Int -> [Int] | |
| radix n = (:) 0 . reverse . unfoldr (fmap (swap . (`divMod` n)) . boolToMaybe (== 0)) | |
| boolToMaybe p a = if p a then Nothing else Just a | |
| radix10 :: Int -> [Int] -> Int | |
| radix10 n = sum . zipWith (\x y -> 3^x * y) [0..] . reverse | |
| -- > and $ fmap (\(x,y) -> solve x == y) tests | |
| -- True | |
| tests :: [(Int, [Int])] | |
| tests = [ (21, [19,22,23]) | |
| , (0, [1,2]) | |
| , (1, [0,3]) | |
| , (2, [0,6]) | |
| , (3, [1,4,5]) | |
| , (4, [3,9]) | |
| , (9, [4,10,11]) | |
| , (15, [11,16,17]) | |
| , (27, [13,28,29]) | |
| , (32, [30]) | |
| , (47, [45,51]) | |
| , (65, [63,69]) | |
| , (80, [78,162]) | |
| , (199, [198,201]) | |
| , (204, [200,205,206]) | |
| , (243, [121,244,245]) | |
| , (493, [492]) | |
| , (508, [507]) | |
| , (728, [726,1458]) | |
| , (793, [792,795]) | |
| , (902, [900,906]) | |
| , (981, [976,982,983]) | |
| , (1093, [1092,2187]) | |
| , (1202, [1200]) | |
| , (1300, [1299,1305]) | |
| , (1962, [1952,1963,1964]) | |
| , (2188, [2187,2190]) | |
| , (2405, [2403,2409]) | |
| , (3326, [3324]) | |
| , (6561, [3280,6562,6563]) | |
| , (6612, [6608,6613,6614]) | |
| , (7058, [7056,7062]) | |
| , (8444, [8442,8448]) | |
| , (9841, [9840,19683]) | |
| , (15243, [15239,15244,15245]) | |
| , (19946, [19944,19950]) | |
| , (21148, [21147]) | |
| , (39365, [39363]) | |
| , (39366, [19682,39367,39368]) | |
| , (55694, [55692,55698]) | |
| , (57245, [57243]) | |
| , (66430, [66429,66432]) | |
| , (92740, [92739]) | |
| , (115250, [115248]) | |
| , (163031, [163029]) | |
| , (221143, [221142,221157]) | |
| , (410353, [410352]) | |
| , (412649, [412647,412659]) | |
| , (550391, [550389]) | |
| , (699921, [699880,699922,699923]) | |
| , (797161, [797160,1594323]) | |
| , (1000000, [999999,1000002]) | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment