blob: fcdf642b7bcbcf3c058e65e782c26de01c73d51c (
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
|
(library (lib)
(export solve-part1 solve-part2)
(import (chezscheme))
; Rotate contents of `ilist' to the right `times' times
(define (list-rotate ilist times)
(let loop ((xs ilist) (n times))
(if (= n 0)
xs
(loop (append (cdr xs) (list (car xs))) (- n 1)))))
(define (solve-puzzle chars chars-rot)
(fold-left
(lambda (accum c1 c2)
; Does `k'th char in list match with `k'th char in rotated list?
(if (char=? c1 c2)
; Assume `c1' is a digit, convert it to integer in [0,9]
(+ accum (string->number (string c1)))
accum))
0 chars chars-rot))
; Compare each char to its right neighbour in circular list
(define (solve-part1 str)
(let* ((chars (string->list str))
(chars-rot (list-rotate chars 1)))
(solve-puzzle chars chars-rot)))
; Compare each char to char halfway across circular list
(define (solve-part2 str)
(let* ((chars (string->list str))
(chars-rot (list-rotate chars (div (length chars) 2))))
(solve-puzzle chars chars-rot)))
)
|