2015 Day 04 in Haskell
This commit is contained in:
		
							parent
							
								
									dbc69a8a6d
								
							
						
					
					
						commit
						055c95b9e3
					
				
							
								
								
									
										23
									
								
								2015/day04/day04.hs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								2015/day04/day04.hs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
import Aoc
 | 
			
		||||
import Crypto.Hash.MD5 ( hash )
 | 
			
		||||
import Data.ByteString.Char8 ( pack )
 | 
			
		||||
import Data.ByteString.Builder ( byteStringHex, toLazyByteString )
 | 
			
		||||
import Data.ByteString.Lazy.Char8 ( pack, isPrefixOf )
 | 
			
		||||
 | 
			
		||||
parseFile :: [String] -> [Char]
 | 
			
		||||
parseFile = single
 | 
			
		||||
 | 
			
		||||
mine :: Int -> [Char] -> Int
 | 
			
		||||
mine n key = head $ filter matches [1..]
 | 
			
		||||
    where
 | 
			
		||||
        zeros = Data.ByteString.Lazy.Char8.pack $ replicate n '0'
 | 
			
		||||
        matches = isPrefixOf zeros . toLazyByteString . byteStringHex . hash . Data.ByteString.Char8.pack . (key++) . show
 | 
			
		||||
 | 
			
		||||
part1 :: [Char] -> Int
 | 
			
		||||
part1 = mine 5
 | 
			
		||||
 | 
			
		||||
part2 :: [Char] -> Int
 | 
			
		||||
part2 = mine 6
 | 
			
		||||
 | 
			
		||||
main :: IO ()
 | 
			
		||||
main = aocMain parseFile part1 part2
 | 
			
		||||
							
								
								
									
										6
									
								
								aoc.hs
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								aoc.hs
									
									
									
									
									
								
							@ -4,9 +4,9 @@ aocMain :: Show p => ([String] -> p) -> (p -> Int) -> (p -> Int) -> IO ()
 | 
			
		||||
aocMain parseFile part1 part2 = do
 | 
			
		||||
    contents <- readFile "input.txt"
 | 
			
		||||
    let input = parseFile $ lines contents
 | 
			
		||||
    -- print $ "Input: " ++ show input
 | 
			
		||||
    print $ "Part 1: " ++ show (part1 input)
 | 
			
		||||
    print $ "Part 2: " ++ show (part2 input)
 | 
			
		||||
    -- putStrLn $ "Input: " ++ show input
 | 
			
		||||
    putStrLn $ "Part 1: " ++ show (part1 input)
 | 
			
		||||
    putStrLn $ "Part 2: " ++ show (part2 input)
 | 
			
		||||
 | 
			
		||||
enumerate :: [a] -> [(Int, a)]
 | 
			
		||||
enumerate xs = zip [0..length xs] xs
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user