aboutsummaryrefslogtreecommitdiff
path: root/2022/Day09/part-one.cl
blob: f76fa8d9367c6edc1c949504e5dcc57c59406835 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
;;; 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)))))