Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cellularmitosis
Created December 6, 2021 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cellularmitosis/1493b0d1727d57d3571e2fce11aa1e20 to your computer and use it in GitHub Desktop.
Save cellularmitosis/1493b0d1727d57d3571e2fce11aa1e20 to your computer and use it in GitHub Desktop.
Advent of Code problem 1.1 (Common Lisp)
; I'm a new lisper, this code is probably gross :)
; misc resources:
; https://hyperpolyglot.org/lisp
; http://www.lispworks.com/documentation/HyperSpec/Body/f_parse_.htm
; https://gigamonkeys.com/book/files-and-file-io.html
; https://lispcookbook.github.io/cl-cookbook/iteration.html
; http://www.ai.sri.com/pkarp/loop.html
; read a file as a list of lines.
; thanks to https://stackoverflow.com/a/3814098
(defun read-lines (filename)
(with-open-file (stream filename)
(loop for line = (read-line stream nil)
while line
collect line)))
; convert a list of strings into a list of integers.
(defun as-ints (strs)
(mapcar (function parse-integer) strs))
; (princ (as-ints (read-lines "problem1.input.txt")))
(defparameter *previous* nil)
(defparameter *current* nil)
(defparameter *depth-increase-count* 0)
(loop for depth in (as-ints (read-lines "problem1.input.txt"))
do (cond ((not *current*) (setq *current* depth))
(t (progn (setq *previous* *current*)
(setq *current* depth)
(if (> *current* *previous*) (setq *depth-increase-count* (+ 1 *depth-increase-count*)))))))
(print *depth-increase-count*)
@cellularmitosis
Copy link
Author

Here's a follow-up version which doesn't use global variables:

; misc resources:
; https://hyperpolyglot.org/lisp
; http://www.lispworks.com/documentation/HyperSpec/Body/f_parse_.htm
; https://gigamonkeys.com/book/files-and-file-io.html
; https://lispcookbook.github.io/cl-cookbook/iteration.html
; http://www.ai.sri.com/pkarp/loop.html

; read a file as a list of lines.
; thanks to https://stackoverflow.com/a/3814098
(defun read-lines (filename)
    (with-open-file (stream filename)
        (loop for line = (read-line stream nil)
            while line
            collect line)))

; convert a list of strings into a list of integers.
(defun as-ints (strs)
    (mapcar (function parse-integer) strs))

; return the number of increases in the list of depths.
(defun count-depth-increases (depths)
    (let ((current nil) (previous nil) (total 0))
        (loop
            for depth in depths
            do (if (not current) (setq current depth))
                   (progn (setq previous current)
                          (setq current depth)
                          (if (> current previous) (incf total))))
        total))

; main:
(let* ((lines (read-lines "problem1.input.txt"))
       (depths (as-ints lines))
       (num-depth-increases (count-depth-increases depths)))
    (print num-depth-increases))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment