aboutsummaryrefslogtreecommitdiff
path: root/2024/aoc-utils.scm
blob: 9f22c479cd1cad06abf43be7eb4032b19e5abaec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(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

            <matrix>
            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)))
    ; Remove EOF token
    (take lines (1- (length lines)))))

(define (file->str filename)
  (call-with-input-file filename read-string))

(define (file->matrix filename)
  (map string->list (file->list filename)))

(define (lines->number-list lines)
  (map (lambda (line)
         (map string->number (string-split line #\Space)))
       lines))

(define (remove-nth l n)
  "Remove the `N' element from `L', returning a new list."
  (append (list-head l n) (list-tail l (1+ n))))

(define (indexes lst)
  "Get the indexes for `lst'."
  (let ((retval '()))
    (do ((i (1- (length lst)) (1- i)))
        ((< i 0))
      (set! retval (cons i retval)))
    retval))

;; Matrix utilities

(define-record-type <matrix>
  (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)))