jordan dashel 3 years ago
parent
commit
51f559fb6c
2 changed files with 1090 additions and 0 deletions
  1. 1000 0
      2/input.txt
  2. 90 0
      2/submarine.lisp

+ 1000 - 0
2/input.txt

@@ -0,0 +1,1000 @@
+forward 7
+forward 9
+forward 9
+down 3
+down 8
+down 3
+forward 6
+down 7
+up 3
+forward 3
+up 3
+down 3
+down 9
+down 6
+forward 8
+down 3
+down 9
+forward 4
+forward 3
+down 1
+forward 2
+down 5
+forward 9
+forward 6
+up 1
+forward 8
+down 9
+forward 8
+down 7
+down 9
+down 4
+down 2
+forward 2
+down 6
+up 8
+forward 3
+forward 7
+down 2
+forward 4
+down 3
+down 1
+down 8
+forward 5
+forward 6
+down 3
+forward 1
+up 1
+down 8
+forward 5
+forward 2
+forward 9
+up 4
+down 1
+down 3
+up 5
+up 5
+forward 7
+up 1
+forward 8
+forward 4
+forward 3
+down 9
+forward 2
+forward 5
+down 5
+up 7
+forward 4
+up 5
+down 5
+down 4
+down 9
+forward 1
+up 3
+forward 6
+down 3
+forward 9
+down 1
+up 5
+down 7
+forward 3
+forward 3
+up 9
+down 2
+down 4
+forward 1
+forward 2
+down 4
+down 4
+up 5
+forward 9
+down 3
+down 1
+down 8
+down 2
+down 8
+forward 9
+forward 5
+down 2
+forward 8
+up 9
+down 8
+down 5
+down 6
+down 2
+down 4
+up 2
+forward 4
+down 9
+forward 4
+down 6
+down 9
+forward 9
+down 3
+down 1
+up 2
+down 6
+forward 2
+down 4
+down 6
+forward 5
+up 2
+up 8
+down 2
+up 9
+forward 6
+down 8
+down 7
+forward 3
+forward 5
+forward 4
+down 5
+forward 2
+up 1
+up 3
+up 7
+forward 2
+forward 2
+up 6
+down 3
+down 7
+down 8
+forward 5
+up 5
+down 9
+forward 7
+down 2
+down 6
+forward 8
+down 1
+down 7
+down 2
+forward 2
+forward 4
+down 5
+up 5
+down 6
+down 5
+down 8
+forward 5
+forward 4
+down 3
+up 7
+up 4
+forward 7
+down 9
+down 3
+down 7
+forward 6
+up 4
+down 2
+down 9
+up 1
+down 2
+up 7
+down 7
+down 1
+down 5
+forward 9
+down 5
+down 8
+down 4
+down 5
+forward 3
+down 5
+forward 5
+down 6
+forward 6
+up 7
+down 7
+down 3
+down 8
+down 8
+up 5
+down 3
+up 4
+down 1
+up 4
+up 2
+up 2
+forward 6
+forward 3
+forward 4
+down 9
+up 2
+forward 2
+down 2
+down 6
+up 2
+forward 1
+up 4
+forward 5
+up 5
+forward 3
+forward 7
+down 2
+up 6
+forward 1
+up 6
+down 8
+forward 1
+forward 7
+up 3
+up 5
+forward 4
+down 1
+forward 7
+up 7
+forward 5
+forward 6
+forward 6
+forward 2
+down 6
+down 1
+forward 4
+forward 2
+forward 2
+up 2
+forward 1
+down 2
+down 8
+forward 5
+forward 3
+up 1
+forward 5
+down 8
+down 6
+down 1
+up 2
+down 1
+down 3
+forward 3
+up 9
+forward 5
+down 6
+up 8
+down 9
+up 4
+down 8
+forward 2
+forward 9
+forward 6
+forward 2
+up 5
+down 5
+down 6
+forward 2
+forward 3
+forward 5
+forward 7
+down 8
+forward 1
+forward 1
+forward 4
+forward 5
+down 4
+forward 6
+forward 4
+forward 6
+down 5
+down 8
+down 7
+up 9
+down 8
+forward 2
+down 9
+forward 3
+down 4
+up 1
+down 6
+forward 4
+down 9
+down 5
+down 3
+up 6
+down 1
+down 3
+forward 2
+down 7
+down 9
+forward 6
+forward 1
+forward 8
+down 6
+down 2
+down 2
+forward 7
+up 8
+forward 5
+forward 7
+forward 7
+forward 3
+up 3
+forward 3
+up 4
+down 5
+forward 1
+forward 7
+forward 7
+down 9
+up 4
+forward 1
+down 9
+forward 3
+forward 8
+down 3
+forward 6
+down 7
+down 2
+forward 1
+down 4
+down 6
+down 2
+forward 7
+forward 7
+down 9
+forward 7
+down 8
+forward 7
+down 5
+down 7
+forward 1
+forward 7
+up 6
+down 5
+forward 9
+down 2
+forward 7
+up 4
+down 1
+down 7
+forward 4
+down 2
+down 4
+forward 4
+down 8
+forward 3
+forward 2
+down 3
+down 4
+forward 2
+forward 6
+down 9
+down 3
+forward 9
+up 4
+up 1
+forward 7
+up 3
+up 8
+down 2
+up 2
+up 6
+forward 8
+down 3
+up 4
+up 6
+down 8
+forward 5
+down 1
+up 2
+up 5
+forward 7
+down 6
+up 4
+forward 1
+up 9
+forward 8
+down 3
+up 8
+down 3
+down 5
+up 6
+up 8
+down 2
+up 4
+down 1
+down 2
+forward 4
+up 9
+up 1
+up 2
+forward 4
+forward 9
+up 8
+forward 8
+down 8
+up 9
+forward 3
+forward 2
+down 7
+up 1
+up 8
+up 1
+up 6
+up 9
+forward 6
+forward 8
+forward 8
+forward 6
+down 2
+down 4
+forward 8
+up 3
+forward 1
+down 3
+down 1
+down 9
+up 9
+down 4
+forward 7
+down 3
+forward 1
+down 6
+forward 7
+forward 3
+forward 2
+down 5
+forward 6
+up 8
+forward 6
+forward 8
+down 1
+up 3
+down 1
+down 7
+up 2
+down 8
+forward 7
+down 1
+forward 5
+forward 5
+up 9
+down 1
+forward 6
+up 1
+forward 1
+down 8
+down 5
+forward 7
+forward 5
+forward 3
+down 2
+down 4
+forward 3
+forward 2
+forward 3
+down 1
+down 6
+down 8
+down 5
+forward 6
+forward 5
+down 8
+forward 2
+forward 6
+forward 1
+down 3
+down 7
+down 1
+down 1
+down 6
+down 5
+forward 1
+up 9
+down 4
+down 8
+forward 4
+down 3
+down 6
+down 9
+up 4
+forward 6
+down 1
+down 3
+up 5
+up 7
+forward 6
+up 6
+down 6
+down 2
+up 1
+forward 1
+down 3
+forward 2
+down 6
+down 5
+down 1
+down 5
+up 2
+down 3
+up 1
+forward 7
+down 3
+forward 5
+down 4
+up 1
+down 7
+forward 3
+up 9
+down 2
+up 5
+forward 7
+up 8
+forward 8
+forward 8
+up 8
+forward 8
+forward 1
+forward 2
+down 8
+forward 6
+down 3
+down 9
+forward 9
+forward 4
+down 6
+down 4
+forward 3
+up 2
+up 7
+down 9
+down 2
+forward 8
+down 2
+down 3
+down 7
+forward 9
+down 4
+up 3
+down 4
+down 5
+forward 9
+down 9
+forward 4
+forward 3
+down 6
+forward 4
+down 5
+down 2
+forward 1
+down 4
+forward 2
+up 3
+up 3
+forward 4
+down 4
+up 2
+up 8
+forward 8
+down 5
+down 8
+down 7
+down 7
+forward 6
+forward 5
+up 4
+down 6
+down 9
+forward 1
+down 3
+forward 8
+down 4
+forward 3
+down 7
+forward 3
+forward 3
+forward 1
+forward 5
+down 7
+forward 4
+up 5
+down 9
+down 3
+down 6
+down 6
+forward 1
+down 2
+forward 8
+forward 5
+forward 9
+up 2
+forward 5
+down 1
+up 2
+forward 7
+down 9
+down 2
+up 9
+down 6
+forward 4
+down 8
+forward 4
+down 7
+down 6
+up 7
+up 3
+forward 6
+forward 7
+down 4
+down 6
+up 1
+forward 6
+down 6
+forward 5
+forward 9
+forward 5
+forward 7
+down 3
+down 9
+forward 7
+forward 6
+down 2
+down 9
+down 8
+down 5
+forward 3
+up 5
+down 4
+forward 5
+down 8
+forward 8
+up 3
+down 5
+up 8
+down 5
+down 1
+down 3
+down 1
+down 8
+up 9
+forward 1
+forward 5
+forward 9
+forward 9
+down 6
+forward 3
+up 6
+up 2
+down 6
+forward 4
+down 7
+up 1
+up 3
+down 3
+up 3
+up 1
+down 9
+down 2
+up 6
+down 6
+forward 8
+forward 3
+forward 1
+up 9
+down 2
+forward 6
+down 1
+forward 5
+up 8
+down 2
+forward 7
+forward 2
+down 1
+forward 6
+up 7
+down 7
+forward 3
+down 8
+down 6
+forward 3
+down 9
+down 7
+forward 5
+forward 5
+down 1
+forward 3
+up 8
+forward 8
+down 3
+up 4
+up 6
+up 5
+up 6
+down 8
+up 9
+down 6
+up 5
+down 6
+forward 4
+down 7
+forward 5
+forward 2
+down 1
+down 5
+forward 4
+forward 4
+down 9
+forward 6
+down 4
+forward 1
+forward 3
+down 5
+forward 2
+up 3
+forward 6
+down 2
+up 5
+down 6
+down 6
+forward 3
+up 5
+forward 4
+forward 3
+forward 6
+forward 5
+forward 7
+down 5
+down 8
+up 6
+up 3
+down 1
+forward 6
+down 4
+forward 7
+up 3
+forward 9
+down 3
+forward 2
+forward 8
+down 9
+down 2
+up 8
+down 3
+down 6
+forward 8
+forward 6
+up 5
+forward 9
+forward 7
+down 9
+forward 5
+down 1
+up 5
+down 4
+up 2
+forward 1
+up 9
+forward 1
+forward 2
+down 1
+forward 5
+forward 8
+down 8
+up 3
+down 4
+forward 6
+down 9
+down 9
+forward 7
+forward 3
+down 3
+down 7
+forward 8
+forward 1
+forward 6
+down 8
+up 2
+up 6
+forward 2
+down 6
+up 3
+down 5
+forward 9
+down 6
+down 9
+down 8
+down 5
+forward 3
+forward 3
+down 1
+forward 7
+forward 5
+down 3
+down 1
+down 9
+forward 4
+up 9
+up 8
+down 2
+down 1
+forward 6
+forward 5
+up 8
+up 4
+down 7
+forward 9
+forward 9
+up 4
+forward 3
+down 6
+forward 1
+down 2
+forward 2
+down 6
+forward 4
+down 2
+forward 9
+down 3
+down 4
+forward 9
+forward 4
+forward 7
+forward 8
+down 7
+up 5
+forward 3
+forward 1
+down 3
+forward 2
+forward 5
+forward 5
+down 5
+down 5
+down 6
+forward 3
+up 4
+forward 6
+down 1
+down 3
+down 5
+down 6
+up 1
+forward 3
+forward 2
+up 9
+forward 6
+up 1
+down 9
+up 3
+down 4
+forward 1
+down 4
+forward 3
+forward 5
+down 2
+forward 5
+down 5
+down 9
+down 8
+forward 5
+forward 7
+down 1
+forward 2
+up 5
+up 9
+forward 4
+up 7
+down 9
+forward 9
+up 9
+down 5
+up 8
+forward 1
+forward 8
+up 9
+down 1
+down 3
+down 6
+forward 8
+forward 9
+down 7
+forward 8
+down 9
+forward 7
+up 6
+forward 7
+forward 3
+forward 5
+down 4
+forward 3
+up 8
+up 3
+down 7
+up 3
+forward 6
+down 7
+forward 5
+down 5
+forward 8
+down 4
+up 1
+up 1
+forward 9
+forward 8
+up 3
+down 5
+forward 8
+forward 6
+forward 9
+forward 5
+forward 3
+down 5
+forward 4
+down 6
+forward 9
+up 6
+down 3
+down 8
+down 5
+down 8
+down 4
+down 5
+down 2
+down 4
+down 8
+down 2
+down 7
+down 9
+down 5
+up 3
+down 1
+forward 6
+forward 4
+forward 1
+forward 6
+forward 4
+down 1
+forward 3
+forward 1
+forward 1
+down 5
+down 4
+up 6
+forward 2
+up 6
+down 8
+forward 1
+up 7
+down 4
+up 2
+down 3
+forward 6
+forward 2
+up 5
+forward 7
+down 8
+forward 6
+up 6
+down 9
+down 6
+down 5
+down 8
+down 5
+down 8
+down 2
+down 7
+up 6
+up 9
+down 3
+down 5
+forward 3
+up 7
+down 7
+up 8
+forward 4
+forward 2
+down 1
+up 1
+up 6
+up 2
+down 1
+down 6
+down 1
+forward 4
+down 6
+forward 9
+forward 2
+forward 1
+forward 7
+forward 5

+ 90 - 0
2/submarine.lisp

@@ -0,0 +1,90 @@
+(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