diff options
author | Tristan Williams <tgwil@tgwil.net> | 2025-01-11 18:30:10 -0500 |
---|---|---|
committer | Tristan Williams <tgwil@tgwil.net> | 2025-01-11 18:30:10 -0500 |
commit | d12822711f4d1fe3519f63eb117a0b3018e3de07 (patch) | |
tree | 8c9e6455b7eaa1e4ff7e7241e8368dc3bceec732 /src/2015.02.01-first-attempt.lisp | |
parent | Fix header comments format again (diff) | |
download | aoc-d12822711f4d1fe3519f63eb117a0b3018e3de07.tar.gz aoc-d12822711f4d1fe3519f63eb117a0b3018e3de07.tar.bz2 aoc-d12822711f4d1fe3519f63eb117a0b3018e3de07.zip |
Partial progress: AoC 2015 Day 2, Part 1 done
Diffstat (limited to 'src/2015.02.01-first-attempt.lisp')
-rw-r--r-- | src/2015.02.01-first-attempt.lisp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/2015.02.01-first-attempt.lisp b/src/2015.02.01-first-attempt.lisp new file mode 100644 index 0000000..14b3cfe --- /dev/null +++ b/src/2015.02.01-first-attempt.lisp | |||
@@ -0,0 +1,130 @@ | |||
1 | ;;;; 2015.02.01-first-attempt.lisp | ||
2 | ;;;; Advent of Code 2015 | ||
3 | ;;;; Day 2: I Was Told There Would Be No Math | ||
4 | ;;;; Attempt 1: First attempt in Common Lisp | ||
5 | |||
6 | ;; --------------------------------------------------------------------- | ||
7 | ;;; Package Definition | ||
8 | ;; --------------------------------------------------------------------- | ||
9 | |||
10 | (ql:quickload :cl-ppcre) | ||
11 | (ql:quickload :alexandria) | ||
12 | |||
13 | (defpackage :2015-02-01-first-attempt | ||
14 | (:use #:common-lisp | ||
15 | #:cl-ppcre | ||
16 | #:alexandria)) | ||
17 | (in-package :2015-02-01-first-attempt) | ||
18 | |||
19 | |||
20 | |||
21 | |||
22 | ;; --------------------------------------------------------------------- | ||
23 | ;;; Definitions | ||
24 | ;; --------------------------------------------------------------------- | ||
25 | |||
26 | (defconstant +PUZZLE-INPUT-FILE+ "~/proj/Learn/aoc/src/2015.02._PUZZLE-INPUT.txt" | ||
27 | "File to be used as input to the program.") | ||
28 | |||
29 | (defconstant +PUZZLE-INPUT-STRING+ (uiop:read-file-string +PUZZLE-INPUT-FILE+) | ||
30 | "Single-string version of the text in `+PUZZLE-INPUT-FILE+'.") | ||
31 | |||
32 | (defconstant +PUZZLE-INPUT-STRING-LIST+ | ||
33 | (loop for line in (ppcre:split #\Newline +PUZZLE-INPUT-STRING+) | ||
34 | collect (ppcre:all-matches-as-strings "-?\\d+" line)) | ||
35 | "List of sublists containing string versions of the dimensions of presents.") | ||
36 | |||
37 | (defconstant +PUZZLE-INPUT-LIST+ | ||
38 | (loop for sublist in +PUZZLE-INPUT-STRING-LIST+ | ||
39 | collect (mapcar #'parse-integer sublist)) | ||
40 | "A list of presents and their numerical dimensions. | ||
41 | Represented as a list of sublists, with each sublist containing the 3 | ||
42 | dimensions of a particular present.") | ||
43 | |||
44 | |||
45 | |||
46 | |||
47 | ;; --------------------------------------------------------------------- | ||
48 | ;;; Part 1 | ||
49 | ;; ------- | ||
50 | ;; The elves are running low on wrapping paper, and so they need to | ||
51 | ;; submit an order for more. They have a list of the dimensions | ||
52 | ;; (length l, width w, and height h) of each present, and only want to | ||
53 | ;; order exactly as much as they need. | ||
54 | ;; | ||
55 | ;; Fortunately, every present is a box (a perfect right rectangular | ||
56 | ;; prism), which makes calculating the required wrapping paper for each | ||
57 | ;; gift a little easier: find the surface area of the box, which is | ||
58 | ;; 2*l*w + 2*w*h + 2*h*l. The elves also need a little extra paper for | ||
59 | ;; each present: the area of the smallest side. | ||
60 | ;; | ||
61 | ;; For example: | ||
62 | ;; | ||
63 | ;; A present with dimensions 2x3x4 requires 2*6 + 2*12 + 2*8 = 52 | ||
64 | ;; square feet of wrapping paper plus 6 square feet of slack, for a | ||
65 | ;; total of 58 square feet. | ||
66 | ;; | ||
67 | ;; A present with dimensions 1x1x10 requires 2*1 + 2*10 + 2*10 = 42 | ||
68 | ;; square feet of wrapping paper plus 1 square foot of slack, for a | ||
69 | ;; total of 43 square feet. | ||
70 | ;; | ||
71 | ;; All numbers in the elves' list are in feet. How many total square | ||
72 | ;; feet of wrapping paper should they order? | ||
73 | ;; --------------------------------------------------------------------- | ||
74 | |||
75 | |||
76 | (defun smallest-two (DIMENSIONS) | ||
77 | "Return the smallest 2 `DIMENSIONS', assuming there are 3." | ||
78 | (cdr (sort DIMENSIONS #'>))) | ||
79 | |||
80 | (defun wrapping-paper-area (DIMENSIONS) | ||
81 | "Calculate the amount of paper needed for a present with `DIMENSIONS'. | ||
82 | `DIMENSIONS' is assumed to be a list with 3 elements representing the | ||
83 | length, width, and height of a particular present." | ||
84 | (+ (apply #'+ (list (* 2 (first DIMENSIONS) (second DIMENSIONS)) | ||
85 | (* 2 (first DIMENSIONS) (third DIMENSIONS)) | ||
86 | (* 2 (second DIMENSIONS) (third DIMENSIONS)))) | ||
87 | (apply #'* (smallest-two DIMENSIONS)))) | ||
88 | |||
89 | ;; Test single-present examples from problem text | ||
90 | (assert (= 58 (wrapping-paper-area '(2 3 4)))) | ||
91 | (assert (= 43 (wrapping-paper-area '(1 1 10)))) | ||
92 | |||
93 | (defun wrapping-paper-total (PRESENTS) | ||
94 | "Calculate the total amount of wrapping paper needed for the entire list of `PRESENTS'. | ||
95 | `PRESENTS' is assumed to be a list of sublists of 3 | ||
96 | elements each, representing the dimensions of each present." | ||
97 | (apply #'+ (mapcar #'wrapping-paper-area PRESENTS))) | ||
98 | |||
99 | ;; Test correct answer | ||
100 | (assert (= 1586300 (wrapping-paper-total +PUZZLE-INPUT-LIST+))) | ||
101 | |||
102 | |||
103 | |||
104 | |||
105 | ;; --------------------------------------------------------------------- | ||
106 | ;;; Part 2 | ||
107 | ;; ------- | ||
108 | ;; The elves are also running low on ribbon. Ribbon is all the same | ||
109 | ;; width, so they only have to worry about the length they need to | ||
110 | ;; order, which they would again like to be exact. | ||
111 | ;; | ||
112 | ;; The ribbon required to wrap a present is the shortest distance around | ||
113 | ;; its sides, or the smallest perimeter of any one face. Each present | ||
114 | ;; also requires a bow made out of ribbon as well; the feet of ribbon | ||
115 | ;; required for the perfect bow is equal to the cubic feet of volume of | ||
116 | ;; the present. Don't ask how they tie the bow, though; they'll never | ||
117 | ;; tell. | ||
118 | ;; | ||
119 | ;; For example: | ||
120 | ;; | ||
121 | ;; A present with dimensions 2x3x4 requires 2+2+3+3 = 10 feet of | ||
122 | ;; ribbon to wrap the present plus 2*3*4 = 24 feet of ribbon for the | ||
123 | ;; bow, for a total of 34 feet. | ||
124 | ;; | ||
125 | ;; A present with dimensions 1x1x10 requires 1+1+1+1 = 4 feet of | ||
126 | ;; ribbon to wrap the present plus 1*1*10 = 10 feet of ribbon for | ||
127 | ;; the bow, for a total of 14 feet. | ||
128 | ;; | ||
129 | ;; How many total feet of ribbon should they order? | ||
130 | ;; --------------------------------------------------------------------- | ||