Day 23: refactor.
This commit is contained in:
parent
c57e57079c
commit
6d6e175548
33
src/23.rkt
33
src/23.rkt
|
@ -14,35 +14,6 @@
|
||||||
(define packets
|
(define packets
|
||||||
(build-vector 50 (λ (_) (make-queue))))
|
(build-vector 50 (λ (_) (make-queue))))
|
||||||
|
|
||||||
(define part1
|
|
||||||
(let/cc k
|
|
||||||
(let loop ()
|
|
||||||
(for ([i (range 0 50)])
|
|
||||||
(let ([st (vector-ref network i)]
|
|
||||||
[input (vector-ref packets i)])
|
|
||||||
(type-case state st
|
|
||||||
[in (resume)
|
|
||||||
(if (queue-empty? input)
|
|
||||||
(vector-set! network i (resume -1))
|
|
||||||
(let* ([x (dequeue! input)]
|
|
||||||
[y (dequeue! input)]
|
|
||||||
[st (resume-with-input (resume x) y)])
|
|
||||||
(vector-set! network i st)))]
|
|
||||||
[out (j resume)
|
|
||||||
(let*-values ([(x st) (resume-with-output (resume))]
|
|
||||||
[(y st) (resume-with-output st)])
|
|
||||||
(when (= j 255) (k y))
|
|
||||||
(enqueue! (vector-ref packets j) x)
|
|
||||||
(enqueue! (vector-ref packets j) y)
|
|
||||||
(vector-set! network i st))]
|
|
||||||
[halt (_) (error "Unexpected program state.")])))
|
|
||||||
(loop))))
|
|
||||||
|
|
||||||
(set! network
|
|
||||||
(build-vector 50 (λ (n) (resume-with-input (exec input) n))))
|
|
||||||
|
|
||||||
(collect-garbage 'major)
|
|
||||||
|
|
||||||
(define (waiting? st)
|
(define (waiting? st)
|
||||||
(type-case state st
|
(type-case state st
|
||||||
[in (_) #t]
|
[in (_) #t]
|
||||||
|
@ -71,6 +42,8 @@
|
||||||
[(y st) (resume-with-output st)])
|
[(y st) (resume-with-output st)])
|
||||||
(if (= j 255)
|
(if (= j 255)
|
||||||
(begin
|
(begin
|
||||||
|
(when (= NATy -1)
|
||||||
|
(printf "Part 1: ~a\n" y))
|
||||||
(set! NATx x)
|
(set! NATx x)
|
||||||
(set! NATy y))
|
(set! NATy y))
|
||||||
(begin
|
(begin
|
||||||
|
@ -88,4 +61,4 @@
|
||||||
(enqueue! addr0 NATy)))
|
(enqueue! addr0 NATy)))
|
||||||
(loop)))))
|
(loop)))))
|
||||||
|
|
||||||
(show-solution part1 part2)
|
(printf "Part 2: ~a\n" part2)
|
Loading…
Reference in New Issue