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

Update to the blog yet agian

30 Nov, 2020 - 11:50:52

By

Matthew Deig

Tags:

Update

So I updated this blog again to use flask and python. I figure if I did this way it can open up a few more things I can do for the blog.

I looked into webmentions and it looked hard to do under hugo so I figure I would just update to use a server side blog.

Any weirdness

Let me know if anything weird happens on this site on my mastodon account or whatever works for you

Mix CD VOL 1: Spacey

15 Jun, 2020 - 00:00:00

By

Matthew Deig

Notes

I was trying to go for a space like mix. If any of these links are wrong just hit me up. I will fix it.

Tracks

Track Name Artist Duration Link
Paper Dolls 4mat 3:11 Bandcamp, YouTube
Makin It lpower 6:18 Bandcamp, Internet Archive
Orbital Sky Diving Gario 3:18 Bandcamp
Wake Up Aseul 4:20 Bandcamp, YouTube
Every Day is Night Garoad 3:40 Bandcamp, YouTube
LazerSwag Coyote Kisses 4:49 YouTube
Floating on Simple Waves TheGuitahHeroe 4:08 Bandcamp, YouTube
Triumph nervoustestpilot 4:56 YouTube
Untouchable Nimanslin 4:53 YouTube
Subaquos Ipsum Vrantheo 1:57 YouTube
Everything will be okay in the end don’tblinkoryou’lldie 5:42 Bandcamp, Youtube
Future, and It Doesn’t Work Starscream 5:25 Bandcamp, YouTube