aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-08 04:42:19 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-08 04:42:19 +0100
commitf6174701ace9f37954ac0f71aa41908433540ad6 (patch)
treeae79ae9e6d68be7ea82f9814f851b97a70ab4eda
parentc06ab5769572e1f1b95b6953a3aecc22eaaf69ae (diff)
downloadscratch-f6174701ace9f37954ac0f71aa41908433540ad6.tar.gz
scratch-f6174701ace9f37954ac0f71aa41908433540ad6.tar.bz2
scratch-f6174701ace9f37954ac0f71aa41908433540ad6.zip
[AoC2022] Elisp 07-01
-rw-r--r--AoC2022/07/solver.el58
1 files changed, 38 insertions, 20 deletions
diff --git a/AoC2022/07/solver.el b/AoC2022/07/solver.el
index ab97c08..38354ff 100644
--- a/AoC2022/07/solver.el
+++ b/AoC2022/07/solver.el
@@ -6,10 +6,6 @@
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; Created: December 08, 2022
;; Modified: December 08, 2022
-;; Version: 0.0.1
-;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
-;; Homepage: https://github.com/titan/solver
-;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
@@ -22,7 +18,6 @@
(require 'cl-lib)
-(with-temp-buffer
(insert "$ cd /
$ ls
dir a
@@ -46,24 +41,47 @@ $ ls
8033020 d.log
5626152 d.ext
7214296 k")
- (let ((data (split-string (buffer-string) "\n")))
- (cl-labels ((scan ()
- (cl-loop for item = (pop data)
- while (and item (not (string= "$ cd .." item)))
- when (parse item)
- collect it))
- (parse (entry)
- (pcase (split-string entry)
- (`("$" "cd" ,dir) (cons dir (scan)))
- (`("$" "ls") nil)
- (`("dir" ,dir) nil)
- (`(,size ,name) (cons name (string-to-number size))))))
- (scan)
- )))
+(with-temp-buffer
+ (insert-file-contents "input")
+ (solver-add-dir-size
+ (solver-parse-listing (split-string (buffer-string) "\n"))))
+
+(defun solver-parse-listing (lines)
+ (cl-labels ((scan ()
+ (cl-loop for item = (pop lines)
+ while (and item (not (string= "$ cd .." item)))
+ when (parse item)
+ collect it))
+ (parse (entry)
+ (pcase (split-string entry)
+ (`("$" "cd" ,dir) (cons dir (scan)))
+ (`("$" "ls") nil)
+ (`("dir" ,dir) nil)
+ (`(,size ,name) (cons name (string-to-number size))))))
+
+ (car (scan))))
+(defun solver-add-dir-size (tree)
+ (cl-labels ((combiner (tri)
+ (list (car tri)
+ (adder tri)
+ (seq-reduce (lambda (acc item)
+ (if (numberp (cdr item))
+ acc
+ (cons (combiner item) acc)))
+ (cdr tri) nil)))
+ (adder (tri)
+ (seq-reduce (lambda (acc item)
+ (+ acc
+ (if (numberp (cdr item))
+ (cdr item)
+ (adder item))))
+ (cdr tri) 0)))
+ (cl-loop for (_ size) on (flatten-tree (combiner tree)) by #'cddr
+ when (< size 100000)
+ sum size)))
-(append '(5 6 8) (cons 5 8))
(provide 'solver)
;;; solver.el ends here