From 1843eac277feb37d9fd1ae1a60157f041333ad8c Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sat, 3 Dec 2022 01:06:00 -0500 Subject: [PATCH] day3: Add part 2 --- 2022/03/sol.scm | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/2022/03/sol.scm b/2022/03/sol.scm index f14ee74..685cc69 100755 --- a/2022/03/sol.scm +++ b/2022/03/sol.scm @@ -32,11 +32,37 @@ (if (member x r) (prio x) 0)) l)))) +; this could be optimized by sorting first +(define (uniq lst) + (if (null? lst) + lst + (if (member (car lst) (cdr lst)) + (uniq (cdr lst)) + (cons (car lst) (uniq (cdr lst)))))) + +(define (score-group a b c) + (apply + + (map (lambda (x) + (if (and (member x b) (member x c)) + (prio x) + 0)) + a))) + +(define (part-2 packs) + (let loop ((i 0) (score 0) (pks (map uniq packs))) + (if (null? pks) + score + (if (= (modulo i 3) 0) + (loop (add1 i) + (+ score (score-group (car pks) (cadr pks) (caddr pks))) + (cdr pks)) + (loop (add1 i) score (cdr pks)))))) + + (define (main args) (let* ((lines (read-lines)) (packs (map string->list lines)) - (scores (map score-pack packs))) - (print (car packs)) - (print (bisect (car packs))) - (print (score-pack (car packs))) - (print (apply + scores)))) + (scores-1 (map score-pack packs)) + (score-2 (part-2 packs))) + (print (apply + scores-1)) + (print score-2))) -- libgit2 1.8.1