Day 4: Perilous Pier

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • Ismail@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    22 hours ago

    TXR

    (defun day04 ()
      (let ((lines (file-get-lines "04-input")))
        (flet ((pt1 ()
                 (sum-each ((line lines))
                   (shortest-cycle line)))
               (pt2 ()
                 (shortest-cycle [apply append lines])))
          (put-line `1: @(pt1)`)
          (put-line `2: @(pt2)`))))
    
    (defun unify (str1 str2)
      (and str1 str2 
           (map (lambda (c1 c2)
                  (cond ((eq c1 c2) c1)
                        ((eq c1 #\?) c2)
                        ((eq c2 #\?) c1)
                        (t (return-from unify nil))))
                str1 str2)))
          
    (defun shortest-cycle (str)
      (each ((stride 1..(succ (len str))))
        (when [reduce-left unify (tuples stride str)]
          (return stride))))