feat: Set project up and implement day1 part1
This commit is contained in:
commit
022cf2a609
8 changed files with 1142 additions and 0 deletions
41
day1/Main.hs
Normal file
41
day1/Main.hs
Normal file
|
@ -0,0 +1,41 @@
|
|||
module Main where
|
||||
|
||||
import System.Environment (getArgs)
|
||||
import System.IO (Handle, hIsEOF, hGetLine, withFile, IOMode(ReadMode))
|
||||
import Text.Read (readMaybe)
|
||||
|
||||
import Data.List (sort)
|
||||
import Data.Char (isDigit, isSpace)
|
||||
|
||||
mapSnd :: (b -> c) -> (a, b) -> (a, c)
|
||||
mapSnd f (a, b) = (a, f b)
|
||||
|
||||
parseLine :: String -> IO (Int, Int)
|
||||
parseLine line = let (l, (_, r)) = (mapSnd (span isSpace). span isDigit) line
|
||||
in
|
||||
case readMaybe l >>= \l' -> readMaybe r
|
||||
>>= \r' -> return (l', r') of
|
||||
Just a -> return a
|
||||
Nothing -> fail "Input file is incorrectly formatted"
|
||||
|
||||
readLists :: Handle -> IO ([Int], [Int])
|
||||
readLists h = do
|
||||
shouldStop <- hIsEOF h
|
||||
if shouldStop
|
||||
then return ([], [])
|
||||
else do
|
||||
line <- hGetLine h
|
||||
(l, r) <- parseLine line
|
||||
(ls, rs) <- readLists h
|
||||
return (l:ls, r:rs)
|
||||
|
||||
computeDistance :: [Int] -> [Int] -> Int
|
||||
computeDistance l r = sum (zipWith (curry (abs . uncurry (-))) l r)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
(fp:_) <- return args
|
||||
(l, r) <- withFile fp ReadMode readLists
|
||||
|
||||
putStrLn ("Solution (Part 1) : " ++ show (computeDistance (sort l) (sort r)))
|
1000
day1/puzzle_input
Normal file
1000
day1/puzzle_input
Normal file
File diff suppressed because it is too large
Load diff
6
day1/test_input
Normal file
6
day1/test_input
Normal file
|
@ -0,0 +1,6 @@
|
|||
3 4
|
||||
4 3
|
||||
2 5
|
||||
1 3
|
||||
3 9
|
||||
3 3
|
Loading…
Add table
Add a link
Reference in a new issue