Add solution to day 2.
This commit is contained in:
parent
d57f12e9c6
commit
25440a0ed0
3 changed files with 47 additions and 1 deletions
|
|
@ -1,4 +1,48 @@
|
|||
module Main (main) where
|
||||
|
||||
tupleEq :: (Eq a) => (a, a) -> Bool
|
||||
tupleEq (a, b) = a == b
|
||||
|
||||
splitOn :: (Eq a) => a -> [a] -> [[a]]
|
||||
splitOn x [] = []
|
||||
splitOn x xs =
|
||||
let e = takeWhile (/= x) xs
|
||||
in e : splitOn x (drop (length e + 1) xs)
|
||||
|
||||
isInvalidId :: Int -> Bool
|
||||
isInvalidId n =
|
||||
let e = floor (logBase 10 (fromIntegral n) :: Double) :: Int
|
||||
in (odd e && tupleEq (n `divMod` (10 ^ (e `div` 2 + 1))))
|
||||
|
||||
expandRange :: String -> [Int]
|
||||
expandRange s =
|
||||
case splitOn '-' s of
|
||||
[a, b] -> [(read a) .. (read b)]
|
||||
_ -> error "Invalid input!"
|
||||
|
||||
part1 :: IO ()
|
||||
part1 =
|
||||
getContents
|
||||
>>= print . sum . concatMap (filter isInvalidId . expandRange) . splitOn ',' . takeWhile (/= '\n')
|
||||
|
||||
chunksOf :: Int -> [a] -> [[a]]
|
||||
chunksOf _ [] = []
|
||||
chunksOf n xs = take n xs : chunksOf n (drop n xs)
|
||||
|
||||
allSame :: (Eq a) => [a] -> Bool
|
||||
allSame [] = False
|
||||
allSame [_] = False
|
||||
allSame (x:xs) = all (== x) xs
|
||||
|
||||
isInvalidId' :: Int -> Bool
|
||||
isInvalidId' num =
|
||||
let s = show num
|
||||
in or [allSame (chunksOf n s) | n <- [1 .. length s + 1]]
|
||||
|
||||
part2 :: IO ()
|
||||
part2 =
|
||||
getContents
|
||||
>>= print . sum . concatMap (filter isInvalidId' . expandRange) . splitOn ',' . takeWhile (/= '\n')
|
||||
|
||||
main :: IO ()
|
||||
main = putStrLn "Hi"
|
||||
main = part2
|
||||
|
|
|
|||
1
2025/aoc/inputs/day2.txt
Normal file
1
2025/aoc/inputs/day2.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
269351-363914,180-254,79-106,771-1061,4780775-4976839,7568-10237,33329-46781,127083410-127183480,19624-26384,9393862801-9393974421,2144-3002,922397-1093053,39-55,2173488366-2173540399,879765-909760,85099621-85259580,2-16,796214-878478,163241-234234,93853262-94049189,416472-519164,77197-98043,17-27,88534636-88694588,57-76,193139610-193243344,53458904-53583295,4674629752-4674660925,4423378-4482184,570401-735018,280-392,4545446473-4545461510,462-664,5092-7032,26156828-26366132,10296-12941,61640-74898,7171671518-7171766360,3433355031-3433496616
|
||||
1
2025/aoc/inputs/day2_test.txt
Normal file
1
2025/aoc/inputs/day2_test.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||
Loading…
Add table
Reference in a new issue