summaryrefslogtreecommitdiffstats
path: root/src/02.lisp
blob: 970e36360130292f228f5e72db9095bcce997afc (plain)
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
(load "~/quicklisp/setup.lisp")
(ql:quickload 'uiop :silent t)
(ql:quickload 'cl-ppcre :silent t)

(defun get-file-lines (name)
  (uiop:read-file-lines name))

;; Turn the input file into whatever form you will use for both parts
;; (get-file-lines) and (get-file-string) will be useful
(defun parse-input (input-file)
  (let ((input (mapcar (lambda (s) (cl-ppcre:split " " s)) (get-file-lines input-file))))
    (mapcar (lambda (x) (cons (intern (string-upcase (CAR x))) (parse-integer (CADR x)))) input)))

(defun part-a (parsed-input)
  (let ((h 0) (v 0))
    (dolist (i parsed-input)
      (case (car i)
        (forward (incf h (cdr i)))
        (up (decf v (cdr i)))
        (down (incf v (cdr i)))))
    (* h v)))

(defun part-b (parsed-input)
  (let ((h 0) (v 0) (a 0))
    (dolist (i parsed-input)
      (case (car i)
        (forward (progn (incf v (* (cdr i) a)) (incf h (cdr i))))
        (up (decf a (cdr i)))
        (down (incf a (cdr i)))))
    (* h v)))

(time (format t "part 2: ~a~%" (part-a (parse-input "../inputs/02.txt"))))
(time (format t "part 2: ~a~%" (part-b (parse-input "../inputs/02.txt"))))