Created
December 6, 2021 18:32
-
-
Save cellularmitosis/1493b0d1727d57d3571e2fce11aa1e20 to your computer and use it in GitHub Desktop.
Advent of Code problem 1.1 (Common Lisp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's a follow-up version which doesn't use global variables: