Quest 3: The Deepest Fit

  • 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

Link to participate: https://everybody.codes/

Also, don’t wait for me to make these posts, feel free to post yourself :)

  • ystael@beehaw.org
    link
    fedilink
    arrow-up
    2
    ·
    10 days ago

    Common Lisp doesn’t have a built in set datatype, so you have to do uniqueness checking sort of by hand unless you want to pull in an external package.

    (ql:quickload :str)
    
    (defun read-inputs (filename)
      (let ((input-lines (uiop:read-file-lines filename)))
        (mapcar #'parse-integer (str:split "," (car input-lines)))))
    
    (defun add-distinct (xs)
      (loop for x in (remove-duplicates (sort (copy-seq xs) #'>))
            sum x))
    
    (defun main-1 (filename)
      (add-distinct (read-inputs filename)))
    
    (defun main-2 (filename)
      (let* ((sizes (read-inputs filename))
             (first-20 (subseq (remove-duplicates (sort (copy-seq sizes) #'<)) 0 20)))
        (loop for x in first-20
              sum x)))
    
    (defun count-max-copies (xs)
      (labels ((iter (xs cur-x cur-count max-count)
                 (cond ((null xs)
                        max-count)
                       ((equal (car xs) cur-x)
                        (iter (cdr xs) cur-x (1+ cur-count) (max max-count (1+ cur-count))))
                       (t
                        (iter (cdr xs) (car xs) 1 max-count)))))
        (iter (cdr xs) (car xs) 1 1)))
    
    (defun main-3 (filename)
      (count-max-copies (sort (read-inputs filename) #'<)))