From 0e0607f86e9187fe36b3462f6ee95c74f36a1f94 Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Sun, 7 Dec 2025 15:33:52 +0100 Subject: [PATCH] Add solution to day 3. --- 2025/aoc/app/day2/Main.hs | 46 ++++++++++++++++++++++++++++++++++- 2025/aoc/inputs/day2.txt | 1 + 2025/aoc/inputs/day2_test.txt | 1 + 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 2025/aoc/inputs/day2.txt create mode 100644 2025/aoc/inputs/day2_test.txt diff --git a/2025/aoc/app/day2/Main.hs b/2025/aoc/app/day2/Main.hs index 2ae014b..ccc2210 100644 --- a/2025/aoc/app/day2/Main.hs +++ b/2025/aoc/app/day2/Main.hs @@ -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 diff --git a/2025/aoc/inputs/day2.txt b/2025/aoc/inputs/day2.txt new file mode 100644 index 0000000..32ff73e --- /dev/null +++ b/2025/aoc/inputs/day2.txt @@ -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 diff --git a/2025/aoc/inputs/day2_test.txt b/2025/aoc/inputs/day2_test.txt new file mode 100644 index 0000000..a3f22ef --- /dev/null +++ b/2025/aoc/inputs/day2_test.txt @@ -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