diff options
| author | Mistivia <i@mistivia.com> | 2024-12-04 15:51:40 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-12-04 15:51:40 +0800 |
| commit | 45d88908e3fca797db359129fbf9a752dee7b8fa (patch) | |
| tree | fabd5076cdf3ff002b82be6e6f634143adc486bd /04/2.scm | |
| parent | 1c64562386506550cae91b01707d66b9bf1c09f0 (diff) | |
day 4
Diffstat (limited to '04/2.scm')
| -rw-r--r-- | 04/2.scm | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/04/2.scm b/04/2.scm new file mode 100644 index 0000000..fbd5694 --- /dev/null +++ b/04/2.scm @@ -0,0 +1,59 @@ +(import (chicken string)) +(import (chicken io)) +(import (chicken sort)) +(import matchable) +(import srfi-1) +(import regex) + +(define input + (with-input-from-file "input" + (lambda () + (let loop ((ret '())) + (let ((line (read-line))) + (if (eof-object? line) + (reverse ret) + (loop (cons line ret)))))))) + +(define (transpose in) + (define cols (string-length (car in))) + (let loop ((i 0) (ret '())) + (if (>= i cols) + (reverse ret) + (loop (+ i 1) (cons (list->string (map (lambda (x) (string-ref x i)) + in)) + ret))))) + +(define (flip in) + (define (flip-row row) + (list->string (reverse (string->list row)))) + (map flip-row in)) + +(define (char-at in x y) + (string-ref (list-ref in y) x)) + +(define (check-xmas mat x y) + (and (equal? #\M (char-at mat x y)) + (equal? #\M (char-at mat x (+ 2 y))) + (equal? #\S (char-at mat (+ x 2) y)) + (equal? #\S (char-at mat (+ x 2) (+ y 2))) + (equal? #\A (char-at mat (+ x 1) (+ y 1))))) + +(define (count-mat mat) + (define count 0) + (define width (string-length (car mat))) + (define height (length mat)) + (do ((x 0 (+ 1 x))) + ((>= x (- width 2)) #f) + (do ((y 0 (+ 1 y))) + ((>= y (- height 2)) #f) + (if (check-xmas mat x y) + (set! count (+ 1 count)) + #f))) + count) + +(define mat-list + (list input (flip input) + (transpose input) (flip (transpose input)))) + +(display (apply + (map count-mat mat-list))) +(newline) |
