Skip to content

Commit

Permalink
Working Day 1 (1 + 2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anup Kishore committed Dec 26, 2017
1 parent 365bcfa commit 89d5a82
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
36 changes: 33 additions & 3 deletions day1/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,43 @@

module Main where

import Data.Char (digitToInt)
import Data.Text (strip, pack, unpack)
import Paths_aoc2017
import System.Environment (getArgs)
import System.Exit (exitSuccess)

file :: IO FilePath
file = getDataFileName "data/1-1"

getData :: IO String
getData = readFile =<< file
getData :: IO [Int]
getData = map digitToInt . unpack . strip . pack <$> (readFile =<< file)

rotate :: Int -> [Int] -> [Int]
rotate _ [] = []
rotate n xs = drop n xs ++ take n xs

formPairs :: Int -> [Int] -> [(Int, Int)]
formPairs n l = zip l $ rotate n l

usage :: IO ()
usage = putStrLn "Usage: stack exec day-n [--two] [...args]"

doOne :: [String] -> IO ()
doOne _ = print . sum . map fst . filter (uncurry (==)) <$> formPairs 1 =<< getData

doTwo :: [String] -> IO ()
doTwo _ =
print . sum . map fst . filter (uncurry (==)) <$> (\l -> formPairs (length l `div` 2) l) =<<
getData

parse :: [String] -> IO ()
parse ("--two":xs) = doTwo xs
parse ["--help"] = usage
parse xs = doOne xs

main :: IO ()
main = print =<< getData
main = do
args <- getArgs
parse args
exitSuccess
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ extra-source-files:

dependencies:
- base >=4.7 && <5
- text >=1.2.2.0
ghc-options: [-Wall, -Wwarn]

library:
Expand Down

0 comments on commit 89d5a82

Please sign in to comment.