summaryrefslogtreecommitdiff
path: root/4-2.scm
diff options
context:
space:
mode:
Diffstat (limited to '4-2.scm')
-rwxr-xr-x4-2.scm37
1 files changed, 37 insertions, 0 deletions
diff --git a/4-2.scm b/4-2.scm
new file mode 100755
index 0000000..48517ce
--- /dev/null
+++ b/4-2.scm
@@ -0,0 +1,37 @@
+#!/usr/bin/env -S guile -s
+!#
+(use-modules (ice-9 regex)
+ (ice-9 textual-ports)
+ (srfi srfi-1))
+
+(define (read-lines port)
+ (letrec ((loop (lambda (l ls)
+ (if (eof-object? l)
+ ls
+ (loop (get-line port) (cons l ls))))))
+ (reverse (loop (get-line port) '()))))
+
+(define (split-up nums)
+ (filter (lambda (x) (not (string-null? x))) (string-split nums char-set:whitespace)))
+
+(define (parse line)
+ (let* ((m (string-match " ([0-9]+): ([0-9| ]+) \\| ([0-9| ]+)" line))
+ (winning (split-up (match:substring m 2)))
+ (have (split-up (match:substring m 3)))
+ (shared (filter (lambda (x) (member x winning)) have)))
+ `(,(length shared) . ,1)))
+
+(let* ((port (open-input-file "4.txt"))
+ (lines (read-lines port))
+ (cards (map parse lines)))
+ (close-port port)
+ ;; (while (< index (length cards))
+ ;; (do ((i 1 (1+ i))) ((> i (cdr (list-ref cards index))))
+ ;; (set! cards (append cards `(,(assoc (+ (car (list-ref cards index)) i) cards)))))
+ ;; (set! index (1+ index)))
+ ;; (display (length cards))
+ (do ((i 0 (1+ i))) ((= i (length cards)))
+ (do ((j 1 (1+ j))) ((> j (car (list-ref cards i))))
+ (let ((card (list-ref cards (+ i j))))
+ (list-set! cards (+ i j) `(,(car card) . ,(+ (cdr card) (cdr (list-ref cards i))))))))
+ (display (fold + 0 (map cdr cards))))