Skip to content

Commit

Permalink
Cosmetics
Browse files Browse the repository at this point in the history
Another round of minor cosmetic changes, so I don't have to feel that bad for my code.
  • Loading branch information
abraemer committed Dec 4, 2018
1 parent 8cb0826 commit be7262e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
8 changes: 6 additions & 2 deletions advent-of-code-2018.asd
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
11 changes: 4 additions & 7 deletions day1.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
1 change: 0 additions & 1 deletion day2.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

(in-package #:advent-of-code-2018)


(defun count-chars (string)
(loop
:with counts := nil
Expand Down
10 changes: 5 additions & 5 deletions day3.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand All @@ -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))))))

18 changes: 8 additions & 10 deletions day4.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
(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)
(mapcan #'list '(:month :day :hour :min) (list month day hour min))))

(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)
Expand Down Expand Up @@ -53,18 +51,18 @@

(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)))
(most-asleep-id (max-key asleep-counters :accessor (lambda (c) (reduce #'+ c))))
(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))))

1 change: 1 addition & 0 deletions utilities.lisp
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit be7262e

Please sign in to comment.