123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- (defparameter *movements-input* "./input.txt")
- (ql:quickload :cl-strings)
- (use-package :cl-strings)
- (defun read-movements ()
- "Read input file, returning a list of its depth readings."
- (let ((input (open *movements-input* :if-does-not-exist nil))
- (input-movements '()))
- (when input
- (loop with direction and distance
- for line = (read-line input nil)
- while line do
- (setf direction (car (cl-strings:split line)))
- (setf distance (parse-integer (cadr (cl-strings:split line))))
- (setq input-movements (cons (cons direction distance) input-movements)))
- (close input))
- (reverse input-movements)))
- (defun calculate-depths (input)
- "Calculate trajectory thru the deep."
- (defun iter-movements (movements horizontal depth)
- (cond ((eq movements '())
- (cons horizontal depth))
- ('true
- (let* ((movement (car movements))
- (direction (car movement))
- (distance (cdr movement)))
- (cond ((string= direction "up")
- (iter-movements
- (cdr movements)
- horizontal
- (- depth distance)))
- ((string= direction "down")
- (iter-movements
- (cdr movements)
- horizontal
- (+ depth distance)))
- ((string= direction "forward")
- (iter-movements
- (cdr movements)
- (+ horizontal distance)
- depth)))))))
- (iter-movements input 0 0))
- ;; get measurements!!
- (calculate-depths (read-movements)) ;; (1868 . 1090)
- (let* ((calculations
- (calculate-depths (read-movements)))
- (horizontal (car calculations))
- (depth (cdr calculations)))
- (* horizontal depth)) ;; results: 2036120
- (defun calculate-depths-with-aim (input)
- "Calculate trajectory thru the deep, understanding aim."
- (defun iter-movements (movements horizontal depth aim)
- (if ((eq movements '())
- (cons horizontal depth))
- (let* ((movement (car movements))
- (direction (car movement))
- (units (cdr movement)))
- (cond ((string= direction "up")
- (iter-movements
- (cdr movements)
- horizontal
- depth
- (- aim units)))
- ((string= direction "down")
- (iter-movements
- (cdr movements)
- horizontal
- depth
- (+ aim units)))
- ((string= direction "forward")
- (iter-movements
- (cdr movements)
- (+ horizontal units)
- (+ depth (* units aim))
- aim))))))
- (iter-movements input 0 0 0))
- ;; get measurements!!
- (calculate-depths-with-aim (read-movements)) ;; (1868 . 1078987)
- (let* ((calculations
- (calculate-depths-with-aim (read-movements)))
- (horizontal (car calculations))
- (depth (cdr calculations)))
- (* horizontal depth)) ;; results: 2015547716
|