Created
March 11, 2015 06:06
-
-
Save recursivecurry/572e3eaa458d8f8135bb to your computer and use it in GitHub Desktop.
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
module Starforce where | |
import Data.Bits | |
import Data.List | |
answers :: Int -> [Int] | |
answers = \bitnum -> concat $ map (bit_permutation bitnum) $ reverse [1..bitnum] | |
bit_permutation :: Int -> Int -> [Int] | |
bit_permutation bitnum onebit | |
| zerobit == 0 = [foldr (.|.) 0 $ map bit [0..(onebit-1)]] | |
| onebit == 0 = [0] | |
| otherwise = (bit_permutation (bitnum-1) onebit) | |
++ (map ((bit (bitnum-1)) .|.) (bit_permutation (bitnum-1) (onebit-1))) | |
where zerobit = bitnum - onebit | |
possible :: [Int] -> Int -> Int -> Bool | |
possible = possible' 0 | |
possible' :: Int -> [Int] -> Int -> Int -> Bool | |
possible' _ _ 0 _ = True | |
possible' _ [] _ _ = False | |
possible' c (n:ns) m a | |
| (partial .&. a) == a = possible' 0 ns (m-1) a | |
| otherwise = possible' partial ns m a | |
where partial = c .|. n | |
starforce :: [Int] -> Int -> Int | |
starforce ns m = case (find (possible ns m) (answers 16)) of Just n -> popCount n | |
Nothing -> 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment