day 15: improve solution spaghettiness somewhat
This commit is contained in:
parent
4888c7dd40
commit
8b17fe1d26
19
15.rkt
19
15.rkt
|
@ -6,20 +6,21 @@
|
||||||
|
|
||||||
(define (naive-solution input limit)
|
(define (naive-solution input limit)
|
||||||
(define seen-numbers (make-hash))
|
(define seen-numbers (make-hash))
|
||||||
(define prev #f)
|
(define prev
|
||||||
(for ([num (in-list input)] [i (in-naturals)])
|
(for/last ([num (in-list input)] [i (in-naturals)])
|
||||||
(hash-set! seen-numbers num i)
|
(hash-set! seen-numbers num i)
|
||||||
(set! prev num))
|
num))
|
||||||
|
;; delay "seeing" numbers by one iteration
|
||||||
(hash-remove! seen-numbers prev)
|
(hash-remove! seen-numbers prev)
|
||||||
|
|
||||||
(for ([i (in-range (length input) limit)])
|
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
||||||
(define new-val
|
(define new-val
|
||||||
(cond
|
(match (hash-ref seen-numbers prev #f)
|
||||||
[(hash-has-key? seen-numbers prev) (sub1 (- i (hash-ref seen-numbers prev)))]
|
[#f 0]
|
||||||
[else 0]))
|
[idx (- i 1 idx)]))
|
||||||
|
;; _now_ we've "seen" it, but it happened on the last turn
|
||||||
(hash-set! seen-numbers prev (sub1 i))
|
(hash-set! seen-numbers prev (sub1 i))
|
||||||
(set! prev new-val))
|
new-val))
|
||||||
prev)
|
|
||||||
|
|
||||||
(define part1 #{naive-solution % 2020})
|
(define part1 #{naive-solution % 2020})
|
||||||
(define part2 #{naive-solution % 30000000})
|
(define part2 #{naive-solution % 30000000})
|
||||||
|
|
Loading…
Reference in New Issue