day 3 part 2 in elisp

This commit is contained in:
cow 2022-12-07 01:14:06 -05:00
parent bc25be2ee3
commit d059140b37

View file

@ -33,6 +33,9 @@
(+ (- char ?A) 26 1) (+ (- char ?A) 26 1)
(+ (- char ?a) 1))) (+ (- char ?a) 1)))
(defun solution-3-way-intersection (a b c)
(seq-intersection (seq-intersection a b) c))
(defun solution-part-1 (input) (defun solution-part-1 (input)
(let* ((char-lists (seq-map 'string-to-list input)) (let* ((char-lists (seq-map 'string-to-list input))
(split-into-halves (seq-map (lambda (char-list) (split-into-halves (seq-map (lambda (char-list)
@ -49,12 +52,29 @@
(item-priorities (seq-map 'solution-letter-priority common-items))) (item-priorities (seq-map 'solution-letter-priority common-items)))
(apply '+ item-priorities))) (apply '+ item-priorities)))
(defun solution-part-2 (input)
(let* ((char-lists (seq-map 'string-to-list input))
(groups (seq-split char-lists 3))
(common-items (seq-map (lambda (group)
(car (seq-intersection (seq-intersection (car group)
(cadr group))
(caddr group))))
groups))
(item-priorites (seq-map 'solution-letter-priority common-items)))
(apply '+ item-priorites)))
(ert-deftest solution-part-1-example () (ert-deftest solution-part-1-example ()
(should (equal (solution-part-1 (aoc-read-file-lines solution-example-input)) 157))) (should (equal (solution-part-1 (aoc-read-file-lines solution-example-input)) 157)))
(ert-deftest solution-part-1 () (ert-deftest solution-part-1 ()
(should (equal (solution-part-1 (aoc-read-file-lines solution-input)) 7878))) (should (equal (solution-part-1 (aoc-read-file-lines solution-input)) 7878)))
(ert-deftest solution-part-2-example ()
(should (equal (solution-part-2 (aoc-read-file-lines solution-example-input)) 70)))
(ert-deftest solution-part-2 ()
(should (equal (solution-part-2 (aoc-read-file-lines solution-input)) 2760)))
(when noninteractive (when noninteractive
(ert-run-tests-batch)) (ert-run-tests-batch))