blob: fbd56949d77c1652a72fb3b44dd2d8d66918ff69 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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)
|