aboutsummaryrefslogtreecommitdiff
path: root/04/2.scm
diff options
context:
space:
mode:
Diffstat (limited to '04/2.scm')
-rw-r--r--04/2.scm59
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)