day 5 part 1
This commit is contained in:
parent
9959416bcc
commit
98501dec8b
3 changed files with 71 additions and 1 deletions
59
05a.lisp
Normal file
59
05a.lisp
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
(load (sb-ext:posix-getenv "ASDF"))
|
||||||
|
(asdf:load-system 'str)
|
||||||
|
(asdf:load-system 'alexandria)
|
||||||
|
|
||||||
|
(defun parse-line (line)
|
||||||
|
(str:match line
|
||||||
|
((first "-" last)
|
||||||
|
(cons (parse-integer first) (parse-integer last)))
|
||||||
|
((test)
|
||||||
|
(parse-integer test))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(parse-line "5-15")
|
||||||
|
(parse-line "6")
|
||||||
|
|
||||||
|
(defun in-set-last (item setp)
|
||||||
|
(let ((first (car setp)) (last (cdr setp)))
|
||||||
|
(if (and (>= item first) (<= item last))
|
||||||
|
(+ last 1))))
|
||||||
|
(defun in-set-first (item setp)
|
||||||
|
(let ((first (car setp)) (last (cdr setp)))
|
||||||
|
(if (and (>= item first) (<= item last))
|
||||||
|
(- first 1))))
|
||||||
|
(in-set-first 5 (cons 5 15))
|
||||||
|
|
||||||
|
(defun in-superset (item superset test)
|
||||||
|
(loop for setp in superset
|
||||||
|
when (funcall test item setp)
|
||||||
|
return (funcall test item setp))
|
||||||
|
)
|
||||||
|
(in-superset 10 (list (parse-line "5-15") (parse-line "20-30")) #'in-set-first)
|
||||||
|
(in-superset 10 (list (parse-line "5-15") (parse-line "20-30")) #'in-set-last)
|
||||||
|
|
||||||
|
(defun set-transform (set superset)
|
||||||
|
(let ((first (car set)) (last (cdr set)))
|
||||||
|
(setq first (or (in-superset first superset #'in-set-last) first))
|
||||||
|
(setq last (or (in-superset last superset #'in-set-first) last))
|
||||||
|
(if (< first last)
|
||||||
|
(cons first last))))
|
||||||
|
(set-transform (cons 14 115) (list (cons 5 15) (cons 20 30)))
|
||||||
|
|
||||||
|
(defun count-fresh-ingredients (filename)
|
||||||
|
(let ((superset ())
|
||||||
|
(fresh-count 0))
|
||||||
|
(loop for line in (str:lines (str:from-file filename) :omit-nulls t)
|
||||||
|
do (let ((parsed (parse-line line)))
|
||||||
|
(if (consp parsed)
|
||||||
|
; consp
|
||||||
|
(alexandria:if-let (parsed (set-transform parsed superset))
|
||||||
|
(setf superset (push parsed superset)))
|
||||||
|
; integerp
|
||||||
|
(if (in-superset parsed superset #'in-set-last) (setq fresh-count (+ fresh-count 1)))
|
||||||
|
)))
|
||||||
|
;; (print superset)
|
||||||
|
fresh-count))
|
||||||
|
|
||||||
|
(count-fresh-ingredients "05example.txt")
|
||||||
|
|
||||||
|
(count-fresh-ingredients "05data.txt")
|
||||||
11
05example.txt
Normal file
11
05example.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
default = pkgs.mkShellNoCC {
|
default = pkgs.mkShellNoCC {
|
||||||
packages = with pkgs; [
|
packages = with pkgs; [
|
||||||
shellcheck
|
shellcheck
|
||||||
(sbcl.withPackages (ps: [ps.str ps.cl-heap]))
|
(sbcl.withPackages (ps: [ps.str ps.cl-heap ps.alexandria]))
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue