From acf4a35309c761f08cb003462f4b3b71f4887157 Mon Sep 17 00:00:00 2001 From: MsK` Date: Mon, 13 Dec 2021 22:53:23 +0100 Subject: [PATCH] shorter, using `values` and `apply` --- 13-transparent-origami/code.scm | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/13-transparent-origami/code.scm b/13-transparent-origami/code.scm index c497a1c..b10d11a 100644 --- a/13-transparent-origami/code.scm +++ b/13-transparent-origami/code.scm @@ -35,21 +35,16 @@ ; mirrors coordinates along given axis and line/column ; returns a new list of coordinates (define (fold coords axis-position) - (let* [(axis (car axis-position)) - (position (cadr axis-position)) - (position*2 (* 2 position))] - (define (mirror-x x y) (list (if (> x position) (- position*2 x) x) y)) - (define (mirror-y x y) (list x (if (> y position) (- position*2 y) y))) - (let [(mirror (if (char=? axis #\x) mirror-x mirror-y))] - (define (folder output coord) - (let* [(x (car coord)) - (y (cadr coord)) - (mirrored (mirror x y))] - ; add mirrored coord only if it wasn't in output already - (if (member mirrored output) - output - (cons (mirror x y) output)))) - (fold-left folder '() coords)))) + (let-values [((axis position) (apply values axis-position))] + (let [(position*2 (* position 2))] + (define (mirror-x x y) (list (if (> x position) (- position*2 x) x) y)) + (define (mirror-y x y) (list x (if (> y position) (- position*2 y) y))) + (let [(mirror (if (char=? axis #\x) mirror-x mirror-y))] + (define (folder output coord) + (let* [(mirrored (apply mirror coord))] + ; add mirrored coord only if it wasn't in output already + (if (member mirrored output) output (cons mirrored output)))) + (fold-left folder '() coords))))) (call-with-input-file "input"