blob: bb301019595b14d273d1fec0cc3bfe323fb0cea2 (
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
|
#lang racket
(require "../lib/utils.rkt")
(define lines
(call-with-input-file "input"
(λ (fp) (get-lines fp))))
(define mat (list->vector lines))
(define (char-at x y)
(string-ref (vector-ref mat y) x))
(define (set-mat! x y c)
(string-set! (vector-ref mat y) x c))
(define (move-stone x1 y1 x2 y2)
(define t (char-at x1 y1))
(set-mat! x1 y1 (char-at x2 y2))
(set-mat! x2 y2 t))
(define (find-new-pos x y)
(let loop ((new-y y))
(if (or (= new-y 0)
(not (char=? #\. (char-at x (- new-y 1)))))
new-y
(loop (- new-y 1)))))
(define height (vector-length mat))
(define width (string-length (vector-ref mat 0)))
(define (tilt)
(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)
(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)
|