aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 224961185e1eedf9a6d269834a99936dbda5837b (patch)
parent 01c817ca9438e806c37f9e6ed1a458ba0067650e
Author: Alex Karle <alex@alexkarle.com>
Date:   Sun, 11 Dec 2022 13:55:44 -0500

day9: Refactor solution to print both pt1 and pt2

Diffstat:
M2022/09/sol.scm | 22+++++++++++++---------
1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/2022/09/sol.scm b/2022/09/sol.scm @@ -48,7 +48,7 @@ (dx (- xh xt)) (dy (- yh yt))) (list (+ xt (unitize dx)) (+ yt (unitize dy)))))) -(define (move-all dir knots) +(define (move-all dir knots tail-locs) ; only the head moves according to dir, everything else follows (let ((new-head (move (car knots) dir))) (let loop ((nh new-head) (tails (cdr knots))) @@ -64,22 +64,26 @@ ;; NOTE: at first I was purely functional with cons-ing the ;; the h2,t2 to a history and then pulling unique t2's later ;; but that took 10s and it takes .2s to use a hash table :shrug: -(define tail-locs (make-hash-table)) -(define (play-out dirs) - (let loop ((knots (n-of-v 10 '(0 0))) (dirs dirs)) +(define (play-out dirs knots tail-locs) + (let loop ((knots knots) (dirs dirs)) (if (null? dirs) 'done - (let ((ks (move-all (car dirs) knots))) + (let ((ks (move-all (car dirs) knots tail-locs))) ; (newline) ; (plot-state ks) ; (newline) (loop ks (cdr dirs)))))) (define (main args) - (let* ((dirs (flatten (map parse-line (read-lines))))) - (hash-table-set! tail-locs '(0 0) 'start) - (play-out dirs) - (print (hash-table-size tail-locs)))) + (let* ((dirs (flatten (map parse-line (read-lines)))) + (tail-locs-1 (make-hash-table)) + (tail-locs-2 (make-hash-table))) + (hash-table-set! tail-locs-1 '(0 0) 'start) + (hash-table-set! tail-locs-2 '(0 0) 'start) + (play-out dirs (n-of-v 2 '(0 0)) tail-locs-1) + (play-out dirs (n-of-v 10 '(0 0)) tail-locs-2) + (print (hash-table-size tail-locs-1)) + (print (hash-table-size tail-locs-2)))) ;; helpful for debugging!