From 03e14731fb51fafbf7931231674ecf7a621fcbf4 Mon Sep 17 00:00:00 2001 From: Tristan Williams Date: Sat, 11 Jan 2025 14:07:50 -0500 Subject: Complete AoC 2015 Day 1 --- src/2015.01.01-first-attempt.lisp | 69 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/2015.01.01-first-attempt.lisp (limited to 'src/2015.01.01-first-attempt.lisp') diff --git a/src/2015.01.01-first-attempt.lisp b/src/2015.01.01-first-attempt.lisp new file mode 100644 index 0000000..a3fb2c9 --- /dev/null +++ b/src/2015.01.01-first-attempt.lisp @@ -0,0 +1,69 @@ +;;;; 2015.01.01-first-attempt.lisp +;;;; Advent of Code 2015 Day 1, first attempt in 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. -- cgit v1.2.3