day 7 part 2 in common lisp
This commit is contained in:
parent
c597155e86
commit
60320ec331
|
@ -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)))
|
||||
(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))
|
||||
|
|
Loading…
Reference in a new issue