blob: d117cce168b9c2f507b9c2a4d371490ce090d839 (
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
|
(define-module (pshiav)
#:use-module (srfi srfi-1)
#:export (pshiav
add-coordinates
direction->pair))
(define (pshiav str num)
"Given string of cardinal directions STR,
calculates the number of visited places on
a grid.
NUM specifies the number of visitors. Will
always be at least one."
(set! num (max num 1))
(let ((visitors (map (lambda (x)
(add-coordinates))
(iota num))))
(let loop ((i 0) (lst (map direction->pair
(string->list str))))
(if (null? lst)
(length (delete-duplicates
(apply append (map (lambda (visitor)
(visitor '(0 . 0)))
visitors))))
(begin
((list-ref visitors i) (car lst))
(loop (modulo (1+ i) (length visitors))
(cdr lst)))))))
(define (add-coordinates)
(let ((hist '((0 . 0))))
(lambda dir
"Given cons pair DIR, increments the previous
position, and returns HIST, a history of visited
positions."
(set! hist (cons
(cons (+ (caar dir)
(caar hist))
(+ (cdar dir)
(cdar hist)))
hist))
hist)))
(define (direction->pair char)
"Given a character representing one of the four
cardinal directions, return a pair representing
a change in coordinates."
(cond
((char=? char #\^) '(0 . 1))
((char=? char #\>) '(1 . 0))
((char=? char #\v) '(0 . -1))
((char=? char #\<) '(-1 . 0))
(#t '(0 . 0))))
|