1.rkt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #lang racket
  2. (require "../lib/utils.rkt")
  3. (define lines
  4. (call-with-input-file "input"
  5. (λ (fp) (get-lines fp))))
  6. (define mat (list->vector lines))
  7. (define (char-at x y)
  8. (string-ref (vector-ref mat y) x))
  9. (define (set-mat! x y c)
  10. (string-set! (vector-ref mat y) x c))
  11. (define (move-stone x1 y1 x2 y2)
  12. (define t (char-at x1 y1))
  13. (set-mat! x1 y1 (char-at x2 y2))
  14. (set-mat! x2 y2 t))
  15. (define (find-new-pos x y)
  16. (let loop ((new-y y))
  17. (if (or (= new-y 0)
  18. (not (char=? #\. (char-at x (- new-y 1)))))
  19. new-y
  20. (loop (- new-y 1)))))
  21. (define height (vector-length mat))
  22. (define width (string-length (vector-ref mat 0)))
  23. (define (tilt)
  24. (let loop ((y 0))
  25. (if (>= y height)
  26. (void)
  27. (let ()
  28. (let loop1 ((x 0))
  29. (if (>= x width)
  30. (void)
  31. (let ()
  32. (define c (char-at x y))
  33. (if (char=? #\O c)
  34. (let ()
  35. (define new-y (find-new-pos x y))
  36. (move-stone x y x new-y)
  37. (loop1 (+ 1 x)))
  38. (loop1 (+ 1 x))))))
  39. (loop (+ 1 y))))))
  40. (define (count)
  41. (let loop ((x 0) (y 0) (sum 0))
  42. (if (>= y height)
  43. sum
  44. (if (>= x width)
  45. (loop 0 (+ 1 y) sum)
  46. (let ()
  47. (define c (char-at x y))
  48. (if (char=? c #\O)
  49. (loop (+ 1 x) y (+ sum (- height y)))
  50. (loop (+ 1 x) y sum)))))))
  51. (tilt)
  52. (count)