From: Jack Kinsey Date: Sat, 5 Dec 2020 10:05:44 +0000 (-0500) Subject: Get lambda reader macro working and fix day 03 bug X-Git-Url: http://git.jkinsey.net/?p=adventofcode2020.git;a=commitdiff_plain;h=a7a78c7ac10e6bb6245dcdeab826f118ca2c60b0 Get lambda reader macro working and fix day 03 bug --- diff --git a/src/day03.lisp b/src/day03.lisp index e708c87..093654f 100644 --- a/src/day03.lisp +++ b/src/day03.lisp @@ -1,23 +1,26 @@ (asdf:load-system :adventofcode2020) (in-package #:adventofcode2020) +(named-readtables:in-readtable fn-reader) (defun tree-collisions (slope tree-map) - (destructuring-bind (rise run) slope - (loop with terrain-width = (-> tree-map first length) - for i upfrom 0 by run - for j upfrom 0 - for terrain-line in tree-map - when (->> rise (mod j) (= 0)) - counting (->> terrain-width - (mod i) - (char terrain-line) - (char= #\#))))) + (loop with (rise run) = slope + with proper-map = (loop for j upfrom 0 + for line in tree-map + when (->> rise (mod j) (= 0)) + collecting line) + with terrain-width = (-> tree-map first length) + for i upfrom 0 by run + for terrain-line in proper-map + counting (->> terrain-width + (mod i) + (char terrain-line) + (char= #\#)))) (day 03 input (let ((tree-map (list-from input))) (part1 (tree-collisions '(1 3) tree-map)) (part2 (->> '((1 1) (1 3) (1 5) (1 7) (2 1)) - (mapcar (fn* (tree-collisions _ tree-map))) + (mapcar λ(tree-collisions _ tree-map)) (reduce #'*))))) (def-suite day03) @@ -42,17 +45,17 @@ (test multiple-collision-check (is (equal '(2 7 3 4 2) - (mapcar (fn* (tree-collisions _ '("..##......." - "#...#...#.." - ".#....#..#." - "..#.#...#.#" - ".#...##..#." - "..#.##....." - ".#.#.#....#" - ".#........#" - "#.##...#..." - "#...##....#" - ".#..#...#.#"))) + (mapcar λ(tree-collisions _ '("..##......." + "#...#...#.." + ".#....#..#." + "..#.#...#.#" + ".#...##..#." + "..#.##....." + ".#.#.#....#" + ".#........#" + "#.##...#..." + "#...##....#" + ".#..#...#.#")) '((1 1) (1 3) (1 5) (1 7) (2 1)))))) (run! 'day03) diff --git a/src/day04.lisp b/src/day04.lisp index 395dea3..cb8916f 100644 --- a/src/day04.lisp +++ b/src/day04.lisp @@ -1,5 +1,6 @@ (asdf:load-system :adventofcode2020) (in-package #:adventofcode2020) +(named-readtables:in-readtable fn-reader) (defun parse-passport (str-list) (flet ((parser (str) @@ -15,23 +16,23 @@ (funcall pred value)))) (defparameter *required-field-tests* - (list 'byr (four-digits-test (fn* (<= 1920 _ 2002))) - 'iyr (four-digits-test (fn* (<= 2010 _ 2020))) - 'eyr (four-digits-test (fn* (<= 2020 _ 2030))) - 'hgt (fn* (cl-ppcre:register-groups-bind - ((#'parse-integer value) unit) ("^([0-9]+)(in|cm)$" _) - (cond - ((string= unit "in") (<= 59 value 76)) - ((string= unit "cm") (<= 150 value 193)) - (t nil)))) - 'hcl (fn* (cl-ppcre:scan "^#[0-9a-f]{6}$" _)) - 'ecl (fn* (cl-ppcre:scan "^(amb|blu|brn|gry|grn|hzl|oth)$" _)) - 'pid (fn* (cl-ppcre:scan "^[0-9]{9}$" _)) + (list 'byr (four-digits-test λ(<= 1920 _ 2002)) + 'iyr (four-digits-test λ(<= 2010 _ 2020)) + 'eyr (four-digits-test λ(<= 2020 _ 2030)) + 'hgt λ(cl-ppcre:register-groups-bind + ((#'parse-integer value) unit) ("^([0-9]+)(in|cm)$" _) + (cond + ((string= unit "in") (<= 59 value 76)) + ((string= unit "cm") (<= 150 value 193)) + (t nil))) + 'hcl λ(cl-ppcre:scan "^#[0-9a-f]{6}$" _) + 'ecl λ(cl-ppcre:scan "^(amb|blu|brn|gry|grn|hzl|oth)$" _) + 'pid λ(cl-ppcre:scan "^[0-9]{9}$" _) 'cid (constantly t))) (defun simple-validate-passport (passport) (let ((required-fields '(byr iyr eyr hgt hcl ecl pid))) - (every #'identity (mapcar (fn* (assoc _ passport)) required-fields)))) + (every #'identity (mapcar λ(assoc _ passport) required-fields)))) (defun complex-validate-passport (passport) (flet ((check (pair) diff --git a/src/dayNN.lisp b/src/dayNN.lisp index 8b16cc7..bd53ef0 100644 --- a/src/dayNN.lisp +++ b/src/dayNN.lisp @@ -1,5 +1,6 @@ (asdf:load-system :adventofcode2020) (in-package #:adventofcode2020) +(named-readtables:in-readtable fn-reader) (day 00 input (let ((lines (list-from input))) diff --git a/src/utilities.lisp b/src/utilities.lisp index ab6c3e8..91e74d1 100644 --- a/src/utilities.lisp +++ b/src/utilities.lisp @@ -6,7 +6,7 @@ (split "\\n" str)) (defun int-list-from (str) - (mapcar #'parse-integer (split "\\n" str))) + (mapcar #'parse-integer (list-from str))) (defun cartesian-product (A B &rest C) (let ((helper (lambda (X Y)