aboutsummaryrefslogtreecommitdiff
path: root/src/2015.01.01-first-attempt.lisp
blob: dd9f36b1daffac53b95bb3144cfee54a9a250c85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
;;;; 2015.01.01-first-attempt.lisp
;;;; Advent of Code 2015 Day 1, 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.