Last active
December 3, 2025 12:01
-
-
Save lotusirous/2070af22efe8af455a56a7bc8543f324 to your computer and use it in GitHub Desktop.
advent of code 2025 in haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Main where | |
| -- | Parse "11-22" into (11, 22) | |
| parseRange :: String -> (Integer, Integer) | |
| parseRange s = (read a, read b) | |
| where | |
| (a, _ : b) = break (== '-') s | |
| -- | Split on commas: "a,b,c" -> ["a", "b", "c"] | |
| splitCommas :: String -> [String] | |
| splitCommas = words . map (\c -> if c == ',' then ' ' else c) | |
| -- | Is the number's first half equal to its second? (e.g. 1212, 123123) | |
| isDoubled :: Integer -> Bool | |
| isDoubled n = even (length s) && a == b | |
| where | |
| s = show n | |
| (a, b) = splitAt (length s `div` 2) s | |
| -- | Is the number some pattern repeated at least twice? (e.g. 1212, 123123123, 1111) | |
| isRepeated :: Integer -> Bool | |
| isRepeated n = any matches [1 .. len `div` 2] | |
| where | |
| s = show n | |
| len = length s | |
| matches k = len `mod` k == 0 && take len (cycle (take k s)) == s | |
| solve :: String -> Integer | |
| solve input = sum [n | (lo, hi) <- map parseRange (splitCommas input), n <- [lo .. hi], isDoubled n] | |
| solve2 :: String -> Integer | |
| solve2 input = sum [n | (lo, hi) <- map parseRange (splitCommas input), n <- [lo .. hi], isRepeated n] | |
| main :: IO () | |
| main = do | |
| input <- readFile "./_input/day02.txt" | |
| putStrLn $ "Part 1: " ++ show (solve input) | |
| putStrLn $ "Part 2: " ++ show (solve2 input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment