From f239c7d43ab38470b2a2b8e02efa5fc4b7053c73 Mon Sep 17 00:00:00 2001 From: Mistivia Date: Sun, 3 Mar 2024 17:18:03 +0800 Subject: solve day 12 part 1 --- 12/1.rkt | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 12/1.rkt 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 -- cgit v1.0