aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 1843eac277feb37d9fd1ae1a60157f041333ad8c (patch)
parent a5eb8e8a08568cfbad6415643307fa2b83d30a7c
Author: Alex Karle <alex@alexkarle.com>
Date:   Sat,  3 Dec 2022 01:06:00 -0500

day3: Add part 2

Diffstat:
M2022/03/sol.scm | 36+++++++++++++++++++++++++++++++-----
1 file changed, 31 insertions(+), 5 deletions(-)

diff --git 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)))