-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday11.rkt
67 lines (43 loc) · 1.23 KB
/
day11.rkt
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
55
56
57
58
59
60
61
62
63
64
65
66
67
#lang racket
(require "aoc.rkt" threading)
(define (step! octopi pt)
(define val (hash-ref octopi pt 99))
(when (< val 10)
(hash-update! octopi pt add1)
(when (= val 9)
(for-each (curry step! octopi) (neighbours pt 8)))))
(define (count-flashes octopi)
(for/sum ([(pt val) (in-hash octopi)]
#:when (= val 10))
(hash-set! octopi pt 0)
1))
(define (part-1 octopi)
(for/sum ([n (in-range 100)])
(for-each (curry step! octopi) (hash-keys octopi))
(count-flashes octopi)))
(define (part-2 octopi)
(for/or ([n (in-naturals 101)])
(for-each (curry step! octopi) (hash-keys octopi))
(count-flashes octopi)
(if (andmap zero? (hash-values octopi))
n
#f)))
(define (make-grid lines)
(define octopi (make-hash))
(for* ([(line y) (in-indexed lines)]
[(val x) (in-indexed line)])
(hash-set! octopi (make-rectangular x y) val))
octopi)
(define (parse filename)
(~>> filename
read-input
(map string->digits)
make-grid))
(define octopi (parse 11))
(part-1 octopi)
(part-2 octopi)
(module+ test
(require rackunit)
(define octopi (parse "11-test"))
(check-equal? (part-1 octopi) 1656)
(check-equal? (part-2 octopi) 195))