From be7262ed6f5f966d2345c9a6b911793117800949 Mon Sep 17 00:00:00 2001 From: Adrian Braemer <11058200+NobodysHero@users.noreply.github.com> Date: Tue, 4 Dec 2018 23:09:38 +0100 Subject: [PATCH] Cosmetics Another round of minor cosmetic changes, so I don't have to feel that bad for my code. --- advent-of-code-2018.asd | 8 ++++++-- day1.lisp | 11 ++++------- day2.lisp | 1 - day3.lisp | 10 +++++----- day4.lisp | 18 ++++++++---------- utilities.lisp | 1 + 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/advent-of-code-2018.asd b/advent-of-code-2018.asd index c0bf837..3d37a42 100644 --- a/advent-of-code-2018.asd +++ b/advent-of-code-2018.asd @@ -2,10 +2,14 @@ (asdf:defsystem #:advent-of-code-2018 :description "Solutions for the Advent of Code 2018" - :author "Adrian Braemer" + :author "NobodysHero" :license "Specify license here" :version "0.0.1" :serial t :depends-on ("cl-ppcre") :components ((:file "package") - (:file "advent-of-code-2018"))) + (:file "utilities") + (:file "day1") + (:file "day2") + (:file "day3") + (:file "day4"))) diff --git a/day1.lisp b/day1.lisp index b78d134..2804895 100644 --- a/day1.lisp +++ b/day1.lisp @@ -2,18 +2,15 @@ (in-package #:advent-of-code-2018) - (defun day1 () (let* ((changes (mapcar #'parse-integer (read-puzzlefile "input1.txt"))) - (total (reduce #'+ changes)) + (total (reduce #'+ changes)) ;answer to the first part (seen (make-hash-table :size 1024)) ;use hashtable as a set (first-repetition (loop - ;loop continuously over the changes - :for change :in (setf (cdr (last changes)) changes) + :for change :in (setf (cdr (last changes)) changes) ;loop continuously over the changes :sum change :into freq - :until (gethash freq seen) ;check the set - :do (setf (gethash freq seen) t) - :finally (return freq)))) + :when (gethash freq seen) :return freq ;check the set + :do (setf (gethash freq seen) t)))) (format t "The resulting frequency is ~a.~%The first frequency reached twice is ~a.~%" total first-repetition))) diff --git a/day2.lisp b/day2.lisp index e7ce95d..ddb7b05 100644 --- a/day2.lisp +++ b/day2.lisp @@ -2,7 +2,6 @@ (in-package #:advent-of-code-2018) - (defun count-chars (string) (loop :with counts := nil diff --git a/day3.lisp b/day3.lisp index 797aca3..cbe84e9 100644 --- a/day3.lisp +++ b/day3.lisp @@ -2,9 +2,8 @@ (in-package #:advent-of-code-2018) - (defun day3-parse-line (line) - ;; example "#1 @ 896,863: 29x19" + ;; example input "#1 @ 896,863: 29x19" (ppcre:register-groups-bind ((#'parse-integer id xpos ypos width height)) ("#(\\d+) @ (\\d+),(\\d+): (\\d+)x(\\d+)" line) (mapcan #'list '(:id :xpos :ypos :width :height) (list id xpos ypos width height)))) @@ -24,14 +23,15 @@ (loop-line-by-line (puzzlepath "input3.txt") :for claim := (day3-parse-line line) :do (make-claim claim board) - :do (setf (gethash (getf claim :id) ids) t)) + :do (setf (gethash (getf claim :id) ids) t)) ;collect all claim ids (loop :with overlaps := 0 :for index :below (reduce #'* (array-dimensions board)) :for entries := (row-major-aref board index) :when (>= (length entries) 2) :do (incf overlaps) :and - :do (dolist (id entries) (remhash id ids)) + :do (dolist (id entries) (remhash id ids)) ;remove claim ids with collisions :finally - (format t "There are ~a square inches of fabric within two or more claims.~%Id ~a has no overlaps at all." overlaps (first (hash-keys ids)))))) + (format t "There are ~a square inches of fabric within two or more claims.~%Id ~a has no overlaps at all." + overlaps (first (hash-keys ids)))))) diff --git a/day4.lisp b/day4.lisp index 52464e4..2044939 100644 --- a/day4.lisp +++ b/day4.lisp @@ -3,7 +3,7 @@ (in-package #:advent-of-code-2018) (defun day4-parse-time (time) - ;; ex. "[1518-05-25 00:11] some more text here" + ;; example input "[1518-05-25 00:11] some more text here" ;;ignore the year (ppcre:register-groups-bind ((#'parse-integer nil month day hour min)) ("\\[(\\d+)-(\\d+)-(\\d+) (\\d+):(\\d+)\\]" time) @@ -11,11 +11,9 @@ (defun day4-time< (log1 log2) (loop :for entry :in '(:month :day :hour :min) - :when (< (getf log1 entry 0) - (getf log2 entry 0)) + :when (< (getf log1 entry 0) (getf log2 entry 0)) :return t - :when (> (getf log1 entry 0) - (getf log2 entry 0)) + :when (> (getf log1 entry 0) (getf log2 entry 0)) :return nil)) (defun day4-parse-event (event) @@ -53,9 +51,9 @@ (defun day4-prep-logdata () (day4-organise-log - (loop-line-by-line (puzzlepath "input4.txt") - :collect (day4-parse-line line) into log - :finally (return (sort log #'day4-time<))))) + (sort + (mapcar #'day4-parse-line (read-puzzlefile "input4.txt")) + #'day4-time<))) (defun day4 () (let* ((asleep-counters (day4-asleep-counters (day4-prep-logdata))) @@ -63,8 +61,8 @@ (most-asleep-minute (max-index (gethash most-asleep-id asleep-counters))) (most-freq-asleep-id (max-key asleep-counters :accessor (lambda (c) (reduce #'max c)))) (most-freq-asleep-minute (max-index (gethash most-freq-asleep-id asleep-counters)))) - (format t "The most asleep guard is #~a and it's most asleep during minute ~a -> Answer: ~a~%" + (format t "The most asleep guard is #~a and it's most asleep during minute ~a. -> Answer: ~a~%" most-asleep-id most-asleep-minute (* most-asleep-id most-asleep-minute)) - (format t "The most frequently asleep guard is #~a and it's most asleep during minute ~a -> Answer: ~a~%" + (format t "The most frequently asleep guard is #~a and it's most asleep during minute ~a. -> Answer: ~a~%" most-freq-asleep-id most-freq-asleep-minute (* most-freq-asleep-id most-freq-asleep-minute)))) diff --git a/utilities.lisp b/utilities.lisp index 964cbee..dfd2858 100644 --- a/utilities.lisp +++ b/utilities.lisp @@ -1,6 +1,7 @@ ;;;; advent-of-code-2018.lisp (in-package #:advent-of-code-2018) + (defconstant +path+ "D:/Daten/lisp/advent-of-code-2018/inputs/") (defun puzzlepath (file)