diff options
author | Oscar Najera <hi@oscarnajera.com> | 2023-01-16 00:50:09 +0100 |
---|---|---|
committer | Oscar Najera <hi@oscarnajera.com> | 2023-01-16 01:08:40 +0100 |
commit | f63d22c9e7e75332b9cc01d122a9138673e88801 (patch) | |
tree | c14a4905650a5e7b44b439769c47c3823eb67492 /AoC2022 | |
parent | cfeb6eaf52cfd6dd957c5716877e8ac0f4bea3a1 (diff) | |
download | scratch-f63d22c9e7e75332b9cc01d122a9138673e88801.tar.gz scratch-f63d22c9e7e75332b9cc01d122a9138673e88801.tar.bz2 scratch-f63d22c9e7e75332b9cc01d122a9138673e88801.zip |
cross between faces as composed transformation
This is instead of determining manually the transformations
can be deduced visually from the way the field composes.
Diffstat (limited to 'AoC2022')
-rw-r--r-- | AoC2022/22/solver.lisp | 88 |
1 files changed, 54 insertions, 34 deletions
diff --git a/AoC2022/22/solver.lisp b/AoC2022/22/solver.lisp index b3ea61d..d0d99e2 100644 --- a/AoC2022/22/solver.lisp +++ b/AoC2022/22/solver.lisp @@ -133,39 +133,57 @@ (make-state :x-pos (+ x (* x-face face-length)) :y-pos (+ y (* y-face face-length)) :direction direction))) +;; The one direction from each face +;; The transitions are symetric on the position operation +;; ↱→↴ +;; ↑↱1→↴ +;;⬐234↴| +;;||↳56↲ +;;|↳→⬏↑ +;;↳→→→⬏ + (defun face-jump (face direction fl) "fl is face-length" - (ecase face - (1 (case direction - (> (list 6 '< (lambda (_ y) (declare (ignore _)) (list (1- fl) (- fl y 1))))) - (v (list 4 'v (lambda (x _) (declare (ignore _)) (list x 0)))) - (< (list 3 'v (lambda (_ y) (declare (ignore _)) (list y 0)))) - (^ (list 2 'v (lambda (x _) (declare (ignore _)) (list (- fl x 1) 0)))))) - (2 (case direction - (> (list 3 '> (lambda (_ y) (declare (ignore _)) (list 0 y)))) - (v (list 5 '^ (lambda (x _) (declare (ignore _)) (list (- fl x 1) (1- fl))))) - (< (list 6 '^ (lambda (_ y) (declare (ignore _)) (list (- fl y 1) (1- fl))))) - (^ (list 1 'v (lambda (x _) (declare (ignore _)) (list (- fl x 1) 0)))))) - (3 (case direction - (> (list 4 '> (lambda (_ y) (declare (ignore _)) (list 0 y)))) - (v (list 5 '> (lambda (x _) (declare (ignore _)) (list 0 (- fl x 1))))) - (< (list 2 '< (lambda (_ y) (declare (ignore _)) (list (1- fl) y)))) - (^ (list 1 '> (lambda (x _) (declare (ignore _)) (list 0 x)))))) - (4 (case direction - (> (list 6 'v (lambda (_ y) (declare (ignore _)) (list (- fl y 1) 0)))) - (v (list 5 'v (lambda (x _) (declare (ignore _)) (list x 0)))) - (< (list 3 '< (lambda (_ y) (declare (ignore _)) (list (1- fl) y)))) - (^ (list 1 '^ (lambda (x _) (declare (ignore _)) (list x (1- fl))))))) - (5 (case direction - (> (list 6 '> (lambda (_ y) (declare (ignore _)) (list 0 y)))) - (v (list 2 '^ (lambda (x _) (declare (ignore _)) (list (- fl x 1) (1- fl))))) - (< (list 3 '^ (lambda (_ y) (declare (ignore _)) (list (- fl y 1) (1- fl))))) - (^ (list 4 '^ (lambda (x _) (declare (ignore _)) (list x (1- fl))))))) - (6 (case direction - (> (list 1 '< (lambda (_ y) (declare (ignore _)) (list (1- fl) (- fl y 1))))) - (v (list 2 '> (lambda (x _) (declare (ignore _)) (list 0 (- fl x 1))))) - (< (list 5 '< (lambda (_ y) (declare (ignore _)) (list (1- fl) y)))) - (^ (list 4 '< (lambda (x _) (declare (ignore _)) (list (1- fl) (- fl x 1))))))))) + (flet ((rot-ccw (x y) (list y (- fl 1 x))) + (rot-cw (x y) (list (- fl 1 y) x)) + (cross-x (x y) (list (- fl 1 x) y)) + (cross-y (x y) (list x (- fl 1 y))) + (compose (&rest funcs) + (reduce (lambda (f g) + (lambda (&rest args) + (apply f (apply g args)))) + funcs))) + (ecase face + (1 (case direction + (> (list 6 '< (compose #'rot-cw #'rot-cw #'cross-x))) + (v (list 4 'v #'cross-y)) + (< (list 3 'v (compose #'rot-ccw #'cross-x))) + (^ (list 2 'v (compose #'rot-cw #'rot-cw #'cross-y))))) + (2 (case direction + (> (list 3 '> #'cross-x)) + (v (list 5 '^ (compose #'rot-ccw #'rot-ccw #'cross-y))) + (< (list 6 '^ (compose #'rot-ccw #'rot-ccw #'rot-ccw #'cross-x))) + (^ (list 1 'v (compose #'rot-cw #'rot-cw #'cross-y))))) + (3 (case direction + (> (list 4 '> #'cross-x)) + (v (list 5 '> (compose #'rot-ccw #'cross-y))) + (< (list 2 '< #'cross-x)) + (^ (list 1 '> (compose #'rot-cw #'cross-y))))) + (4 (case direction + (> (list 6 'v (compose #'rot-cw #'cross-x))) + (v (list 5 'v #'cross-y)) + (< (list 3 '< #'cross-x)) + (^ (list 1 '^ #'cross-y)))) + (5 (case direction + (> (list 6 '> #'cross-x)) + (v (list 2 '^ (compose #'rot-cw #'rot-cw #'cross-y))) + (< (list 3 '^ (compose #'rot-cw #'cross-x))) + (^ (list 4 '^ #'cross-y)))) + (6 (case direction + (> (list 1 '< (compose #'rot-ccw #'rot-ccw #'cross-x))) + (v (list 2 '> (compose #'rot-cw #'rot-cw #'rot-cw #'cross-y))) + (< (list 5 '< #'cross-x)) + (^ (list 4 '< (compose #'rot-ccw #'cross-y)))))))) (defun cube-step (state face-length) (destructuring-bind (face x-coord y-coord) (reduced-coords state face-length) @@ -215,8 +233,10 @@ origin #'advance-fn)))) ;; around corners (dotimes (i 6) - (fiveam:is (check (place-state (1+ i) 0 0 '^ face-length) - '(1 left 1 left 1 left)))) + (let ((origin (place-state (1+ i) 0 0 '^ face-length))) + (fiveam:is (equalp origin + (traverse field '(1 left 1 left 1 left) + origin #'advance-fn))))) ;; walk all dirs straight (dolist (dir '(> v < ^)) (fiveam:is (check (place-state 1 2 1 dir face-length) '(16))))))) @@ -225,7 +245,7 @@ (fiveam:is (= 6032 (solver "eg-in" #'wrap-step))) (fiveam:is (= 159034 (solver "input" #'wrap-step))) ;part 2 - (fiveam:is (= 5031 (solver "eg-in" (lambda (state field) (cube-step state 4)))))) + (fiveam:is (= 5031 (solver "eg-in" (lambda (state field) (declare (ignore field)) (cube-step state 4)))))) ;; (solver "input" (lambda (state field) (cube-step state 50))) |