-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
52 lines (40 loc) · 1.21 KB
/
run.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import Data.Maybe (mapMaybe)
import Data.List (transpose)
parseAll :: String -> [String]
parseAll = lines
gamma :: [String] -> Int
gamma = readBinary . map mostCommon . transpose
epsilon :: [String] -> Int
epsilon = readBinary . map leastCommon . transpose
part1 :: [String] -> Int
part1 input = gamma input * epsilon input
locate :: ([Char] -> Char) -> [String] -> String
locate selector = go
where go = \case
[] -> []
xs -> let m = selector (map head xs)
in m:go (mapMaybe (pickSimilar m) xs)
pickSimilar m =
\case [_] -> Nothing
x:xs | x == m -> Just xs
_ -> Nothing
oxyGen :: [String] -> Int
oxyGen = readBinary . locate mostCommon
co2Scrub :: [String] -> Int
co2Scrub = readBinary . locate leastCommon
part2 :: [String] -> Int
part2 input = oxyGen input * co2Scrub input
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)