From 60320ec3311ae7489eb773cf0095bc15dc93ffb9 Mon Sep 17 00:00:00 2001 From: cow Date: Thu, 22 Dec 2022 22:34:40 -0500 Subject: [PATCH] day 7 part 2 in common lisp --- common-lisp/day-7/solution.lisp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/common-lisp/day-7/solution.lisp b/common-lisp/day-7/solution.lisp index bc7ca2e..99ba239 100755 --- a/common-lisp/day-7/solution.lisp +++ b/common-lisp/day-7/solution.lisp @@ -20,13 +20,13 @@ (require 'iterate) (require 'uiop) -(defpackage :solution +(defpackage :advent-of-code-day-7 (:use :cl) (:import-from :iterate :iter) (:import-from :uiop :if-let) (:shadow :directory)) -(in-package :solution) +(in-package :advent-of-code-day-7) (defclass dir () ((name @@ -142,11 +142,28 @@ (<= size 100000)) (mapcar 'size (walk (root fs)))))) +(defun solution-part-2 (fs) + (let* ((fs-max-size 70000000) + (update-size 30000000) + (fs-used (size (root fs))) + (fs-free (- fs-max-size fs-used)) + (size-needed (- update-size fs-free)) + (dir-sizes (mapcar 'size (walk (root fs)))) + (dirs-that-are-large-enough (remove-if-not (lambda (s) (>= s size-needed)) dir-sizes)) + (sorted (sort dirs-that-are-large-enough '<))) + (car sorted))) + (defun run-part-1 (input) (let* ((events (parse-input input)) - (fs (run-commands events))) + (fs (run-commands events))) (solution-part-1 fs))) +(defun run-part-2 (input) + (let* ((events (parse-input input)) + (fs (run-commands events))) + (solution-part-2 fs))) (assert (= (run-part-1 (uiop:read-file-lines "example.txt")) 95437)) (assert (= (run-part-1 (uiop:read-file-lines "input.txt")) 1084134)) +(assert (= (run-part-2 (uiop:read-file-lines "example.txt")) 24933642)) +(assert (= (run-part-2 (uiop:read-file-lines "input.txt")) 6183184))