1.rkt 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #lang racket
  2. (require "../lib/utils.rkt")
  3. (define lines
  4. (call-with-input-file "input"
  5. (λ (fp) (get-lines fp))))
  6. (define patterns (split-list-by "" lines))
  7. (define (find-reflection lines)
  8. (define lines-vec (list->vector lines))
  9. (define len (vector-length lines-vec))
  10. (define (find-first)
  11. (let loop ((i 0) (ret '()))
  12. (if (>= i (- len 1))
  13. (reverse ret)
  14. (if (string=? (vector-ref lines-vec i)
  15. (vector-ref lines-vec (+ i 1)))
  16. (loop (+ 1 i) (cons i ret))
  17. (loop (+ 1 i) ret)))))
  18. (define (count start)
  19. (let loop ((cnt 1) (i start))
  20. (if (or (< i 0)
  21. (>= (+ start cnt) len))
  22. (+ start 1)
  23. (if (string=? (vector-ref lines-vec i)
  24. (vector-ref lines-vec (+ start cnt)))
  25. (loop (+ 1 cnt) (- i 1))
  26. 0))))
  27. (define start (find-first))
  28. (if (null? start)
  29. 0
  30. (apply max (map count start))))
  31. (define (score pattern)
  32. (+ (* 100 (find-reflection pattern))
  33. (find-reflection (transpose-list pattern))))
  34. (apply + (map score patterns))