From dcc3c287bb8cd463720e4bc401662217a45b6ca1 Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Sun, 2 Jun 2024 20:05:55 +0200 Subject: [PATCH] Solve part 2 of Not Quite Lisp --- 2015/days/01/lisp.lisp | 3 +++ 2015/days/01/lisp2.lisp | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 2015/days/01/lisp2.lisp diff --git a/2015/days/01/lisp.lisp b/2015/days/01/lisp.lisp index 9e7fe98..fa4889c 100644 --- a/2015/days/01/lisp.lisp +++ b/2015/days/01/lisp.lisp @@ -1,4 +1,7 @@ ;; Day 1: Not Quite Lisp +;; +;; Part 1: What is the floor Santa takes ? + (defparameter balance 0) diff --git a/2015/days/01/lisp2.lisp b/2015/days/01/lisp2.lisp new file mode 100644 index 0000000..938432d --- /dev/null +++ b/2015/days/01/lisp2.lisp @@ -0,0 +1,27 @@ +;; Day 1: Not Quite Lisp +;; +;; Part 2: Position of the character with balance -1 (basement) +;; +;; ref. +;; - read: https://lispcookbook.github.io/cl-cookbook/files.html + +(defparameter balance 0) +(defparameter position 0) + +(defun direction (char) (cond + ((char= char #\() + +1) + ((char= char #\)) + -1) + )) + +(with-open-file (in "./data/input") + (loop for char = (read-char in nil) + until (= balance -1) + do + (setf balance + (+ balance (direction char))) + (setf position + (+ position 1)))) + +(print position)