diff options
| author | Mistivia <i@mistivia.com> | 2024-03-03 17:18:03 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-03-03 17:18:03 +0800 |
| commit | f239c7d43ab38470b2a2b8e02efa5fc4b7053c73 (patch) | |
| tree | 84a01d7893323f9b9b01c22462230d11f3c8ddcd /12 | |
| parent | d13c776569387b5be0adca83585ddf833404f9af (diff) | |
solve day 12 part 1
Diffstat (limited to '12')
| -rw-r--r-- | 12/1.rkt | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/12/1.rkt b/12/1.rkt new file mode 100644 index 0000000..42cd9e0 --- /dev/null +++ b/12/1.rkt @@ -0,0 +1,33 @@ +#lang racket + +(require "../lib/utils.rkt") + +(define lines + (call-with-input-file "input" + (λ (fp) + (get-lines fp)))) + +(define (parse-line line) + (define line-splited (string-split line)) + (define arr-list (string->list (car line-splited))) + (define conds (map string->number (string-split (cadr line-splited) ","))) + (cons arr-list conds)) + +(define (arr-match? lst conds) + (define str (list->string (reverse lst))) + (equal? conds (filter (λ (n) (not (= 0 n))) (map string-length (string-split str "."))))) + +(define (possible-arr-num instance) + (define conds (cdr instance)) + (define (impl scanned unscanned) + (if (null? unscanned) + (if (arr-match? scanned conds) 1 0) + (let () + (define head (car unscanned)) + (if (not (char=? head #\?)) + (impl (cons head scanned) (cdr unscanned)) + (+ (impl (cons #\. scanned) (cdr unscanned)) + (impl (cons #\# scanned) (cdr unscanned))))))) + (impl '() (car instance))) + +(apply + (map possible-arr-num (map parse-line lines)))
\ No newline at end of file |
