aoc

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

commit f635613d7c6b8d3bb8844907b9be070add5595d3 (patch)
parent 96db211c1436807900c76b939d701db9e240012d
Author: Alex Karle <alex@alexkarle.com>
Date:   Sat,  3 Dec 2022 01:21:52 -0500

day3: Refactor code a bit for readability

Diffstat:
M2022/03/sol.scm | 18++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/2022/03/sol.scm b/2022/03/sol.scm @@ -1,6 +1,8 @@ #!/usr/local/bin/chicken-csi -ss (import (chicken io)) +(define (sum lst) (apply + lst)) + (define (prio c) (if (char-upper-case? c) (- (char->integer c) 38) ; #\A -> 65 - 38 = 27 @@ -26,13 +28,10 @@ (let* ((parts (bisect pk)) (l (car parts)) (r (cadr parts))) - (apply + - (map - (lambda (x) - (if (member x r) (prio x) 0)) - l)))) + (sum (map (lambda (x) (if (member x r) (prio x) 0)) l)))) -; this could be optimized by sorting first +; lots of member checks.. could use a better data structure +; than linked lists (hash tables) if this was slow / large input (define (uniq lst) (if (null? lst) lst @@ -41,12 +40,11 @@ (cons (car lst) (uniq (cdr lst)))))) (define (score-group a b c) - (apply + - (map (lambda (x) + (sum (map (lambda (x) (if (and (member x b) (member x c)) (prio x) 0)) - a))) + a))) (define (part-2 packs) (let loop ((i 0) (score 0) (pks (map uniq packs))) @@ -64,5 +62,5 @@ (packs (map string->list lines)) (scores-1 (map score-pack packs)) (score-2 (part-2 packs))) - (print (apply + scores-1)) + (print (sum scores-1)) (print score-2)))