diff options
author | Tristan Williams <tgwil@tgwil.net> | 2025-01-11 14:07:50 -0500 |
---|---|---|
committer | Tristan Williams <tgwil@tgwil.net> | 2025-01-11 14:07:50 -0500 |
commit | 03e14731fb51fafbf7931231674ecf7a621fcbf4 (patch) | |
tree | 969f82a20353cd279354b97b26fde756651780b3 /src | |
download | aoc-03e14731fb51fafbf7931231674ecf7a621fcbf4.tar.gz aoc-03e14731fb51fafbf7931231674ecf7a621fcbf4.tar.bz2 aoc-03e14731fb51fafbf7931231674ecf7a621fcbf4.zip |
Complete AoC 2015 Day 1
Diffstat (limited to 'src')
-rw-r--r-- | src/2015.01.01-first-attempt.lisp | 69 | ||||
-rw-r--r-- | src/2015.01.02-more-lispy-this-time.lisp | 106 | ||||
-rw-r--r-- | src/2015.01._PUZZLE-INPUT.txt | 1 |
3 files changed, 176 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. | ||
diff --git a/src/2015.01.02-more-lispy-this-time.lisp b/src/2015.01.02-more-lispy-this-time.lisp new file mode 100644 index 0000000..15614bc --- /dev/null +++ b/src/2015.01.02-more-lispy-this-time.lisp | |||
@@ -0,0 +1,106 @@ | |||
1 | ;;;; 2015.01.02-more-lispy-this-time.lisp | ||
2 | ;;;; Advent of Code 2015 Day 1, first attempt in Lisp | ||
3 | |||
4 | ;; --------------------------------------------------------------------- | ||
5 | ;;; Package Definition | ||
6 | ;; --------------------------------------------------------------------- | ||
7 | |||
8 | (defpackage :2015-01-02-more-lispy-this-time | ||
9 | (:use #:common-lisp)) | ||
10 | (in-package :2015-01-02-more-lispy-this-time) | ||
11 | |||
12 | |||
13 | |||
14 | |||
15 | ;; --------------------------------------------------------------------- | ||
16 | ;;; Definitions | ||
17 | ;; --------------------------------------------------------------------- | ||
18 | |||
19 | (defconstant +PUZZLE-INPUT-FILE+ "~/proj/Learn/aoc/src/2015.01._PUZZLE-INPUT.txt" | ||
20 | "File to be used as input to the program.") | ||
21 | |||
22 | (defconstant +PUZZLE-INPUT-STRING+ (uiop:read-file-string +PUZZLE-INPUT-FILE+) | ||
23 | "String version of the text in `+PUZZLE-INPUT-FILE+'.") | ||
24 | |||
25 | (defconstant +PUZZLE-INPUT-LIST+ (loop for char across +PUZZLE-INPUT-STRING+ | ||
26 | collect char) | ||
27 | "Puzzle input in lisp list form.") | ||
28 | |||
29 | (defconstant +PUZZLE-INPUT-VALUES-LIST+ (mapcar (lambda (INPUT-LIST) | ||
30 | (cond ((eql INPUT-LIST #\() 1) | ||
31 | ((eql INPUT-LIST #\)) -1))) | ||
32 | +PUZZLE-INPUT-LIST+) | ||
33 | "Puzzle input converted into floor direction values.") | ||
34 | |||
35 | |||
36 | |||
37 | |||
38 | ;; --------------------------------------------------------------------- | ||
39 | ;;; Part 1 | ||
40 | ;; ------- | ||
41 | ;; Santa is trying to deliver presents in a large apartment building, but | ||
42 | ;; he can't find the right floor - the directions he got are a little | ||
43 | ;; confusing. He starts on the ground floor (floor 0) and then follows | ||
44 | ;; the instructions one character at a time. | ||
45 | ;; | ||
46 | ;; An opening parenthesis, (, means he should go up one floor, and a | ||
47 | ;; closing parenthesis, ), means he should go down one floor. | ||
48 | ;; | ||
49 | ;; The apartment building is very tall, and the basement is very deep; | ||
50 | ;; he will never find the top or bottom floors. | ||
51 | ;; | ||
52 | ;; For example: | ||
53 | ;; | ||
54 | ;; (()) and ()() both result in floor 0. | ||
55 | ;; ((( and (()(()( both result in floor 3. | ||
56 | ;; ))((((( also results in floor 3. | ||
57 | ;; ()) and ))( both result in floor -1 (the first basement level). | ||
58 | ;; ))) and )())()) both result in floor -3. | ||
59 | ;; | ||
60 | ;; To what floor do the instructions take Santa? | ||
61 | ;; --------------------------------------------------------------------- | ||
62 | |||
63 | (defun final-floor (INPUT) | ||
64 | "Calculate the final floor Santa ends up on given the provided `INPUT'." | ||
65 | (apply #'+ INPUT)) | ||
66 | |||
67 | (assert (= 138 (final-floor +PUZZLE-INPUT-VALUES-LIST+))) | ||
68 | |||
69 | |||
70 | |||
71 | |||
72 | ;; --------------------------------------------------------------------- | ||
73 | ;;; Part 2 | ||
74 | ;; ------- | ||
75 | ;; Now, given the same instructions, find the position of the first | ||
76 | ;; character that causes him to enter the basement (floor -1). The first | ||
77 | ;; character in the instructions has position 1, the second character | ||
78 | ;; has position 2, and so on. | ||
79 | ;; | ||
80 | ;; For example: | ||
81 | ;; | ||
82 | ;; ) causes him to enter the basement at character position 1. | ||
83 | ;; ()()) causes him to enter the basement at character position 5. | ||
84 | ;; | ||
85 | ;; What is the position of the character that causes Santa to first enter the basement? | ||
86 | ;; --------------------------------------------------------------------- | ||
87 | |||
88 | (defun next-floor () | ||
89 | "Determine the result of the next button press, go in that direction, and destroy the information so the Nazis don't get their dirty hands on it." | ||
90 | (let ((FLOOR-NUMBER 0) | ||
91 | (INPUT-POSITION 0) | ||
92 | (INPUT +PUZZLE-INPUT-VALUES-LIST+)) | ||
93 | (lambda () | ||
94 | (list (incf FLOOR-NUMBER (pop INPUT)) | ||
95 | (incf INPUT-POSITION))))) | ||
96 | (defvar next-floor (next-floor)) | ||
97 | |||
98 | (defun first-basement-input () | ||
99 | "Determine the position of the input in `+PUZZLE-INPUT-VALUES-LIST' which results in the first entry into the basement (floor -1)." | ||
100 | (let ((RESULT (funcall next-floor))) | ||
101 | (loop until (= -1 (car RESULT)) | ||
102 | do (setf RESULT (funcall next-floor))) | ||
103 | (cdr RESULT))) | ||
104 | |||
105 | ;; Test for correct result | ||
106 | (assert (= 1771 (first-basement-input))) | ||
diff --git a/src/2015.01._PUZZLE-INPUT.txt b/src/2015.01._PUZZLE-INPUT.txt new file mode 100644 index 0000000..71a7803 --- /dev/null +++ b/src/2015.01._PUZZLE-INPUT.txt | |||
@@ -0,0 +1 @@ | |||
()(((()))(()()()((((()(((())(()(()((((((()(()(((())))((()(((()))((())(()((()()()()(((())(((((((())))()()(()(()(())(((((()()()((())(((((()()))))()(())(((())(())((((((())())))(()())))()))))()())()())((()()((()()()()(()((((((((()()())((()()(((((()(((())((())(()))()((((()((((((((())()((()())(())((()))())((((()())(((((((((((()()(((((()(()))())(((()(()))())((()(()())())())(()(((())(())())()()(()(()((()))((()))))((((()(((()))))((((()(()(()())())()(((()((((())((((()(((()()(())()()()())((()((((((()((()()))()((()))()(()()((())))(((()(((()))((()((()(()))(((()()(()(()()()))))()()(((()(((())())))))((()(((())()(()(())((()())))((((())))(()(()(()())()((()())))(((()((()(())()()((()((())(()()((())(())()))()))((()(())()))())(((((((()(()()(()(())())))))))(()((((((())((((())((())())(()()))))()(())(()())()())((())(()))))(()))(()((()))()(()((((((()()()()((((((((()(()(())((()()(()()))(())()())()((())))()))()())(((()))(())()(())()))()((()((()(()()())(())()()()((())())))((()()(()()((()(())()()())(((()(()()))))(())))(()(()())()))()()))))))()))))((((((())))())))(()(())())(()())))))(()))()))))))()((()))))()))))(()(()((()())())(()()))))(((())()))())())())(((()(()()))(())()(())(())((((((()()))))((()(()))))))(()))())(((()()(()))()())()()()())))))))))))))(())(()))(()))((()(())(()())(())())(()())(())()()(()())))()()()))(())())()))())())(())((())))))))(())))(())))))()))))((())(()(((()))))(()))()((()(())))(()())(((((()))()())()()))))()))))()))())(()(()()()))()))))))((()))))))))))()((()))((()(())((())()()(()()))()(()))))()()(()))()))(((())))(())()((())(())(()())()())())))))))())))()((())))()))(()))()()))(((((((()))())(()()))(()()(()))()(()((()())()))))))(((()()()())))(())()))()())(()()))()()))))))))(())))()))()()))))))()))()())))()(())(())))))()(())()()(()()))))())((()))))()))))(()(((((()))))))))())))())()(())()()))))(())))())()()())()()())()(()))))()))()))))))))())))((()))()))()))())))()())()()())))())))(()((())()((()))())))))())()(())((())))))))))))())()())(())())())(()))(()))()))())(()(())())()())()()(()))))(()(())))))))(())))())(())))))))())()()(())())())))(())))))()))()(()())()(()))())())))))()()(()))()))))())))))))))()))))()))))))())()())()()))))()())))())))))))))))()()))))()()(((()))()()(())()))))((()))))(()))(())())))(())()))))))(()))()))))(())())))))()))(()())))))))))))))())))))))))()((()())(()())))))))((()))))(())(())))()(()())())))())())(()()()())))()))))))())))))())()()())))))))))))()()(()))))()())()))((()())(()))))()(()))))))))))()())())(((())(()))))())()))()))()))))))()))))))(()))))()))))()(())))(())))(()))())()()(()()))()))(()()))))))))()))(()))())(()()(()(()())()()))()))))))))(())))))((()()(()))())())))))()))())(()())()()))())))()(()()()()))((())())))())()(()()))()))))))))(()))(())))()))))(()(()())(()))))()())())()))()()))())))))))))))())()))))))()))))))))())))))()))))())(()())))(())()))())())))))()()(()()())(()())))()()))(((()))(()()()))))()))))()))))((())))()((((((()()))))))())))))))))))(((()))))))))))))(())())))))())(()))))))(()))((()))())))()(()((()))()))()))))))))))())()))()(()()))))())))())(())()(()))()))())(()))()))))(()()))()()(())))))()))(())(()(()()))(()()())))))(((()))))))()))))))))))))(())(()))))()())())()()((()()))())))))(()))))())))))))()()()))))))))())))()(((()()))(())))))(((())())))))((()))()(()))(()))))(()())))(()))())))))()))))(())(())))()((()))(())())))()()))()))))))))()))(()()()(()()()(()))())(())()())(((()))(())))))))))(((()())))()()))))))))()(())(()))()((((())(())(()())))()))(((())()()()))((()))(()))())())))())))(()))())()())())(()(())())()()()(())))())(())))(())))(())()))()))(()((()))))))))())(()))))))())(()()))()()))()(()(()())))()()(()((()((((((()))(())))()()()))())()))((()()(()))())((()(()(()))(()()))))()())))()))()())))))))()()((()())(())))()))(()))(())(()))())(()(())))()()))))))(((()(((()()))()(()(())())((()()))()))()))()))()(()()()(()))((()())()(())))()()))(((())()()())(())()((()()()()(()(())(()()))()(((((()())))((())))))(()()()))))(((()(())))()))((()((()(())()(()((())))((()())()(()))(((()())()()(()))(())(((()((()())()((())()())(((()()))((()((())(()))(()())(()()()))((()))(())(()((()()())((()))(())))(())(())(())))(()())))(((((()(()(((((()())((((()(()())(())(()()(((())((()(((()()(((()()((((((())))())(()((((((()(()))()))()()((()((()))))()(()()(()((()()))))))(((((()(((((())()()()(())())))))))()))((()()(())))(())(()()()())))))(()((((())))))))()()(((()(()(()(()(()())()()()(((((((((()()())()(()))((()()()()()(((((((()())()((())()))((((((()(()(()(()())(((()(((((((()(((())(((((((((())(())())()))((()(()))(((()()())(())(()(()()(((()(())()))())))(())((((((())(()()())()()(((()(((())(()(((())(((((((()(((((((((()))(())(()(()(()))))((()))()(())())())((()(()((()()))((()()((()(())(())(()((())(((())(((()()()((((((()()(())((((())()))))(())((()(()((())))(((((()(()()())())((())())))((())((()((()()((((((())(((()()(()())())(()(()))(()(()))())())()(((((((()(((()(())()()((())((()(()()((()(()()(((((((((((())((())((((((())((()((((()(()((((()(((((((())()((()))))())()((()((((()(()(((()((()())))(())())(((()(((())((((((()(((((((((()()(())))(()(((((()((((()())))((()((()((()(()()(((())((((((((((((()(((())(()(((((()))(()()(()()()()()()((())(((((((())(((((())))))())()(()()(()(()(((()()(((((())(()((()((()(((()()((()((((())()))()((((())(())))()())(((())(())(()()((()(((()()((((((((((()()(()())())(((((((((())((((()))()()((((())(()((((()(((())())(((((((((((()((((())))(())(()(((()(((()((())(((((()((()()(()(()()((((((()((((()((()(()((()(()((((((()))))()()(((((()((()(()(())()))(())(((((((()((((()())(()((()((()(()))())))(())((()))))(((((((()()()())(()))(()()((()())()((()((()()()(()(()()))(()())(())(((((()(((((((((((()((()(((()(((((((()()((((((()(((((()(()((()(((((())((((((()))((((())((()()((())(((())()(((((()()(((((()((()(()(((((((()(((((()((()((()((())(())((())(()))()()))(()()(()(()()(((((((()(((()(((())()(((((()((((((()())((((())()((()((()(()()())(()))((((()()((((((()((()(()(()((((()((()((())((((((()(()(())((((((()((((((((((()((())()))()(()(()(((((()()()))((())))()(()((((((((((((((()(((()((((()((())((()((()(((()()(()(((()((())(()()())))()(()(()(((((()()(()(()((((()(((((())()(()(()))(((((()()(((()()(())((((((((((((((())((())(((((((((((())()()()(())()(()(()(((((((((())(((()))(()()())(()((((()(())(((((()())(())((((((((())()((((()((((((())(()((()(())(((()((((()))(((((((((()()))((((()(())()()()(())(()((())((()()))()(((())(((((())((((((()()))(((((((((()((((((())))(((((((()((()(()(())))())(()(()))()(((((()())(()))()(()(())(((()))))())()())))(((((()))())()((()(()))))((()()()((((((()))()()((((((((())((()(()(((()(()((())((()())(()((((())(()(((()()()(()(()()))())())((((((((((())())((()))()((())(())(())))())()(()()(())))())(()))(((()(()()(((()(((())))()(((()(())()((((((())()))()))()((((((()(()(((((()())))()))))())()()(((()(((((())((()()(()((()((()(()(()(())))(()()()()((()(())(((()((()))((((()))())(())))())(()))()()()())()))(((()()())()((())))(())(()()()()(()())((()(()()((((())))((()((()(())((()(()((())()(()()(((()())()()())((()))((())(((()()(())))()()))(((()((())()(((((()())(())((())()())())((((((()(()(((((()))(()( \ No newline at end of file | |||