;;; https://gitlab.com/Syndamia/senzill (require :senzill) (use-package :senzill.math) (use-package :senzill.collections) (use-package :senzill.pair) (use-package :senzill.io) (ask-for-stream (prog-input) (let ((rhead '(0 . 0)) (rtail '(0 . 0)) (visited '((0 . 0))) (movec '())) (flet ((norm>sqrt2p (c) ; If c's eucledian length is less than sqrt(2). In other words, if c is outside the 3x3 square. "Optimized version of (> (norm p) (sqrt 2))" (or (> (abs (car c)) 1) (> (abs (cdr c)) 1))) (dir-to-pair (direction) (cond ((char= direction #\R) '( 1 . 0)) ((char= direction #\L) '(-1 . 0)) ((char= direction #\U) '( 0 . 1)) ((char= direction #\D) '( 0 . -1))))) (doread-lines (inpt :read-line-options (prog-input NIL)) (loop for i from 1 to (parse-integer inpt :start 2) do (pair+= rhead (dir-to-pair (char inpt 0))) (setf movec (pair- rhead rtail)) if (norm>sqrt2p movec) do ;; dist-resize so we don't put the knot on top of the head, but behind it (pair+= rtail (pair-round (dist-resize-by movec -1))) (if (not (member-if (lambda (x) (pair= x rtail)) visited)) (push rtail visited)))) (print (length visited)))))