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 --- README.md | 25 ++++++++ license.txt | 18 ++++++ src/2015.01.01-first-attempt.lisp | 69 ++++++++++++++++++++ src/2015.01.02-more-lispy-this-time.lisp | 106 +++++++++++++++++++++++++++++++ src/2015.01._PUZZLE-INPUT.txt | 1 + 5 files changed, 219 insertions(+) create mode 100644 README.md create mode 100644 license.txt create mode 100644 src/2015.01.01-first-attempt.lisp create mode 100644 src/2015.01.02-more-lispy-this-time.lisp create mode 100644 src/2015.01._PUZZLE-INPUT.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..57255bc --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# Advent of Code + +This repository contains my solutions to [Advent of Code](https://adventofcode.com/) puzzles. I intend to start from Day 1 of the first year and working through them all. I will probably eventually do this in multiple languages. + +All solutions are contained within the `/src` directory and are named suchly: + +`YEAR.DAY.ATTEMPT.EXT` + +First attempts (per language) are named `01-first-attempt`, and are never edited afterwards. Editing is why I designed the file naming scheme to handle multiple attempts. + +For example: +- `2015.01.01-first-attempt.c` +- `2015.01.01-first-attempt.lisp` +- `2016.05.03-recursive.lisp` + + +## Progress + +The following table shows the number of complete attempts I've produced for each problem+language combination. + +COMPLETE SOLUTIONS: 0 + +| DAY | CL | +|---------|----| +| 2015.01 | | diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..365a8f0 --- /dev/null +++ b/license.txt @@ -0,0 +1,18 @@ +Copyright © 2024 Tristan Williams + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the “Software”), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 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. 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 @@ +;;;; 2015.01.02-more-lispy-this-time.lisp +;;;; Advent of Code 2015 Day 1, first attempt in Lisp + +;; --------------------------------------------------------------------- +;;; Package Definition +;; --------------------------------------------------------------------- + +(defpackage :2015-01-02-more-lispy-this-time + (:use #:common-lisp)) +(in-package :2015-01-02-more-lispy-this-time) + + + + +;; --------------------------------------------------------------------- +;;; Definitions +;; --------------------------------------------------------------------- + +(defconstant +PUZZLE-INPUT-FILE+ "~/proj/Learn/aoc/src/2015.01._PUZZLE-INPUT.txt" + "File to be used as input to the program.") + +(defconstant +PUZZLE-INPUT-STRING+ (uiop:read-file-string +PUZZLE-INPUT-FILE+) + "String version of the text in `+PUZZLE-INPUT-FILE+'.") + +(defconstant +PUZZLE-INPUT-LIST+ (loop for char across +PUZZLE-INPUT-STRING+ + collect char) + "Puzzle input in lisp list form.") + +(defconstant +PUZZLE-INPUT-VALUES-LIST+ (mapcar (lambda (INPUT-LIST) + (cond ((eql INPUT-LIST #\() 1) + ((eql INPUT-LIST #\)) -1))) + +PUZZLE-INPUT-LIST+) + "Puzzle input converted into floor direction values.") + + + + +;; --------------------------------------------------------------------- +;;; 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 final-floor (INPUT) + "Calculate the final floor Santa ends up on given the provided `INPUT'." + (apply #'+ INPUT)) + +(assert (= 138 (final-floor +PUZZLE-INPUT-VALUES-LIST+))) + + + + +;; --------------------------------------------------------------------- +;;; Part 2 +;; ------- +;; Now, given the same instructions, find the position of the first +;; character that causes him to enter the basement (floor -1). The first +;; character in the instructions has position 1, the second character +;; has position 2, and so on. +;; +;; For example: +;; +;; ) causes him to enter the basement at character position 1. +;; ()()) causes him to enter the basement at character position 5. +;; +;; What is the position of the character that causes Santa to first enter the basement? +;; --------------------------------------------------------------------- + +(defun next-floor () + "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." + (let ((FLOOR-NUMBER 0) + (INPUT-POSITION 0) + (INPUT +PUZZLE-INPUT-VALUES-LIST+)) + (lambda () + (list (incf FLOOR-NUMBER (pop INPUT)) + (incf INPUT-POSITION))))) +(defvar next-floor (next-floor)) + +(defun first-basement-input () + "Determine the position of the input in `+PUZZLE-INPUT-VALUES-LIST' which results in the first entry into the basement (floor -1)." + (let ((RESULT (funcall next-floor))) + (loop until (= -1 (car RESULT)) + do (setf RESULT (funcall next-floor))) + (cdr RESULT))) + +;; Test for correct result +(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 -- cgit v1.2.3