Skip to content

Commit a92676e

Browse files
committed
Add day15
1 parent de82f95 commit a92676e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

2020/day15.rkt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#lang racket
2+
3+
(define INPUT (map string->number (string-split "2,20,0,4,1,17" ",")))
4+
5+
(define (initialize nums)
6+
(for/hash ([n (in-list nums)]
7+
[i (in-range (length nums))])
8+
(values n i)))
9+
10+
(define (nth-spoken input n)
11+
(for/fold ([spoken-nums (initialize (drop-right input 1))]
12+
[just-said (last input)]
13+
#:result just-said)
14+
([turn (in-range (length input) n)])
15+
(define to-speak
16+
(match (hash-ref spoken-nums just-said #f)
17+
[#f 0]
18+
[when-spoke (- (sub1 turn) when-spoke)]))
19+
(values (hash-set spoken-nums just-said (sub1 turn)) to-speak)))
20+
21+
(nth-spoken INPUT 2020)
22+
(nth-spoken INPUT 30000000)

0 commit comments

Comments
 (0)