day 11 part 2
This commit is contained in:
@@ -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))))))))
|
||||
|
||||
|
||||
|
||||
|
||||
36
common.scm
36
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)))
|
||||
|
||||
Reference in New Issue
Block a user