0a241f64eebb
2016/01
author | Steve Losh <steve@stevelosh.com> |
---|---|
date | Fri, 06 Dec 2019 20:57:40 -0500 |
parents | 9b6e236a27a0 |
children | cb62272f4acc |
branches/tags | (none) |
files | advent.asd data/2016/01.txt package.lisp src/2016/days/day-01.lisp src/2017/days/day-03.lisp src/2019/days/day-03.lisp src/utils.lisp |
Changes
--- a/advent.asd Fri Dec 06 18:58:39 2019 -0500 +++ b/advent.asd Fri Dec 06 20:57:40 2019 -0500 @@ -41,6 +41,8 @@ (:file "package") (:module "src" :serial t :components ((:file "utils") + (:module "2016" :serial t + :components ((:auto-module "days"))) (:module "2017" :serial t :components ((:file "number-spiral") (:file "knot-hash")
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/2016/01.txt Fri Dec 06 20:57:40 2019 -0500 @@ -0,0 +1,1 @@ +R1, L3, R5, R5, R5, L4, R5, R1, R2, L1, L1, R5, R1, L3, L5, L2, R4, L1, R4, R5, L3, R5, L1, R3, L5, R1, L2, R1, L5, L1, R1, R4, R1, L1, L3, R3, R5, L3, R4, L4, R5, L5, L1, L2, R4, R3, R3, L185, R3, R4, L5, L4, R48, R1, R2, L1, R1, L4, L4, R77, R5, L2, R192, R2, R5, L4, L5, L3, R2, L4, R1, L5, R5, R4, R1, R2, L3, R4, R4, L2, L4, L3, R5, R4, L2, L1, L3, R1, R5, R5, R2, L5, L2, L3, L4, R2, R1, L4, L1, R1, R5, R3, R3, R4, L1, L4, R1, L2, R3, L3, L2, L1, L2, L2, L1, L2, R3, R1, L4, R1, L1, L4, R1, L2, L5, R3, L5, L2, L2, L3, R1, L4, R1, R1, R2, L1, L4, L4, R2, R2, R2, R2, R5, R1, L1, L4, L5, R2, R4, L3, L5, R2, R3, L4, L1, R2, R3, R5, L2, L3, R3, R1, R3
--- a/package.lisp Fri Dec 06 18:58:39 2019 -0500 +++ b/package.lisp Fri Dec 06 20:57:40 2019 -0500 @@ -33,6 +33,7 @@ :positions-if :digits :fresh-vector + :first-character :let-result :let-complex
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/2016/days/day-01.lisp Fri Dec 06 20:57:40 2019 -0500 @@ -0,0 +1,26 @@ +(defpackage :advent/2016/01 #.cl-user::*advent-use*) +(in-package :advent/2016/01) + + +(defun turn (direction heading) + (* heading (ecase direction + (#\L #c(0 1)) + (#\R #c(0 -1))))) + +(define-problem (2016 1) (data read-line) (298 158) + (iterate + (with pos = #c(0 0)) + (with seen = (make-hash-set :initial-contents (list #c(0 0)))) + (for ((#'first-character direction) (#'parse-integer distance)) + :matching "([LR])(\\d+)" :against data) + (for heading :seed #c(0 1) :then (turn direction heading)) + (do-repeat distance + (incf pos heading) + (finding-first pos :such-that (hset-contains-p seen pos) :into part2) + (hset-insert! seen pos)) + (returning (manhattan-distance pos) + (manhattan-distance part2)))) + + +#; Scratch -------------------------------------------------------------------- +
--- a/src/2017/days/day-03.lisp Fri Dec 06 18:58:39 2019 -0500 +++ b/src/2017/days/day-03.lisp Fri Dec 06 20:57:40 2019 -0500 @@ -2,12 +2,6 @@ (in-package :advent/2017/03) -(defun manhattan-distance (a b) - (let-complex ((ax ay a) - (bx by b)) - (+ (abs (- ax bx)) - (abs (- ay by))))) - (defun neighbors (coord) (iterate (for (dx dy) :within-radius 1 :skip-origin t) (collect (+ coord (complex dx dy)))))
--- a/src/2019/days/day-03.lisp Fri Dec 06 18:58:39 2019 -0500 +++ b/src/2019/days/day-03.lisp Fri Dec 06 20:57:40 2019 -0500 @@ -20,15 +20,11 @@ (princ (gethash (complex x y) grid #\.))) (terpri)))) -(defun first-character (string) - (aref string 0)) - (defun parse-path (string) - (gathering - (ppcre:do-register-groups - ((#'first-character direction) (#'parse-integer distance)) - ("([UDLR])(\\d+)" string) - (gather (cons direction distance))))) + (iterate + (for ((#'first-character direction) (#'parse-integer distance)) + :matching "([UDLR])(\\d+)" :against string) + (collect (cons direction distance)))) (defun delta (direction) (ecase direction
--- a/src/utils.lisp Fri Dec 06 18:58:39 2019 -0500 +++ b/src/utils.lisp Fri Dec 06 20:57:40 2019 -0500 @@ -32,6 +32,9 @@ (symbol (alexandria:make-keyword input)) (string (alexandria:make-keyword (string-upcase (str:trim input))))))) +(defun ensure-list (input) + (if (listp input) input (list input))) + ;;;; Problems ----------------------------------------------------------------- (defmacro define-problem-tests ((year day) part1 part2) @@ -72,7 +75,6 @@ :type "txt")) - ;;;; Readers ------------------------------------------------------------------ (defun read-numbers-from-string (line) (mapcar #'parse-integer (ppcre:all-matches-as-strings "-?\\d+" line))) @@ -227,11 +229,6 @@ (return start))))) -;;;; Iterate ------------------------------------------------------------------ -(defmacro returning (&rest values) - `(finally (return (values ,@values)))) - - ;;;; Miscellaneous ------------------------------------------------------------ (defun hash-table= (h1 h2 &optional (test #'eql)) "Return whether `h1` and `h2` have the same keys and values. @@ -434,6 +431,11 @@ (coerce sequence 'vector))) +(defun first-character (string) + "Return the first character of `string`." + (char string 0)) + + (defmacro let-result ((symbol initform) &body body) "Bind `symbol` to initform, execute `body`, and return `symbol`.