aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/04/1.rkt
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2024-02-15 17:49:56 +0800
committerMistivia <i@mistivia.com>2024-02-15 17:51:00 +0800
commitf163146fe4b31f1b9e4e25f7f61a5c6928e2fe5b (patch)
treee3e99fc5eb674c783ac2947a4276f1b92a892a64 /advent-of-code/2023/04/1.rkt
parent58709a89622062b527b242f65aa0501a4efd47d3 (diff)
refactor advent of code 2023 day 04 from c to racket
Diffstat (limited to 'advent-of-code/2023/04/1.rkt')
-rw-r--r--advent-of-code/2023/04/1.rkt35
1 files changed, 35 insertions, 0 deletions
diff --git a/advent-of-code/2023/04/1.rkt b/advent-of-code/2023/04/1.rkt
new file mode 100644
index 0000000..30ea62f
--- /dev/null
+++ b/advent-of-code/2023/04/1.rkt
@@ -0,0 +1,35 @@
+#lang racket
+
+(require "../lib/obj.rkt")
+
+(define (get-lines fp)
+ (let loop ((lines '()))
+ (define l (read-line fp))
+ (if (eof-object? l)
+ (reverse lines)
+ (loop (cons l lines)))))
+
+(define fp (open-input-file "input"))
+
+(define lines (get-lines fp))
+
+(define (strip-head s)
+ (string-trim (cadr (string-split s ":"))))
+
+(set! lines (map strip-head lines))
+
+(define make-card (obj-maker 'win-nums 'nums))
+
+(define (parse-card s)
+ (define lst (string-split s "|"))
+ (define win-nums (map string->number (string-split (car lst))))
+ (define nums (map string->number (string-split (cadr lst))))
+ (make-card win-nums nums))
+
+(define (point card)
+ (define wins (length (filter (lambda (x) (member x (card 'win-nums))) (card 'nums))))
+ (if (= wins 0)
+ 0
+ (expt 2 (- wins 1))))
+
+(apply + (map point (map parse-card lines)))