Last active
December 4, 2024 12:35
-
-
Save qookei/508b13f97aebc92c896c3d3a9e0ccd94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use-modules (srfi srfi-1) (srfi srfi-26) | |
(ice-9 pretty-print) (ice-9 textual-ports)) | |
(define (%read-input) | |
(let ([line (get-line (current-input-port))]) | |
(if (eof-object? line) | |
'() | |
(cons (string->list line) (%read-input))))) | |
(define (read-input) | |
(list->array 2 (%read-input))) | |
(define (transform-right _ y x) | |
(λ (dx) | |
(list y (+ x dx)))) | |
(define (transform-down _ y x) | |
(λ (dy) | |
(list (+ y dy) x))) | |
(define (transform-diag-right _ y x) | |
(λ (d) | |
(list (+ y d) (+ x d)))) | |
(define (transform-diag-left len y x) | |
(λ (d) | |
(list (+ y d) (- x (- len) d)))) | |
(define (index-list bounds limits) | |
(concatenate | |
(map (λ (y) | |
(map (λ (x) | |
(list y x)) | |
(iota (- (cadr bounds) (cadr limits))))) | |
(iota (- (car bounds) (car limits)))))) | |
(define (array-fwd-rev-equal? a b) | |
(let ([rev-b | |
(make-shared-array | |
b | |
(λ (x) | |
(list (- (array-length b) 1 x))) | |
(array-length b))]) | |
(or (array-equal? a b) | |
(array-equal? a rev-b)))) | |
(define +p1-target+ (list->array 1 (string->list "XMAS"))) | |
(define +p2-target+ (list->array 1 (string->list "MAS"))) | |
(define (part1 input) | |
(fold | |
+ 0 | |
(map | |
(λ (transform indices) | |
(count | |
(cut array-fwd-rev-equal? +p1-target+ <>) | |
(map | |
(λ (yx) | |
(make-shared-array input (apply transform (cons 3 yx)) 4)) | |
indices))) | |
(list transform-right | |
transform-down | |
transform-diag-right | |
transform-diag-left) | |
(map | |
(λ (limits) | |
(index-list (array-dimensions input) limits)) | |
'((0 3) (3 0) | |
(3 3) (3 3)))))) | |
(define (part2 input) | |
(let ([indices (index-list (array-dimensions input) '(2 2))]) | |
(count | |
identity | |
(map | |
(λ (yx) | |
(every | |
(λ (transform) | |
(array-fwd-rev-equal? | |
+p2-target+ | |
(make-shared-array input | |
(apply transform (cons 2 yx)) | |
3))) | |
(list transform-diag-right | |
transform-diag-left))) | |
indices)))) | |
(let ([input (read-input)]) | |
(pretty-print (part1 input)) | |
(pretty-print (part2 input))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment