From 98336be2d749147e80a6a8ecb9d5f99e8b878dd5 Mon Sep 17 00:00:00 2001 From: MsK` Date: Sat, 11 Dec 2021 12:02:23 +0100 Subject: [PATCH] day 11 part 2 --- 11-dumbo-octopus/code.scm | 36 ++++++++++-------------------------- common.scm | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 26 deletions(-) diff --git a/11-dumbo-octopus/code.scm b/11-dumbo-octopus/code.scm index b160619..1bfde15 100644 --- a/11-dumbo-octopus/code.scm +++ b/11-dumbo-octopus/code.scm @@ -1,29 +1,5 @@ (load "../common.scm") -; matrix visitor -; calls function with all coordinates of matrix and value at corresponding point -; value returned by function is set at the visiting coordinate -(define (visit-matrix! matrix function) - (let [(data (matrix-data matrix)) - (width-1 (- (matrix-width matrix) 1)) - (height-1 (- (matrix-height matrix) 1))] - (let y-loop [(y 0)] - (let x-loop [(x 0)] - (let [(value (vector-ref (vector-ref data y) x))] - (vector-set! (vector-ref data y) x - (function x y value)) - (when (< x width-1) - (x-loop (+ x 1))))) - (when (< y height-1) - (y-loop (+ y 1)))))) - -; increase by 1 value at x,y in given matrix -; return that value too -(define (matrix-inc! matrix x y) - (let [(v (+ 1 (matrix-get matrix x y 0)))] - (matrix-set! matrix x y v) - v)) - (define-record-type vec2i (fields x y)) ; flash given octopus @@ -83,6 +59,14 @@ (step! matrix))) (when (< i 99) (loop (+ i 1)))) - (printf "part 1:~% ~a flashes after a hundred steps." - flash-count)))) + (printf "part 1:~% ~a flashes after a hundred steps.~%" + flash-count) + (let loop [(i 100)] + (if (= (matrix-sum matrix) 0) + (printf "part 2:~% Octopuses synchronized at step ~a.~%" i) + (begin + (step! matrix) + (loop (+ i 1)))))))) + + diff --git a/common.scm b/common.scm index 99de2ef..9f7c4d6 100644 --- a/common.scm +++ b/common.scm @@ -43,6 +43,8 @@ (< y (matrix-height matrix))) (vector-set! (vector-ref (matrix-data matrix) y) x value))) +; print matrix line by line to console +; each line will be displayed as a scheme-vector (define (matrix-print matrix) (let y-loop [(y 0)] (printf "~a~%" (vector-ref (matrix-data matrix) y)) @@ -61,3 +63,37 @@ (y-loop (cons (reverse-list->vector line) heightmap))] [(char-numeric? c) (x-loop (cons (char->number c) line))]))))) + +; matrix visitor +; calls function with all coordinates of matrix and value at corresponding point +; value returned by function is set at the visiting coordinate +(define (visit-matrix! matrix function) + (let [(data (matrix-data matrix)) + (width-1 (- (matrix-width matrix) 1)) + (height-1 (- (matrix-height matrix) 1))] + (let y-loop [(y 0)] + (let x-loop [(x 0)] + (let [(value (vector-ref (vector-ref data y) x))] + (vector-set! (vector-ref data y) x + (function x y value)) + (when (< x width-1) + (x-loop (+ x 1))))) + (when (< y height-1) + (y-loop (+ y 1)))))) + +; increase by 1 value at x,y in given matrix +; return that value too +(define (matrix-inc! matrix x y) + (let [(v (+ 1 (matrix-get matrix x y 0)))] + (matrix-set! matrix x y v) + v)) + +; sum all values of matrix +(define (matrix-sum matrix) + (vector-fold + (lambda (y sum line) + (+ sum + (vector-fold + (lambda (x sum value) (+ sum value)) + 0 line))) + 0 (matrix-data matrix)))