Skip Navigation
Cryptic Crossword Daily puzzle
  • Oh, that's fun! (And looks like an easy way to lose track of a few hours as well...)

  • The Gargoyle Indexโ„ข is an indicator of a functioning society
  • In case anybody hasn't seen it, the relevant Oglaf (NSFW)

  • KB, MB, GB, and TB are all part of the metric system. What empirical measurements should we Freeโ„ข๏ธ Americans use for computer memory?
  • Most people would use "word", "half-word", "quarter-word" etc, but the Anglophiles insist on "tuppit", "ternary piece", "span" and "chunk" (that's 5 bits, or 12 old bits).

  • P.D AoC Leaderboard Results
  • Maybe it was due to attempting the puzzles in real-time for the first time, but it felt like there was quite a spike in difficulty this year. Day 5 (If You Give A Seed A Fertilizer) in particular was pretty tough for an early puzzle.

    Day 8 (Haunted Wasteland), Day 20 (Pulse Propagation) and Day 21 (Step Counter) were (I felt) a bit mean due to hidden properties of the input data.

    I particularly liked Day 6 (Wait For It), Day 14 (Parabolic Reflector Dish) and Day 24 (Never Tell Me The Odds), although that one made my brain hurt.

    Day 25 (Snowverload) had me reading research papers, although in the end I stumbled across Karger's algorithm. That's the first time I've used a probabilistic approach. This solution in particular was very clever.

    I learned the Shoelace formula and Pick's theorem this year, which will be very helpful to remember.

    Perhaps I'll try using Prolog or J next year :)

  • ๐Ÿ›ถ - 2023 DAY 18 SOLUTIONS -๐Ÿ›ถ
  • Oh, just like day 11! I hadn't thought of that. I was initially about to try something similar by separating into rectangular regions, as in ear-clipping triangulation. But that would require a lot of iterating, and something about "polygon" and "walking the edges" went ping in my memory...

  • ๐Ÿ›ถ - 2023 DAY 18 SOLUTIONS -๐Ÿ›ถ
  • Haskell

    Wasn't able to start on time today, but this was a fun one! Got to apply the two theorems I learned from somebody else's solution to Day 10.

    Solution
    import Data.Char
    import Data.List
    
    readInput :: String -> (Char, Int, String)
    readInput s =
      let [d, n, c] = words s
       in (head d, read n, drop 2 $ init c)
    
    boundary :: [(Char, Int)] -> [(Int, Int)]
    boundary = scanl' step (0, 0)
      where
        step (x, y) (d, n) =
          let (dx, dy) = case d of
                'U' -> (0, 1)
                'D' -> (0, -1)
                'L' -> (-1, 0)
                'R' -> (1, 0)
           in (x + n * dx, y + n * dy)
    
    area :: [(Char, Int)] -> Int
    area steps =
      let a = -- shoelace formula
            (abs . (`quot` 2) . sum)
              . (zipWith (\(x, y) (x', y') -> x * y' - x' * y) <*> tail)
              $ boundary steps
       in a + 1 + sum (map snd steps) `quot` 2 -- Pick's theorem
    
    part1, part2 :: [(Char, Int, String)] -> Int
    part1 = area . map (\(d, n, _) -> (d, n))
    part2 = area . map (\(_, _, c) -> decode c)
      where
        decode s = ("RDLU" !! digitToInt (last s), read $ "0x" ++ init s)
    
    main = do
      input <- map readInput . lines <$> readFile "input18"
      print $ part1 input
      print $ part2 input
    
  • [2023 Day 17] [Rust] Optimizing day 17 (spoilers)
  • Clever! And removing constraints doesn't increase the path cost, so it won't be an overestimate.

  • [2023 Day 17] [Rust] Optimizing day 17 (spoilers)
  • Some (not very insightful or helpful) observations:

    • The shortest path is likely to be mostly monotonic (it's quite hard for the "long way round" to be cost-effective), so the Manhattan distance is probably a good metric.
    • The center of the puzzle is expensive, so the straight-line distance is probably not a good metric
    • I'm pretty sure that the shortest route (for part one at least) can't self-intersect. Implementing this constraint is probably not going to speed things up, and there might be some pathological case where it's not true.

    Not an optimization, but I suspect that a heuristic-based "reasonably good" path such as a human would take will be fairly close to optimal.

  • ๐Ÿต - 2023 DAY 17 SOLUTIONS -๐Ÿต
  • Yeah, finding a good way to represent the "last three moves" constraint was a really interesting twist. You beat me to it, anyway!

  • ๐Ÿต - 2023 DAY 17 SOLUTIONS -๐Ÿต
  • Haskell

    Wowee, I took some wrong turns solving today's puzzle! After fixing some really inefficient pruning I ended up with a Dijkstra search that runs in 2.971s (for a less-than-impressive 124.782 l-s).

    Solution
    import Control.Monad
    import Data.Array.Unboxed (UArray)
    import qualified Data.Array.Unboxed as Array
    import Data.Char
    import qualified Data.HashSet as Set
    import qualified Data.PQueue.Prio.Min as PQ
    
    readInput :: String -> UArray (Int, Int) Int
    readInput s =
      let rows = lines s
       in Array.amap digitToInt
            . Array.listArray ((1, 1), (length rows, length $ head rows))
            $ concat rows
    
    walk :: (Int, Int) -> UArray (Int, Int) Int -> Int
    walk (minStraight, maxStraight) grid = go Set.empty initPaths
      where
        initPaths = PQ.fromList [(0, ((1, 1), (d, 0))) | d <- [(0, 1), (1, 0)]]
        goal = snd $ Array.bounds grid
        go done paths =
          case PQ.minViewWithKey paths of
            Nothing -> error "no route"
            Just ((n, (p@(y, x), hist@((dy, dx), k))), rest)
              | p == goal && k >= minStraight -> n
              | (p, hist) `Set.member` done -> go done rest
              | otherwise ->
                  let next = do
                        h'@((dy', dx'), _) <-
                          join
                            [ guard (k >= minStraight) >> [((dx, dy), 1), ((-dx, -dy), 1)],
                              guard (k < maxStraight) >> [((dy, dx), k + 1)]
                            ]
                        let p' = (y + dy', x + dx')
                        guard $ Array.inRange (Array.bounds grid) p'
                        return (n + grid Array.! p', (p', h'))
                   in go (Set.insert (p, hist) done) $
                        (PQ.union rest . PQ.fromList) next
    
    main = do
      input <- readInput <$> readFile "input17"
      print $ walk (0, 3) input
      print $ walk (4, 10) input
    

    (edited for readability)

  • ๐Ÿช - 2023 DAY 14 SOLUTIONS -๐Ÿช
  • Haskell

    A little slow (1.106s on my machine), but list operations made this really easy to write. I expect somebody more familiar with Haskell than me will be able to come up with a more elegant solution.

    Nevertheless, 59th on the global leaderboard today! Woo!

    Solution
    import Data.List
    import qualified Data.Map.Strict as Map
    import Data.Semigroup
    
    rotateL, rotateR, tiltW :: Endo [[Char]]
    rotateL = Endo $ reverse . transpose
    rotateR = Endo $ map reverse . transpose
    tiltW = Endo $ map tiltRow
      where
        tiltRow xs =
          let (a, b) = break (== '#') xs
              (os, ds) = partition (== 'O') a
              rest = case b of
                ('#' : b') -> '#' : tiltRow b'
                [] -> []
           in os ++ ds ++ rest
    
    load rows = sum $ map rowLoad rows
      where
        rowLoad = sum . map (length rows -) . elemIndices 'O'
    
    lookupCycle xs i =
      let (o, p) = findCycle 0 Map.empty xs
       in xs !! if i < o then i else (i - o) `rem` p + o
      where
        findCycle i seen (x : xs) =
          case seen Map.!? x of
            Just j -> (j, i - j)
            Nothing -> findCycle (i + 1) (Map.insert x i seen) xs
    
    main = do
      input <- lines <$> readFile "input14"
      print . load . appEndo (tiltW <> rotateL) $ input
      print $
        load $
          lookupCycle
            (iterate (appEndo $ stimes 4 (rotateR <> tiltW)) $ appEndo rotateL input)
            1000000000
    

    42.028 line-seconds

  • Rating problems and solutions

    We all know and love (!) the leaderboard, but how about a different method?

    One can solve a problem with a simple, naive method resulting in a short program and long runtime, or put in lots of explicit optimizations for more code and shorter runtime. (Or if you're really good, a short, fast program!)

    I propose the line-second.

    Take the number of lines in your program (eg, 42 lines) and the runtime (eg 0.096 seconds). Multiply these together to get a score of 4.032 line-seconds.

    A smaller score is a shorter, faster program.

    Similarly, (for a particular solver), a larger score is a "harder" problem.

    0
    Featured
    ๐ŸŽ - 2023 DAY 12 SOLUTIONS -๐ŸŽ
  • Haskell

    Phew! I struggled with this one. A lot of the code here is from my original approach, which cuts down the search space to plausible positions for each group. Unfortunately, that was still way too slow...

    It took an embarrassingly long time to try memoizing the search (which made precomputing valid points far less important). Anyway, here it is!

    Solution
    {-# LANGUAGE LambdaCase #-}
    
    import Control.Monad
    import Control.Monad.State
    import Data.List
    import Data.List.Split
    import Data.Map (Map)
    import qualified Data.Map as Map
    import Data.Maybe
    
    readInput :: String -> ([Maybe Bool], [Int])
    readInput s =
      let [a, b] = words s
       in ( map (\case '#' -> Just True; '.' -> Just False; '?' -> Nothing) a,
            map read $ splitOn "," b
          )
    
    arrangements :: ([Maybe Bool], [Int]) -> Int
    arrangements (pat, gs) = evalState (searchMemo 0 groups) Map.empty
      where
        len = length pat
        groups = zipWith startPoints gs $ zip minStarts maxStarts
          where
            minStarts = scanl (\a g -> a + g + 1) 0 $ init gs
            maxStarts = map (len -) $ scanr1 (\g a -> a + g + 1) gs
            startPoints g (a, b) =
              let ps = do
                    (i, pat') <- zip [a .. b] $ tails $ drop a pat
                    guard $
                      all (\(p, x) -> maybe True (== x) p) $
                        zip pat' $
                          replicate g True ++ [False]
                    return i
               in (g, ps)
        clearableFrom i =
          fmap snd $
            listToMaybe $
              takeWhile ((<= i) . fst) $
                dropWhile ((< i) . snd) clearableRegions
          where
            clearableRegions =
              let go i [] = []
                  go i pat =
                    let (a, a') = span (/= Just True) pat
                        (b, c) = span (== Just True) a'
                     in (i, i + length a - 1) : go (i + length a + length b) c
               in go 0 pat
        searchMemo :: Int -> [(Int, [Int])] -> State (Map (Int, Int) Int) Int
        searchMemo i gs = do
          let k = (i, length gs)
          cached <- gets (Map.!? k)
          case cached of
            Just x -> return x
            Nothing -> do
              x <- search i gs
              modify (Map.insert k x)
              return x
        search i gs | i >= len = return $ if null gs then 1 else 0
        search i [] = return $
          case clearableFrom i of
            Just b | b == len - 1 -> 1
            _ -> 0
        search i ((g, ps) : gs) = do
          let maxP = maybe i (1 +) $ clearableFrom i
              ps' = takeWhile (<= maxP) $ dropWhile (< i) ps
          sum <$> mapM (\p -> let i' = p + g + 1 in searchMemo i' gs) ps'
    
    expand (pat, gs) =
      (intercalate [Nothing] $ replicate 5 pat, concat $ replicate 5 gs)
    
    main = do
      input <- map readInput . lines <$> readFile "input12"
      print $ sum $ map arrangements input
      print $ sum $ map (arrangements . expand) input
    
  • ๐ŸŒŸ - 2023 DAY 6 SOLUTIONS -๐ŸŒŸ
  • Haskell

    This problem has a nice closed form solution, but brute force also works.

    (My keyboard broke during part two. Yet another day off the bottom of the leaderboard...)

    import Control.Monad
    import Data.Bifunctor
    import Data.List
    
    readInput :: String -> [(Int, Int)]
    readInput = map (\[t, d] -> (read t, read d)) . tail . transpose . map words . lines
    
    -- Quadratic formula
    wins :: (Int, Int) -> Int
    wins (t, d) =
      let c = fromIntegral t / 2 :: Double
          h = sqrt (fromIntegral $ t * t - 4 * d) / 2
       in ceiling (c + h) - floor (c - h) - 1
    
    main = do
      input <- readInput <$> readFile "input06"
      print $ product . map wins $ input
      print $ wins . join bimap (read . concatMap show) . unzip $ input
    
  • ๐ŸŽ - 2023 DAY 5 SOLUTIONS -๐ŸŽ
  • Torn between doing the problem backwards and implementing a more general case -- glad to know both approaches work out in the end!

  • ๐ŸŽ - 2023 DAY 5 SOLUTIONS -๐ŸŽ
  • Ah, nice! Dealing with each range individually makes things much simpler.

  • ๐ŸŽ - 2023 DAY 5 SOLUTIONS -๐ŸŽ
  • Haskell

    Not hugely proud of this one; part one would have been easier if I'd spend more time reading the question and not started on an overly-general solution, and I lost a lot of time on part two to a missing a +. More haste, less speed, eh?

    import Data.List
    import Data.List.Split
    
    readInput :: String -> ([Int], [(String, [(Int, Int, Int)])])
    readInput s =
      let (seedsChunk : mapChunks) = splitOn [""] $ lines s
          seeds = map read $ tail $ words $ head seedsChunk
          maps = map readMapChunk mapChunks
       in (seeds, maps)
      where
        readMapChunk (title : rows) =
          let name = head $ words title
              entries = map ((\[a, b, c] -> (a, b, c)) . map read . words) rows
           in (name, entries)
    
    part1 (seeds, maps) =
      let f = foldl1' (flip (.)) $ map (ref . snd) maps
       in minimum $ map f seeds
      where
        ref [] x = x
        ref ((a, b, c) : rest) x =
          let i = x - b
           in if i >= 0 && i < c
                then a + i
                else ref rest x
    
    mapRange :: [(Int, Int, Int)] -> (Int, Int) -> [(Int, Int)]
    mapRange entries (start, end) =
      go start $ sortOn (\(_, b, _) -> b) entries
      where
        go i [] = [(i, end)]
        go i es@((a, b, c) : rest)
          | i > end = []
          | b > end = go i []
          | b + c <= i = go i rest
          | i < b = (i, b - 1) : go b es
          | otherwise =
              let d = min (b + c - 1) end
               in (a + i - b, a + d - b) : go (d + 1) rest
    
    part2 (seeds, maps) =
      let seedRanges = map (\[a, b] -> (a, a + b - 1)) $ chunksOf 2 seeds
       in minimum $ map fst $ foldl' (flip mapRanges) seedRanges $ map snd maps
      where
        mapRanges m = concatMap (mapRange m)
    
    main = do
      input <- readInput <$> readFile "input05"
      print $ part1 input
      print $ part2 input
    
  • โ˜ƒ๏ธ - 2023 DAY 4 SOLUTIONS -โ˜ƒ๏ธ
  • Not familiar with Lean4 but it looks like the same approach. High five!

  • โ˜ƒ๏ธ - 2023 DAY 4 SOLUTIONS -โ˜ƒ๏ธ
  • Puzzles on the weekend are usually a bit more involved than weekdays. 23 is probably going to be a monster this year...

  • โ˜ƒ๏ธ - 2023 DAY 4 SOLUTIONS -โ˜ƒ๏ธ
  • Haskell

    11:39 -- I spent most of the time reading the scoring rules and (as usual) writing a parser...

    import Control.Monad
    import Data.Bifunctor
    import Data.List
    
    readCard :: String -> ([Int], [Int])
    readCard =
      join bimap (map read) . second tail . break (== "|") . words . tail . dropWhile (/= ':')
    
    countShared = length . uncurry intersect
    
    part1 = sum . map ((\n -> if n > 0 then 2 ^ (n - 1) else 0) . countShared)
    
    part2 = sum . foldr ((\n a -> 1 + sum (take n a) : a) . countShared) []
    
    main = do
      input <- map readCard . lines <$> readFile "input04"
      print $ part1 input
      print $ part2 input
    
  • ๐ŸฆŒ - 2023 DAY 2 SOLUTIONS -๐ŸฆŒ
  • Haskell

    A rather opaque parser, but much shorter than I could manage with Parsec.

    import Data.Bifunctor
    import Data.List.Split
    import Data.Map.Strict (Map)
    import qualified Data.Map.Strict as Map
    import Data.Tuple
    
    readGame :: String -> (Int, [Map String Int])
    readGame = bimap (read . drop 5) (map readPull . splitOn "; " . drop 2) . break (== ':')
      where
        readPull = Map.fromList . map (swap . bimap read tail . break (== ' ')) . splitOn ", "
    
    possibleWith limit = and . Map.intersectionWith (>=) limit
    
    main = do
      games <- map (fmap (Map.unionsWith max) . readGame) . lines <$> readFile "input02"
      let limit = Map.fromList [("red", 12), ("green", 13), ("blue", 14)]
      print $ sum $ map fst $ filter (possibleWith limit . snd) games
      print $ sum $ map (product . snd) games
    
  • Cryptic Crosswords @lemmy.sdf.org Leo Uino @lemmy.sdf.org
    Puzzle #6 - "Green"

    Tried a little too hard to go with a theme on this one, and some of the clues are a bit contrived. Feel free to suggest alternatives!

    0
    Cryptic Crosswords @lemmy.sdf.org Leo Uino @lemmy.sdf.org
    Puzzle #5

    Here's an old puzzle of mine to get started. One of the clues (at least!) is a little unfair, but the puzzle has been solved by others so it should be possible. Comments much appreciated, and more to come...

    0