Day 1: Secret Entrance

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

  • ystael@beehaw.org
    link
    fedilink
    arrow-up
    3
    ·
    5 days ago

    I agree with strlcpy – computing was better in the 1980s. Let’s try the version of the 1980s where Lisp Machines were going to power the AI future. It can’t be any worse than the AI future we’ve got right now.

    (This is SBCL, not a Lisp Machine emulator, because I’m not that hardcore.)

    (defun parse-line (line)
      (let ((sign (if (eql (char line 0) #\R) 1 -1))
            (number (parse-integer (subseq line 1))))
        (* sign number)))
    
    (defun read-inputs (filename)
      (let ((input-lines (uiop:read-file-lines filename)))
        (mapcar #'parse-line input-lines)))
    
    (defun rotate (pos rotation)
      (mod (+ pos rotation) 100))
    
    (defun main-1 (filename)
      (let ((rotations (read-inputs filename))
            (pos 50))
        (loop for rotation in rotations
              do (setf pos (rotate pos rotation))
              sum (if (= pos 0) 1 0))))
    
    (defun zero-crossings (pos rotation)
      (if (> rotation 0)
          (floor (+ rotation pos) 100)
          (let ((neg-pos (if (zerop pos) pos (- pos 100))))
            (- (ceiling (+ rotation neg-pos) 100)))))
    
    (defun main-2 (filename)
      (let ((rotations (read-inputs filename))
            (pos 50))
        (loop for rotation in rotations
              sum (zero-crossings pos rotation) into crossings
              do (setf pos (rotate pos rotation))
              finally (return crossings))))