blob: 1f3ca37da89215afe98626d888f558fede711ae0 (
plain) (
blame)
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
|
#lang racket
(require racket/set)
(define (scan f i l)
(if (null? l)
l
(let ((v (f i (car l))))
(cons v (scan f v (cdr l))))))
(define/match (move-after head tail)
[((list hx hy) (list tx ty))
(if (and (<= (abs (- hx tx)) 1)
(<= (abs (- hy ty)) 1))
tail
(list (+ tx (sgn (- hx tx)))
(+ ty (sgn (- hy ty)))))])
(for/list ([rope-length '(1 9)])
(let ([visited (mutable-set)]
[head '(0 0)]
[tail (make-list rope-length '(0 0))])
(set-add! visited (last tail))
(for ([line (file->lines "day09")])
(let* ([parts (string-split line " ")]
[dir (string->symbol (first parts))]
[n (string->number (second parts))])
(for ([_ (in-range n)])
(set! head (map + head (case dir
[(L) '(-1 0)]
[(R) '(1 0)]
[(U) '(0 1)]
[(D) '(0 -1)])))
(set! tail (scan move-after head tail))
(set-add! visited (last tail)))))
(set-count visited)))
; 6494
; 2691
|