aboutsummaryrefslogtreecommitdiff
path: root/src/2015.01.01-first-attempt.lisp
diff options
context:
space:
mode:
authorTristan Williams <tgwil@tgwil.net>2025-01-11 14:07:50 -0500
committerTristan Williams <tgwil@tgwil.net>2025-01-11 14:07:50 -0500
commit03e14731fb51fafbf7931231674ecf7a621fcbf4 (patch)
tree969f82a20353cd279354b97b26fde756651780b3 /src/2015.01.01-first-attempt.lisp
downloadaoc-03e14731fb51fafbf7931231674ecf7a621fcbf4.tar.gz
aoc-03e14731fb51fafbf7931231674ecf7a621fcbf4.tar.bz2
aoc-03e14731fb51fafbf7931231674ecf7a621fcbf4.zip
Complete AoC 2015 Day 1
Diffstat (limited to 'src/2015.01.01-first-attempt.lisp')
-rw-r--r--src/2015.01.01-first-attempt.lisp69
1 files changed, 69 insertions, 0 deletions
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 @@
1;;;; 2015.01.01-first-attempt.lisp
2;;;; Advent of Code 2015 Day 1, first attempt in Lisp
3
4;; ---------------------------------------------------------------------
5;;; Package Definition
6;; ---------------------------------------------------------------------
7
8(defpackage :2015-01-first-attempt
9 (:use #:common-lisp))
10(in-package :2015-01-first-attempt)
11
12
13
14
15;; ---------------------------------------------------------------------
16;;; Definitions
17;; ---------------------------------------------------------------------
18
19(defparameter *PUZZLE-INPUT-FILE* "~/proj/Learn/aoc/src/2015.01._PUZZLE-INPUT.txt"
20 "File to be used as input to the program.")
21
22(defparameter *PUZZLE-INPUT-STRING* (uiop:read-file-string *PUZZLE-INPUT-FILE*)
23 "String version of the text in `*PUZZLE-INPUT-FILE*'.")
24
25(defparameter *PUZZLE-INPUT* (loop for char across *PUZZLE-INPUT-STRING*
26 collect char)
27 "Puzzle input in lisp list form.")
28
29
30
31
32;; ---------------------------------------------------------------------
33;;; Part 1
34;; -------
35;; Santa is trying to deliver presents in a large apartment building, but
36;; he can't find the right floor - the directions he got are a little
37;; confusing. He starts on the ground floor (floor 0) and then follows
38;; the instructions one character at a time.
39;;
40;; An opening parenthesis, (, means he should go up one floor, and a
41;; closing parenthesis, ), means he should go down one floor.
42;;
43;; The apartment building is very tall, and the basement is very deep;
44;; he will never find the top or bottom floors.
45;;
46;; For example:
47;;
48;; (()) and ()() both result in floor 0.
49;; ((( and (()(()( both result in floor 3.
50;; ))((((( also results in floor 3.
51;; ()) and ))( both result in floor -1 (the first basement level).
52;; ))) and )())()) both result in floor -3.
53;;
54;; To what floor do the instructions take Santa?
55;; ---------------------------------------------------------------------
56
57(defun parse-input (INPUT)
58 "Parse `INPUT' to determine where the instructions take Santa."
59 (let ((FLOOR-NUMBER 0))
60 (+ FLOOR-NUMBER (cond ((eql (first INPUT) nil) 0)
61 ((eql (first INPUT) #\() (+ 1 (parse-input (rest INPUT))))
62 ((eql (first INPUT) #\)) (- (parse-input (rest INPUT)) 1))))))
63
64;; Test for correct answer
65(assert (= 138 (parse-input *PUZZLE-INPUT*)))
66
67
68;; I have elected to just throw this away and not do part 2 in this line of thinking.
69;; Keeping it for posterity.