Advent of Code 2025
1module Day2 where
2
3import Debug.Trace
4
5split :: Char -> String -> [String]
6split c s = case dropWhile (== c) s of
7 "" -> []
8 s' -> w : split c s''
9 where (w, s'') = break (== c) s'
10
11isPartOneCandidate :: Int -> Bool
12isPartOneCandidate n =
13 let s = show n
14 d = length s
15 in take (d `div` 2) s == drop (d `div` 2) s
16
17isPartTwoCandidate :: Int -> Bool
18isPartTwoCandidate n =
19 let s = show n
20 d = length s
21 cands = [concat $ replicate k $ take (d `div` k) s | k <- [2..d]]
22 in any (== s) cands
23
24parseEntry :: [String] -> (Int, Int)
25parseEntry (a : b : _) = (read a, read b)
26parseEntry _ = error "Unreachable "
27
28parse :: String -> [(Int, Int)]
29parse s =
30 map (parseEntry . split '-') $
31 split ',' s
32
33part1 :: String -> String
34part1 input = show $
35 sum $
36 filter isPartOneCandidate $
37 concat $
38 map (\(a,b) -> [a..b]) $
39 parse input
40
41part2 :: String -> String
42part2 input = show $
43 sum $
44 filter isPartTwoCandidate $
45 concat $
46 map (\(a,b) -> [a..b]) $
47 parse input