day 7 part 1 in common lisp
This commit is contained in:
parent
dab8d182ee
commit
c597155e86
1
common-lisp/day-7/input.txt
Symbolic link
1
common-lisp/day-7/input.txt
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../input/day-7.txt
|
|
@ -1,5 +1,19 @@
|
|||
#!/usr/bin/env -S sbcl --script
|
||||
|
||||
;; Copyright (C) 2022
|
||||
;; This program is free software: you can redistribute it and/or modify
|
||||
;; it under the terms of the GNU General Public License as published by
|
||||
;; the Free Software Foundation, either version 3 of the License, or
|
||||
;; (at your option) any later version.
|
||||
|
||||
;; This program is distributed in the hope that it will be useful,
|
||||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
;; GNU General Public License for more details.
|
||||
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
(require 'asdf)
|
||||
(require 'cl-ppcre)
|
||||
(require 'str)
|
||||
|
@ -44,15 +58,24 @@
|
|||
(depth p (incf depth))
|
||||
depth))
|
||||
|
||||
(defun size (dir)
|
||||
(with-accessors ((files files)
|
||||
(subdirs subdirs))
|
||||
dir
|
||||
(let ((files-sum (apply '+ (mapcar 'cdr files)))
|
||||
(subdirs-sum (apply '+ (mapcar 'size (mapcar 'cdr subdirs)))))
|
||||
(+ files-sum subdirs-sum))))
|
||||
|
||||
(defmethod print-object ((obj dir) stream)
|
||||
(format stream
|
||||
"~&name: ~a~%parent: ~a~%depth: ~a~%subdirs: ~a~%files: ~a ~&"
|
||||
(name obj)
|
||||
(if (> (depth obj) 0)
|
||||
(name (parent obj)))
|
||||
(depth obj)
|
||||
(files obj)
|
||||
(subdirs obj)))
|
||||
(format stream "<dir ~a>" (name obj)))
|
||||
|
||||
(defun walk (dir &optional visited)
|
||||
(with-accessors ((subdirs subdirs))
|
||||
dir
|
||||
(push dir visited)
|
||||
(iter (iter:for (_ . subdir) in subdirs)
|
||||
(setq visited (walk subdir visited)))
|
||||
visited))
|
||||
|
||||
(defclass fs ()
|
||||
((root
|
||||
|
@ -97,7 +120,7 @@
|
|||
((str:digit? (nth 0 parts))
|
||||
(iter:collect (list 'file (nth 1 parts) (parse-integer (nth 0 parts))))))))
|
||||
|
||||
(defun solution-part-1 (events)
|
||||
(defun run-commands (events)
|
||||
(iter (iter:with fs = (make-instance 'fs))
|
||||
(iter:for event in events)
|
||||
(let* ((type (car event))
|
||||
|
@ -113,3 +136,17 @@
|
|||
((equal type 'file)
|
||||
(touch cwd dir-or-file-name size))))
|
||||
(iter:finally (return fs)))))
|
||||
|
||||
(defun solution-part-1 (fs)
|
||||
(apply '+ (remove-if-not (lambda (size)
|
||||
(<= size 100000))
|
||||
(mapcar 'size (walk (root fs))))))
|
||||
|
||||
(defun run-part-1 (input)
|
||||
(let* ((events (parse-input input))
|
||||
(fs (run-commands events)))
|
||||
(solution-part-1 fs)))
|
||||
|
||||
|
||||
(assert (= (run-part-1 (uiop:read-file-lines "example.txt")) 95437))
|
||||
(assert (= (run-part-1 (uiop:read-file-lines "input.txt")) 1084134))
|
||||
|
|
1101
input/day-7.txt
Normal file
1101
input/day-7.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue