-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.hs
28 lines (22 loc) · 1.16 KB
/
day4.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
import Data.List
import Text.Regex
main = do
day4_1
day4_2
day4_1 = do
input <- readFile "day4.txt"
let dmin = read (head $ splitAtHyphen input) :: Int
let dmax = read (last $ splitAtHyphen input) :: Int
putStrLn ("Day 04, Problem 1: " ++ (show $ length $ filter (\x -> dmin <= x && x <= dmax) [a*10^5 + b*10^4 + c*10^3 + d*10^2 + e*10 + f | a <- [0..9], b <- [a..9], c <- [b..9], d <- [c..9], e <- [d..9], f <- [e..9], (length $ nub [a,b,c,d,e,f]) < (length [a,b,c,d,e,f])]))
day4_2 = do
input <- readFile "day4.txt"
let dmin = read (head $ splitAtHyphen input) :: Int
let dmax = read (last $ splitAtHyphen input) :: Int
putStrLn ("Day 04, Problem 2: " ++ (show $ length $ filter (\(x,a) -> dmin <= x && x <= dmax && validInstances a) [(a*10^5 + b*10^4 + c*10^3 + d*10^2 + e*10 + f,[a,b,c,d,e,f]) | a <- [0..9], b <- [a..9], c <- [b..9], d <- [c..9], e <- [d..9], f <- [e..9]]))
validInstances :: [Int] -> Bool
validInstances a = any (\x -> instCt x a == 2) a
-- copied from some of my other code
instCt :: Eq a => a -> [a] -> Int
instCt x = length.filter (x==)
splitAtHyphen :: String -> [String]
splitAtHyphen a = splitRegex (mkRegex "\\-") a