Get lambda reader macro working and fix day 03 bug
authorJack Kinsey <j.jameskinsey@gmail.com>
Sat, 5 Dec 2020 10:05:44 +0000 (05:05 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Sat, 5 Dec 2020 10:05:44 +0000 (05:05 -0500)
src/day03.lisp
src/day04.lisp
src/dayNN.lisp
src/utilities.lisp

index e708c87565b8bdf730f8f11b4e4276f90493a1b3..093654f6acfe7ba082a2582e3ae957fa3c4e52f5 100644 (file)
@@ -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)
 (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)
index 395dea36e1f7efc659859b2acf9c9ebc1f7c25d8..cb8916f414c0c6a90420d87a6ac4a9e0bfc3a46c 100644 (file)
@@ -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)
       (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)
index 8b16cc73c7fc4858b10aa302422e9ac56345bbb8..bd53ef0b636a72e493f8c5e15213a6e009ad18c1 100644 (file)
@@ -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)))
index ab6c3e8faa36eb6d59ac780c1ae7d8f6670a142b..91e74d11b1aeb5a8a6316c11562e7934f9eb0aa1 100644 (file)
@@ -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)