day 7 part 2 in common lisp

This commit is contained in:
cow 2022-12-22 22:34:40 -05:00
parent c597155e86
commit 60320ec331

View file

@ -20,13 +20,13 @@
(require 'iterate) (require 'iterate)
(require 'uiop) (require 'uiop)
(defpackage :solution (defpackage :advent-of-code-day-7
(:use :cl) (:use :cl)
(:import-from :iterate :iter) (:import-from :iterate :iter)
(:import-from :uiop :if-let) (:import-from :uiop :if-let)
(:shadow :directory)) (:shadow :directory))
(in-package :solution) (in-package :advent-of-code-day-7)
(defclass dir () (defclass dir ()
((name ((name
@ -142,11 +142,28 @@
(<= size 100000)) (<= size 100000))
(mapcar 'size (walk (root fs)))))) (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) (defun run-part-1 (input)
(let* ((events (parse-input input)) (let* ((events (parse-input input))
(fs (run-commands events))) (fs (run-commands events)))
(solution-part-1 fs))) (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 "example.txt")) 95437))
(assert (= (run-part-1 (uiop:read-file-lines "input.txt")) 1084134)) (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))