diff options
| author | Mistivia <i@mistivia.com> | 2024-03-13 22:53:38 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-03-13 22:53:38 +0800 |
| commit | 50031d2e66694dae5b445367353821e9ceca4a70 (patch) | |
| tree | 5f47530be54a95b594c3fb1306b05846095ef620 /14/1.rkt | |
| parent | b47d2e0abd1adf3ade40062f6dbb22d7665f076e (diff) | |
refactor using do loop
Diffstat (limited to '14/1.rkt')
| -rw-r--r-- | 14/1.rkt | 40 |
1 files changed, 14 insertions, 26 deletions
@@ -30,34 +30,22 @@ (define width (string-length (vector-ref mat 0))) (define (tilt) - (let loop ((y 0)) - (if (>= y height) - (void) - (let () - (let loop1 ((x 0)) - (if (>= x width) - (void) - (let () - (define c (char-at x y)) - (if (char=? #\O c) - (let () - (define new-y (find-new-pos x y)) - (move-stone x y x new-y) - (loop1 (+ 1 x))) - (loop1 (+ 1 x)))))) - (loop (+ 1 y)))))) + (do ((y 0 (+ y 1))) + ((>= y height) (void)) + (do ((x 0 (+ x 1))) + ((>= x width) (void)) + (when (char=? #\O (char-at x y)) + (move-stone x y x (find-new-pos x y)))))) (define (count) - (let loop ((x 0) (y 0) (sum 0)) - (if (>= y height) - sum - (if (>= x width) - (loop 0 (+ 1 y) sum) - (let () - (define c (char-at x y)) - (if (char=? c #\O) - (loop (+ 1 x) y (+ sum (- height y))) - (loop (+ 1 x) y sum))))))) + (define sum 0) + (do ((y 0 (+ y 1))) + ((>= y height) (void)) + (do ((x 0 (+ x 1))) + ((>= x width) (void)) + (when (char=? #\O (char-at x y)) + (set! sum (+ sum (- height y)))))) + sum) (tilt) (count)
\ No newline at end of file |
