Quest 4: Teeth of the Wind

  • 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/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 2 points 11 months ago

In 1980 we didn't have tuple or set or vector types. We addressed elements of our cons-based data structures with nonsense incantations like caddar and we liked it that way.

(ql:quickload :cl-ppcre)

(defun read-inputs (filename)
  (mapcar #'parse-integer (uiop:read-file-lines filename)))

(defun main-1 (filename)
  (let ((gears (read-inputs filename)))
    (truncate (* 2025 (/ (first gears) (car (last gears)))))))

(defun main-2 (filename)
  (let ((gears (read-inputs filename)))
    (ceiling (* 10000000000000 (/ (car (last gears)) (first gears))))))

(defun read-inputs-3 (filename)
  (flet ((parse-line (line)
           (or (ppcre:register-groups-bind
                   (a b)
                   ("^([0-9]+)[|]([0-9]+)$" line)
                 (cons (parse-integer a) (parse-integer b)))
               (let ((a (parse-integer line)))
                 (cons a a)))))
    (mapcar #'parse-line (uiop:read-file-lines filename))))

(defun gear-ratio-3 (gears)
  (labels ((iter (gears ratio)
             (if (< (length gears) 2)
                 ratio
                 (let ((out-gear (cdar gears))
                       (in-gear (caadr gears)))
                   (iter (cdr gears) (* ratio (/ out-gear in-gear)))))))
    (iter gears 1)))

(defun main-3 (filename)
  (let ((gears (read-inputs-3 filename)))
    (truncate (* 100 (gear-ratio-3 gears)))))
  • source