;;;; 2015.01.01-first-attempt.lisp ;;;; Advent of Code 2015 ;;;; Day 1: Not Quite Lisp ;;;; Attempt 1 First attempt in Common Lisp ;; --------------------------------------------------------------------- ;;; Package Definition ;; --------------------------------------------------------------------- (defpackage :2015-01-first-attempt (:use #:common-lisp)) (in-package :2015-01-first-attempt) ;; --------------------------------------------------------------------- ;;; Definitions ;; --------------------------------------------------------------------- (defparameter *PUZZLE-INPUT-FILE* "~/proj/Learn/aoc/src/2015.01._PUZZLE-INPUT.txt" "File to be used as input to the program.") (defparameter *PUZZLE-INPUT-STRING* (uiop:read-file-string *PUZZLE-INPUT-FILE*) "String version of the text in `*PUZZLE-INPUT-FILE*'.") (defparameter *PUZZLE-INPUT* (loop for char across *PUZZLE-INPUT-STRING* collect char) "Puzzle input in lisp list form.") ;; --------------------------------------------------------------------- ;;; Part 1 ;; ------- ;; Santa is trying to deliver presents in a large apartment building, but ;; he can't find the right floor - the directions he got are a little ;; confusing. He starts on the ground floor (floor 0) and then follows ;; the instructions one character at a time. ;; ;; An opening parenthesis, (, means he should go up one floor, and a ;; closing parenthesis, ), means he should go down one floor. ;; ;; The apartment building is very tall, and the basement is very deep; ;; he will never find the top or bottom floors. ;; ;; For example: ;; ;; (()) and ()() both result in floor 0. ;; ((( and (()(()( both result in floor 3. ;; ))((((( also results in floor 3. ;; ()) and ))( both result in floor -1 (the first basement level). ;; ))) and )())()) both result in floor -3. ;; ;; To what floor do the instructions take Santa? ;; --------------------------------------------------------------------- (defun parse-input (INPUT) "Parse `INPUT' to determine where the instructions take Santa." (let ((FLOOR-NUMBER 0)) (+ FLOOR-NUMBER (cond ((eql (first INPUT) nil) 0) ((eql (first INPUT) #\() (+ 1 (parse-input (rest INPUT)))) ((eql (first INPUT) #\)) (- (parse-input (rest INPUT)) 1)))))) ;; Test for correct answer (assert (= 138 (parse-input *PUZZLE-INPUT*))) ;; I have elected to just throw this away and not do part 2 in this line of thinking. ;; Keeping it for posterity.