From 1a81d370510a01b482aae8033704d1a0523d11b7 Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Thu, 5 Dec 2024 08:57:25 -0300 Subject: Add day 04 of 2024 --- 2024/aoc-utils.scm | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) (limited to '2024/aoc-utils.scm') diff --git a/2024/aoc-utils.scm b/2024/aoc-utils.scm index ed16bfd..9f22c47 100644 --- a/2024/aoc-utils.scm +++ b/2024/aoc-utils.scm @@ -1,12 +1,25 @@ (define-module (aoc-utils) #:use-module (srfi srfi-1) #:use-module (srfi srfi-9) + #:use-module (srfi srfi-9 gnu) #:use-module (ice-9 regex) #:use-module (ice-9 rdelim) #:export (file->list file->str file->matrix - lines->number-list)) + lines->number-list + + + matrix + make-matrix + matrix-inner + matrix-width + matrix-height + matrix-ref + matrix-valid-x? + matrix-valid-y? + matrix-valid-point? + list-flatten)) (define (file->list filename) (let ((lines (string-split (call-with-input-file filename read-string) #\Newline))) @@ -35,3 +48,36 @@ ((< i 0)) (set! retval (cons i retval))) retval)) + +;; Matrix utilities + +(define-record-type + (matrix inner width height) + matrix? + (inner matrix-inner) + (width matrix-width) + (height matrix-height)) + +(define (make-matrix lst) + (let ((height (length lst)) + (width (length (car lst)))) + (matrix (list-flatten lst) width height))) + +(define (list-flatten lst) + (apply append lst)) + +(define (matrix-ref matrix x y) + (if (matrix-valid-point? matrix x y) + (list-ref (matrix-inner matrix) + (+ x (* y (matrix-height matrix)))) + #f)) + +(define (matrix-valid-x? matrix x) + (and (>= x 0) (< x (matrix-width matrix)))) + +(define (matrix-valid-y? matrix y) + (and (>= y 0) (< y (matrix-height matrix)))) + +(define (matrix-valid-point? matrix x y) + (and (matrix-valid-x? matrix x) + (matrix-valid-y? matrix y))) -- cgit v1.2.3-70-g09d2