Advent of Code Day 3

06 Dec, 2020 - 14:12:31

By

Matthew Deig

Current Status

As of this post I only did Day 1, 2, and 3. The current day for this is Day 6. I been doing this at my own pace. I feel like I’m getting better with lisp with these puzzles, and I had to look at other people’s solutions to fully understand what the problem was asking for.

Day 3

Day 3 has you finding out how many trees you hit on a toboggan stuck on one path

here is the code

(defparameter *toboggan-vel* '((1 1) (3 1) (5 1) (7 1) (1 2)))

(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)))


(defun is-tree? (pos-char)
  (if (char= pos-char #\#) t))

(defun toboggan-y-vel (lines vel)
  (cond ((eq lines nil) '())
        ((= vel 0) lines)
        (t (toboggan-y-vel (cdr lines) (- vel 1)))))


(defun check-toboggan-path (lines pos)
  (cond ((eq lines nil) '())
        ((not (is-tree? (char (subseq (car lines) pos (+ pos 1)) 0))) (cons #\O (check-toboggan-path (cdr lines) (mod (+ pos *toboggan-x-vel*) (length (car lines))))))
        ((is-tree? (char (subseq (car lines) pos (+ pos 1)) 0)) (cons #\X (check-toboggan-path (cdr lines) (mod (+ pos *toboggan-x-vel*) (length (car lines))))))
        (t '())))

(defun check-toboggan-path-2 (lines pos y-vel x-vel)
  (cond ((eq lines nil) '())
        ((not (is-tree? (char (subseq (car lines) pos (+ pos 1)) 0)))
         (cons #\O
               (check-toboggan-path-2 (toboggan-y-vel lines y-vel) (mod (+ pos x-vel) (length (car lines))) y-vel x-vel)))
        ((is-tree? (char (subseq (car lines) pos (+ pos 1)) 0))
         (cons #\X (check-toboggan-path-2 (toboggan-y-vel lines y-vel) (mod (+ pos x-vel) (length (car lines))) y-vel x-vel)))
        (t '())))

(defun count-tree-hits (path)
  (loop for item in path
        when (char= item #\X)
          sum 1 into total
        finally (return total)))

(defun mul-tree-hit-counts (tree-hit-list)
  (cond ((eq tree-hit-list nil) 1)
        (t (* (car tree-hit-list) (mul-tree-hit-counts (cdr tree-hit-list))))))

(defun loop-vel ()
  (loop for item in *toboggan-vel*
        collect (count-tree-hits (check-toboggan-path-2 (read-file-in "puz3.in") 0 (cadr item) (car item)))))

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)))