diff options
| author | Juan Marín Noguera <juan@mnpi.eu> | 2024-12-04 17:45:46 +0100 |
|---|---|---|
| committer | Juan Marín Noguera <juan@mnpi.eu> | 2024-12-04 17:45:46 +0100 |
| commit | b299763c6b81511a9d0212cb8cf428028809a441 (patch) | |
| tree | fced8f57a29eabe98745e5552714d3a7688ad131 /util.scm | |
| parent | 1f2576473990a8763d2bfbcc5753cc2af915d1b8 (diff) | |
Refactor Guile version of day 04
Diffstat (limited to 'util.scm')
| -rw-r--r-- | util.scm | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/util.scm b/util.scm new file mode 100644 index 0000000..44a9fa8 --- /dev/null +++ b/util.scm @@ -0,0 +1,32 @@ +(define-module (util) + #:use-module (ice-9 rdelim) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-9) + #:use-module (srfi srfi-26) + #:use-module (srfi srfi-41) + #:export (point point? point-i point-j square array-point point-add read-map)) + +(define-record-type <point> (point i j) point? + (i point-i) + (j point-j)) + +(define (square i1 j1 i2 j2) + (define (row i) + (stream-map (cut point i <>) (stream-range j1 j2))) + (stream-concat (stream-map row (stream-range i1 i2)))) + +(define (array-point arr pt) + (array-ref arr (point-i pt) (point-j pt))) + +(define (point-add pt i j) + (point (+ (point-i pt) i) (+ (point-j pt) j))) + +(define* (read-map #:optional (port (current-input-port))) + (define (read-lines acc) + (define line (read-line port)) + (if (and (not (eof-object? line)) (> (string-length line) 0)) + (read-lines (cons line acc)) + acc)) + (define strings (reverse! (read-lines '()))) + (define string->char-codes (compose (cut map char->integer <>) string->list)) + (list->typed-array 'u8 2 (map string->char-codes strings))) |
