diff options
| author | Mistivia <i@mistivia.com> | 2024-02-20 00:42:16 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-02-20 00:42:16 +0800 |
| commit | bebb8946d29069b2acb10db71c8c9203ed4ae922 (patch) | |
| tree | 66ee2e321cbb413baaf32661195f2c734546eba0 /advent-of-code/2023/03 | |
| parent | 3a4e5447a79096d813cdaf4498b9a1e2c086b7ff (diff) | |
λ looks cool & format code
Diffstat (limited to 'advent-of-code/2023/03')
| -rw-r--r-- | advent-of-code/2023/03/1.rkt | 74 | ||||
| -rw-r--r-- | advent-of-code/2023/03/2.rkt | 82 |
2 files changed, 76 insertions, 80 deletions
diff --git a/advent-of-code/2023/03/1.rkt b/advent-of-code/2023/03/1.rkt index 2b9a1d2..5e86227 100644 --- a/advent-of-code/2023/03/1.rkt +++ b/advent-of-code/2023/03/1.rkt @@ -5,7 +5,7 @@ (define (read-input) (call-with-input-file "input" - (lambda (fp) + (λ (fp) (list->vector (get-lines fp))))) (define schema (read-input)) @@ -22,29 +22,29 @@ (define nums '()) (let loop ((i 0)) (if (>= i height) - (void) - (let () - (let loop ((j 0)) + (void) + (let () + (let loop ((j 0)) (define curline (vector-ref schema i)) (if (>= j width) - (void) - (let () - (define next 1) - (define (find-next) - (if (or (>= (+ j next) 140) - (not (char-numeric? (char-at i (+ j next))))) - (void) - (let () - (set! next (+ 1 next)) - (find-next)))) - (if (char-numeric? (char-at i j)) - (let () - (find-next) - (define value (string->number (substring curline j (+ j next)))) - (set! nums (cons (make-num i j value next) nums))) - (void)) - (loop (+ j next))))) - (loop (+ 1 i))))) + (void) + (let () + (define next 1) + (define (find-next) + (if (or (>= (+ j next) 140) + (not (char-numeric? (char-at i (+ j next))))) + (void) + (let () + (set! next (+ 1 next)) + (find-next)))) + (if (char-numeric? (char-at i j)) + (let () + (find-next) + (define value (string->number (substring curline j (+ j next)))) + (set! nums (cons (make-num i j value next) nums))) + (void)) + (loop (+ j next))))) + (loop (+ 1 i))))) (reverse nums)) (define nums (scan-nums)) @@ -55,30 +55,30 @@ (define (collect-adjacent num) (define left (if (= 0 (num 'col)) - '() - (list (char-at (num 'line) (- (num 'col) 1))))) + '() + (list (char-at (num 'line) (- (num 'col) 1))))) (define right (if (= width (+ (num 'col) (num 'length))) - '() - (list (char-at (num 'line) (+ (num 'col) (num 'length)))))) + '() + (list (char-at (num 'line) (+ (num 'col) (num 'length)))))) (define up (if (= 0 (num 'line)) - '() - (string->list - (substring (vector-ref schema (- (num 'line) 1)) - (max 0 (- (num 'col) 1)) - (min width (+ (num 'col) (num 'length) 1)))))) + '() + (string->list + (substring (vector-ref schema (- (num 'line) 1)) + (max 0 (- (num 'col) 1)) + (min width (+ (num 'col) (num 'length) 1)))))) (define down (if (= (- height 1) (num 'line)) - '() - (string->list - (substring (vector-ref schema (+ (num 'line) 1)) - (max 0 (- (num 'col) 1)) - (min width (+ (num 'col) (num 'length) 1)))))) + '() + (string->list + (substring (vector-ref schema (+ (num 'line) 1)) + (max 0 (- (num 'col) 1)) + (min width (+ (num 'col) (num 'length) 1)))))) (append left right up down)) (define (is-part-num? num) (findf is-symbol? (collect-adjacent num))) -(apply + (map (lambda (x) (x 'value)) +(apply + (map (λ (x) (x 'value)) (filter is-part-num? nums)))
\ No newline at end of file diff --git a/advent-of-code/2023/03/2.rkt b/advent-of-code/2023/03/2.rkt index 2bc8c6b..71c3221 100644 --- a/advent-of-code/2023/03/2.rkt +++ b/advent-of-code/2023/03/2.rkt @@ -5,7 +5,7 @@ (define (read-input) (call-with-input-file "input" - (lambda (fp) + (λ (fp) (list->vector (get-lines fp))))) (define schema (read-input)) @@ -22,60 +22,56 @@ (define nums '()) (let loop ((i 0)) (if (>= i height) - (void) - (let () - (let loop ((j 0)) + (void) + (let () + (let loop ((j 0)) (define curline (vector-ref schema i)) (if (>= j width) - (void) - (let () - (define next 1) - (define (find-next) - (if (or (>= (+ j next) 140) - (not (char-numeric? (char-at i (+ j next))))) - (void) - (let () - (set! next (+ 1 next)) - (find-next)))) - (if (char-numeric? (char-at i j)) - (let () - (find-next) - (define value (string->number (substring curline j (+ j next)))) - (set! nums (cons (make-num i j value next) nums))) - (void)) - (loop (+ j next))))) - (loop (+ 1 i))))) + (void) + (let () + (define next 1) + (define (find-next) + (if (or (>= (+ j next) 140) + (not (char-numeric? (char-at i (+ j next))))) + (void) + (let () + (set! next (+ 1 next)) + (find-next)))) + (if (char-numeric? (char-at i j)) + (let () + (find-next) + (define value (string->number (substring curline j (+ j next)))) + (set! nums (cons (make-num i j value next) nums))) + (void)) + (loop (+ j next))))) + (loop (+ 1 i))))) (reverse nums)) (define nums (scan-nums)) -(define (is-symbol? c) - (and (not (char-numeric? c)) - (not (char=? #\. c)))) - (define (collect-adjacent-positions num) (define (position-range line start end) (define delta (- end start)) (map list (repeat delta line) (range start end))) (define left (if (= 0 (num 'col)) - '() - (list (list (num 'line) (- (num 'col) 1))))) + '() + (list (list (num 'line) (- (num 'col) 1))))) (define right (if (= width (+ (num 'col) (num 'length))) - '() - (list (list (num 'line) (+ (num 'col) (num 'length)))))) + '() + (list (list (num 'line) (+ (num 'col) (num 'length)))))) (define up (if (= 0 (num 'line)) - '() - (position-range (- (num 'line) 1) - (max 0 (- (num 'col) 1)) - (min width (+ (num 'col) (num 'length) 1))))) + '() + (position-range (- (num 'line) 1) + (max 0 (- (num 'col) 1)) + (min width (+ (num 'col) (num 'length) 1))))) (define down (if (= (- height 1) (num 'line)) - '() - (position-range (+ (num 'line) 1) - (max 0 (- (num 'col) 1)) - (min width (+ (num 'col) (num 'length) 1))))) + '() + (position-range (+ (num 'line) 1) + (max 0 (- (num 'col) 1)) + (min width (+ (num 'col) (num 'length) 1))))) (append left right up down)) (define asterisks (make-hash)) @@ -84,11 +80,11 @@ (define adjs (collect-adjacent-positions num)) (define (mark coord) (if (not (char=? #\* (char-at (car coord) (cadr coord)))) - (void) - (let () - (when (not (hash-has-key? asterisks coord)) - (hash-set! asterisks coord '())) - (hash-set! asterisks coord (cons (num 'value) (hash-ref asterisks coord)))))) + (void) + (let () + (when (not (hash-has-key? asterisks coord)) + (hash-set! asterisks coord '())) + (hash-set! asterisks coord (cons (num 'value) (hash-ref asterisks coord)))))) (for-each mark adjs)) (for-each mark-adj-asterisk nums) |
