Add 2024 Days 01/02 in Haskell

This commit is contained in:
germax26 2024-12-02 18:55:47 +11:00
parent f7e6dd05df
commit 3933ed1e60
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
3 changed files with 53 additions and 0 deletions

18
2024/day01/day01.hs Normal file
View File

@ -0,0 +1,18 @@
import Data.List ( sort, group )
import Data.Maybe ( fromMaybe )
import Aoc ( aocMain )
parseFile :: [String] -> ([Int], [Int])
parseFile lines = unzip [(a, b) | line <- lines, let [a, b] = map read $ words line]
part1 :: ([Int], [Int]) -> Int
part1 (list1, list2) = sum $ zipWith (curry (abs . uncurry (-))) (sort list1) (sort list2)
part2 :: ([Int], [Int]) -> Int
part2 (list1, list2) = sum $ map score list1
where
score n = n * fromMaybe 0 (lookup n count)
count = map (\xs -> (head xs, length xs)) $ group $ sort list2
main :: IO ()
main = aocMain parseFile part1 part2

23
2024/day02/day02.hs Normal file
View File

@ -0,0 +1,23 @@
import Aoc ( aocMain )
parseFile :: [String] -> [[Int]]
parseFile = map (map read . words)
part1 :: [[Int]] -> Int
part1 reports =
length $ filter isSafe $ map differences reports
where
differences xs = zipWith (-) xs (tail xs)
isGradual dx = 1 <= abs dx && abs dx <= 3
isSafe dxs = all isGradual dxs && (all (<0) dxs || all (>0) dxs)
part2 :: [[Int]] -> Int
part2 reports =
length $ filter isSafe $ map remove1 reports
where
remove n xs = let (front, back) = splitAt n xs in front ++ tail back
remove1 report = map (`remove` report) [0..length report-1]
isSafe = (>0) . part1
main :: IO ()
main = aocMain parseFile part1 part2

12
aoc.hs Normal file
View File

@ -0,0 +1,12 @@
module Aoc where
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)
enumerate :: [a] -> [(Int, a)]
enumerate xs = zip [0..length xs] xs