Advent of Code Day 1 and Day 2

04 Dec, 2020 - 14:52:35

By

Matthew Deig

Advent of Code

I figure I would give it a try this year. Just some small problems to code out. I the language I chose to use was Common Lisp using sbcl. As of this post I just did Day 1 and Day 2.

I’m using common lisp because I though it be fun to use and I still want to get better at using it. I still had to search around to find out how to use some of the functions in lisp still, but I feel I like I’m getting there.

Day 1

Day 1 was to find what 2 transactions summed equal to 2020 and give the product of those numbers

(defparameter *transactions* nil)


(defun read-file-in (filename)
  (with-open-file ( in filename
                       :if-does-not-exist nil
                       :external-format '(:utf-8 :replacement "?"))
    (loop for line = ( read-line in nil nil )
          while line collect line)))

;;part one
(defun sum-2020? (transaction)
  (loop for item in *transactions*
        when (= (+ transaction item) 2020)
          return (* transaction item)))
;;part two
(defun sum-three-2020? (transaction)
  (loop for item2 in *transactions*
        collect (loop for item3 in *transactions*
                      when (= (+ transaction item2 item3) 2020)
                        return (* transaction item2 item3))))

Day 2

Day 2’s was validating password rules from a database corruption. For this one I borrows a snippet from common lisp cookbook and rolled my own boolean-xor

(defparameter *password-policy* nil)

(defun read-file-in (filename)
  (with-open-file ( in filename
                       :if-does-not-exist nil
                       :external-format '(:utf-8 :replacement "?"))
    (loop for line = ( read-line in nil nil )
          while line collect line)))

;;took from http://cl-cookbook.sourceforge.net/strings.html
(defun split-by-one-space (string)
    "Returns a list of substrings of string divided by ONE space each.
     Note: Two consecutive spaces will be seen as if there were an empty string between them."
    (loop for i = 0 then (1+ j)
          as j = (position #\Space string :start i)
          collect (subseq string i j)
          while j))

(defun split-by-one-hypen (string)
  "returns a list of substrings of string divided by ONE hypen"
  (loop for i = 0 then (1+ j)
          as j = (position #\- string :start i)
          collect (subseq string i j)
        while j))

(defun process-file-line (line)
  (list (car (split-by-one-hypen (car (split-by-one-space line))))
        (car (cdr (split-by-one-hypen (car (split-by-one-space line)))))
        (subseq (car (cdr (split-by-one-space line))) 0 1)
        (car (cddr (split-by-one-space line)))))

(defun boolean-xor ( test1 test2 )
  (cond ((and test1 test2) nil)
        ((or test1 test2) t)
        (t nil)))


(defun valid-password? (password)
  (if (<= (parse-integer (car password)) (count (char (caddr password) 0) (cadddr password) :test #'equal)
          (parse-integer (cadr password)))
      t))

(defun valid-password2? (password)
  (if (boolean-xor
       (char= (char (caddr password) 0) (char (cadddr password) (- (parse-integer (car password)) 1)))
       (char= (char (caddr password) 0) (char (cadddr password) (- (parse-integer (cadr password)) 1))))
      t))

(defun count-valid-passwords (pass-list)
  (loop for item in pass-list
        when item
          sum 1 into total
        finally (princ total)))