summaryrefslogtreecommitdiff
path: root/2026/4.hs
blob: 874f3b5e4f13916bb3663c61dbd263b4b8303e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{-# OPTIONS -Wno-x-partial #-}
module Main where

import Data.List (find, tails)
import Data.Maybe (fromJust)
import Data.Set qualified as Set

main :: IO ()
main = do
  codes <- lines <$> getContents
  let divisors n = n : filter (\d -> n `rem` d == 0) [1 .. n `quot` 2]
  let solve1 code =
        let dists = Set.fromList
              [j - i
              | (c1, i, cs) <- zip3 code [0..] (tail (tails code))
              , c1 /= '?'
              , (c2, j) <- zip cs [i+1..]
              , c2 /= '?', c1 /= c2]
            forbidden = Set.fromList
              [d | dist <- Set.toList dists, d <- divisors dist]
        in fromJust (find (`Set.notMember` forbidden) [1..]) :: Int
  print (sum (map solve1 codes))
  print (solve1 (concat codes))