2024-12-02 18:55:47 +11:00
|
|
|
module Aoc where
|
|
|
|
|
2024-12-03 04:45:27 +11:00
|
|
|
aocMain' :: (Show p, Show a) => ([String] -> p) -> (p -> (a, c)) -> (p -> c -> a) -> IO ()
|
|
|
|
aocMain' parseFile part1 part2 = do
|
2024-12-02 18:55:47 +11:00
|
|
|
contents <- readFile "input.txt"
|
|
|
|
let input = parseFile $ lines contents
|
2024-12-03 04:45:27 +11:00
|
|
|
let (part1Answer, data') = part1 input
|
|
|
|
putStrLn $ "Part 1: " ++ show part1Answer
|
|
|
|
let part2Answer = part2 input data'
|
|
|
|
putStrLn $ "Part 2: " ++ show part2Answer
|
|
|
|
|
|
|
|
passthrough :: a -> (a, a)
|
|
|
|
passthrough a = (a, a)
|
|
|
|
|
|
|
|
aocMain :: Show p => ([String] -> p) -> (p -> Int) -> (p -> Int) -> IO ()
|
|
|
|
aocMain parseFile part1 part2 = aocMain' parseFile ((,()) . part1) (const' part2)
|
|
|
|
|
|
|
|
const' :: (a -> b) -> a -> c -> b
|
|
|
|
const' f a _ = f a
|
2024-12-02 18:55:47 +11:00
|
|
|
|
|
|
|
enumerate :: [a] -> [(Int, a)]
|
2024-12-02 21:14:08 +11:00
|
|
|
enumerate xs = zip [0..length xs] xs
|
|
|
|
|
|
|
|
unimplemented :: Show p => p -> Int
|
|
|
|
unimplemented = error . show
|
|
|
|
|
|
|
|
pair :: [a] -> (a, a)
|
|
|
|
pair as = let [a1, a2] = as in (a1, a2)
|
|
|
|
|
|
|
|
triplet :: [a] -> (a, a, a)
|
2024-12-02 22:09:59 +11:00
|
|
|
triplet as = let [a1, a2, a3] = as in (a1, a2, a3)
|
|
|
|
|
|
|
|
single :: [a] -> a
|
|
|
|
single as = let [a] = as in a
|
|
|
|
|
|
|
|
both :: (a -> b) -> (a, a) -> (b, b)
|
|
|
|
both f (a, b) = (f a, f b)
|
2024-12-03 04:45:27 +11:00
|
|
|
|
|
|
|
lastSplit :: Int -> [a] -> ([a], [a])
|
|
|
|
lastSplit n as = splitAt (length as - n) as
|