I looked at Advent of Code and foolishly decided to solve the first riddle. Perhaps we’ll learn something about Elisp coding style? Feel free to leave comments regarding the code.
You must follow instructions: R or L means turn left or right, followed by a number of steps to take. What’s the final distance in steps? Example: “R5, L5, R5, R3 leaves you 12 blocks away.”
(let ((instructions (mapcar (lambda (s) (cons (aref s 0) (string-to-number (substring s 1)))) (split-string "R5, L5, R5, R3"))) (direction 0) (x 0) (y 0)) (dolist (instruction instructions) (destructuring-bind (turn . steps) instruction (setq direction (mod (funcall (if (= turn ?R) '1+ '1-) direction) 4)) (case direction (0 (setq y (+ y steps))) (1 (setq x (+ x steps))) (2 (setq y (- y steps))) (3 (setq x (- x steps)))))) (+ (abs x) (abs y)))
Sadly, for the second star, my solution fails... The question is now: which location was visited twice? The example provided says “if your instructions are R8, R4, R4, R8, the first location you visit twice is 4 blocks away, due East.” The code works for the example given but fails for the data I was provided with in the test.
(let ((instructions (mapcar (lambda (s) (cons (aref s 0) (string-to-number (substring s 1)))) (split-string "R8, R4, R4, R8"))) (direction 0) (x 0) (y 0) (last-x 0) (last-y 0) (seen nil) (final-x nil) (final-y nil)) (or (catch 'twice (dolist (instruction instructions) (destructuring-bind (turn . steps) instruction (setq direction (mod (funcall (if (= turn ?R) '1+ '1-) direction) 4)) (case direction (0 (setq y (+ y steps))) (1 (setq x (+ x steps))) (2 (setq y (- y steps))) (3 (setq x (- x steps))))) (dolist (line (cdr seen)) ; skip the last element (destructuring-bind ((x1 . y1) (x2 . y2)) line (when (or (and (= x last-x) ; last move was vertical (= y1 y2) ; looking at a horizontal (>= x x1) (<= x x2) (or (and (<= y y1) (>= last-y y1)) (and (>= y y1) (<= last-y y1))) (setq final-x x final-y y1)) (and (= y last-y) ; last move was horizontal (= x1 x2) ; looking at a vertical (>= y y1) (<= y y2) (or (and (<= x x1) (>= last-x x1)) (and (>= x x1) (<= last-x x1))) (setq final-x x1 final-y y))) ;; (error "Moving from %S to %S crosses the line from %S to %S in %S" ;; (cons last-x last-y) (cons x y) ;; (cons x1 y1) (cons x2 y2) ;; seen) (throw 'twice (+ (abs final-x) (abs final-y)))))) (push (list (cons last-x last-y) (cons x y)) seen) (setq last-x x last-y y))) (+ (abs x) (abs y))))
Well, I decided to brute force it and remember every point I moved through because my “crossing lines” solution was obviously off by 3. Perhaps I just reported the wrong result, who knows.
(let ((instructions (mapcar (lambda (s) (cons (aref s 0) (string-to-number (substring s 1)))) (split-string "R8, R4, R4, R8"))) (direction 0) (x 0) (y 0) (last-x 0) (last-y 0) (seen '((0 . 0)))) (or (catch 'twice (dolist (instruction instructions) (destructuring-bind (turn . steps) instruction (setq direction (mod (funcall (if (= turn ?R) '1+ '1-) direction) 4)) (case direction (0 (setq y (+ y steps))) (1 (setq x (+ x steps))) (2 (setq y (- y steps))) (3 (setq x (- x steps))))) (while (not (and (= last-x x) (= last-y y))) (if (not (= last-x x)) (setq last-x (funcall (if (< last-x x) '1+ '1-) last-x)) (setq last-y (funcall (if (< last-y y) '1+ '1-) last-y))) (let ((last (cons last-x last-y))) (if (member last seen) (error "Repetition seen: %S in %S" last seen) (push last seen)))))) (error "No repetitions in %S" seen)))
#Emacs #Advent of Code #Programming