git.mcksp
    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
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
(defun read-file (filename)
  (with-open-file (stream filename :direction :input)
    (read-line stream nil nil)))

(defparameter waypoints "roygbv")

(defun order (step)
  (position step waypoints)
)

(defun distance (from to)
  (let ((d (- (order to) (order from))))
    (if (= d 0) 6 (mod d 6))
  )
)

(defun walk (path)
  (let ((current #\r))
    (loop for step across path
        sum (distance current step)
        do (setf current step)
    )
  )
)

(defun optimal (path)
  (let ((counts (make-hash-table)))
    (loop for x across path do
      (incf (gethash x counts 0))
    )
    (let ((current #\r)
          (result 0))
      (loop for point across "rvbgyo" do
        (let ((count (gethash point counts 0)))
          (when (> count 0)
            (incf result (* (- count 1) 6))
            (incf result (distance current point))
            (setf current point)
          )
        )
      )
      result
    )
  )
)

(defun main ()
  (let ((path (read-file "input.txt")))
    (format t "~a~%" (walk path))
    (format t "~a~%" (optimal path))
  )
)

(main)