diff options
| author | Mistivia <i@mistivia.com> | 2024-02-25 20:16:03 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-02-25 20:16:03 +0800 |
| commit | b3aa3d7d85887be28230e91674f9903a0c121df7 (patch) | |
| tree | 59d24a2cf19a8d24e28b1e0bb98d7b41f0fc2abe | |
| parent | 515ecbf5a20a8e432a998b0a543bedcb1bd64cf4 (diff) | |
format
| -rw-r--r-- | 10/1.rkt | 52 | ||||
| -rw-r--r-- | 10/2.rkt | 80 |
2 files changed, 66 insertions, 66 deletions
@@ -16,12 +16,12 @@ (define (find-start) (let loop ((x 0) (y 0)) (if (>= y height) - #f - (if (>= x width) - (loop 0 (+ y 1)) - (if (char=? #\S (char-at x y)) - (cons x y) - (loop (+ x 1) y)))))) + #f + (if (>= x width) + (loop 0 (+ y 1)) + (if (char=? #\S (char-at x y)) + (cons x y) + (loop (+ x 1) y)))))) (define start (find-start)) @@ -37,18 +37,18 @@ (define down (cons (car start) (+ (cdr start) 1))) (define left (cons (- (car start) 1) (cdr start))) (define right (cons (+ (car start) 1) (cdr start))) - (cond ((and (valid-pos? up) - (member (char-at (car up) (cdr up)) (list #\| #\F #\7))) - up) - ((and (valid-pos? down) - (member (char-at (car down) (cdr down)) (list #\| #\L #\J ))) - down) - ((and (valid-pos? left) - (member (char-at (car left) (cdr left)) (list #\- #\F #\L ))) - left) - ((and (valid-pos? right) - (member (char-at (car right) (cdr right)) (list #\- #\7 #\J))) - right)))) + (cond ((and (valid-pos? up) + (member (char-at (car up) (cdr up)) (list #\| #\F #\7))) + up) + ((and (valid-pos? down) + (member (char-at (car down) (cdr down)) (list #\| #\L #\J ))) + down) + ((and (valid-pos? left) + (member (char-at (car left) (cdr left)) (list #\- #\F #\L ))) + left) + ((and (valid-pos? right) + (member (char-at (car right) (cdr right)) (list #\- #\7 #\J))) + right)))) (define (next cur recent) (define up (cons (car cur) (- (cdr cur) 1))) @@ -65,19 +65,19 @@ ((char=? c #\J) (list up left)))) (let loop ((cur possible-pos)) (if (not (equal? (car cur) recent)) - (car cur) - (loop (cdr cur))))) + (car cur) + (loop (cdr cur))))) (define (simulate) (let loop ((i 0) (cur first-pos) (recent start)) (if (equal? cur start) - i - (let () - (define next-loc (next cur recent)) - (loop (+ i 1) next-loc cur))))) + i + (let () + (define next-loc (next cur recent)) + (loop (+ i 1) next-loc cur))))) (define loop-length (simulate)) (if (= 0 (modulo loop-length 2)) - (/ loop-length 2) - (+ 1 (quotient loop-length 2))) + (/ loop-length 2) + (+ 1 (quotient loop-length 2))) @@ -16,12 +16,12 @@ (define (find-start) (let loop ((x 0) (y 0)) (if (>= y height) - #f - (if (>= x width) - (loop 0 (+ y 1)) - (if (char=? #\S (char-at x y)) - (cons x y) - (loop (+ x 1) y)))))) + #f + (if (>= x width) + (loop 0 (+ y 1)) + (if (char=? #\S (char-at x y)) + (cons x y) + (loop (+ x 1) y)))))) (define start (find-start)) @@ -37,18 +37,18 @@ (define down (cons (car start) (+ (cdr start) 1))) (define left (cons (- (car start) 1) (cdr start))) (define right (cons (+ (car start) 1) (cdr start))) - (cond ((and (valid-pos? up) - (member (char-at (car up) (cdr up)) (list #\| #\F #\7))) - up) - ((and (valid-pos? down) - (member (char-at (car down) (cdr down)) (list #\| #\L #\J ))) - down) - ((and (valid-pos? left) - (member (char-at (car left) (cdr left)) (list #\- #\F #\L ))) - left) - ((and (valid-pos? right) - (member (char-at (car right) (cdr right)) (list #\- #\7 #\J))) - right)))) + (cond ((and (valid-pos? up) + (member (char-at (car up) (cdr up)) (list #\| #\F #\7))) + up) + ((and (valid-pos? down) + (member (char-at (car down) (cdr down)) (list #\| #\L #\J ))) + down) + ((and (valid-pos? left) + (member (char-at (car left) (cdr left)) (list #\- #\F #\L ))) + left) + ((and (valid-pos? right) + (member (char-at (car right) (cdr right)) (list #\- #\7 #\J))) + right)))) (define (next cur recent) (define up (cons (car cur) (- (cdr cur) 1))) @@ -65,8 +65,8 @@ ((char=? c #\J) (list up left)))) (let loop ((cur possible-pos)) (if (not (equal? (car cur) recent)) - (car cur) - (loop (cdr cur))))) + (car cur) + (loop (cdr cur))))) (define pipes (make-hash)) @@ -74,22 +74,22 @@ (let loop ((i 0) (cur first-pos) (recent start)) (hash-set! pipes cur '()) (if (equal? cur start) - i - (let () - (define next-loc (next cur recent)) - (loop (+ i 1) next-loc cur))))) + i + (let () + (define next-loc (next cur recent)) + (loop (+ i 1) next-loc cur))))) (define (mark-junk) (let loop ((x 0) (y 0)) (if (>= y height) - (void) - (if (>= x width) - (loop 0 (+ y 1)) - (let () - (when (and (not (hash-has-key? pipes (cons x y))) - (not (char=? #\. (char-at x y)))) - (string-set! (vector-ref diagram y) x #\.)) - (loop (+ 1 x) y)))))) + (void) + (if (>= x width) + (loop 0 (+ y 1)) + (let () + (when (and (not (hash-has-key? pipes (cons x y))) + (not (char=? #\. (char-at x y)))) + (string-set! (vector-ref diagram y) x #\.)) + (loop (+ 1 x) y)))))) (let () (simulate) @@ -99,14 +99,14 @@ (define (ray-cast-find s) (let loop ((i 0) (flag #f) (ret 0)) (if (>= i width) - ret - (let () - (define char (string-ref s i)) - (if (and flag (char=? char #\.)) - (loop (+ 1 i) flag (+ 1 ret)) - (if (member char (list #\| #\F #\7)) - (loop (+ 1 i) (not flag) ret) - (loop (+ 1 i) flag ret))))))) + ret + (let () + (define char (string-ref s i)) + (if (and flag (char=? char #\.)) + (loop (+ 1 i) flag (+ 1 ret)) + (if (member char (list #\| #\F #\7)) + (loop (+ 1 i) (not flag) ret) + (loop (+ 1 i) flag ret))))))) (apply + (map ray-cast-find (vector->list diagram))) |
