From bcf06921c3260c3d37e5d933c2c344e1c1b09102 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Wed, 7 Dec 2022 21:14:24 -0500 Subject: [PATCH] day7: Add part 2 solution --- 2022/07/sol.scm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/2022/07/sol.scm b/2022/07/sol.scm index c4d619e..91c484b 100755 --- a/2022/07/sol.scm +++ b/2022/07/sol.scm @@ -5,6 +5,7 @@ ;; Parsing this one is gonna be hard without set! (import srfi-69 ; hash tables (chicken io) + (chicken sort) (chicken irregex) (chicken string)) @@ -40,6 +41,7 @@ (define (add-size-dir! dir) (let ((newsize (+ n (hash-table-ref/default sizes dir 0)))) (hash-table-set! sizes dir newsize))) + ; TODO: would be more efficient to do this after all lines processed (let loop ((dir cwd)) (if (not (equal? dir "")) (begin (add-size-dir! dir) @@ -57,7 +59,14 @@ 'no-op) (else (add-size! (string->number (car parts))))))) +(define (big-enough? n) + (>= n (- 30000000 + (- 70000000 (hash-table-ref sizes "/"))))) + (define (main args) (let ((lines (read-lines))) (for-each parse lines) - (print (apply + (filter (lambda (x) (<= x 100000)) (hash-table-values sizes)))))) + ; (hash-table-for-each sizes (lambda (x n) (print x n))) + (print (apply + (filter (lambda (x) (<= x 100000)) (hash-table-values sizes)))) + (print (car (sort (filter big-enough? (hash-table-values sizes)) + <))))) -- libgit2 1.8.1