#!/usr/bin/env -S emacs --script ;; -*- lexical-binding: t; -*- ;; 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 . (add-to-list 'load-path "../") (require 'ert) (require 'advent-of-code) (defvar solution-input "../../input/day-3.txt") (defvar solution-example-input "../../input/day-3-example.txt") (defun solution-letter-priority (char) (if (char-uppercase-p char) (+ (- char ?A) 26 1) (+ (- char ?a) 1))) (defun solution-3-way-intersection (a b c) (seq-intersection (seq-intersection a b) c)) (defun solution-part-1 (input) (let* ((char-lists (seq-map 'string-to-list input)) (split-into-halves (seq-map (lambda (char-list) (seq-split char-list (/ (length char-list) 2))) char-lists)) (common-items (seq-map (lambda (halves) (if-let ((intersection (seq-intersection (car halves) (cadr halves)))) (car intersection) nil)) split-into-halves)) (filter-nils (seq-filter (lambda (item) (not (null item))) common-items)) (item-priorities (seq-map 'solution-letter-priority common-items))) (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 () (should (equal (solution-part-1 (aoc-read-file-lines solution-example-input)) 157))) (ert-deftest solution-part-1 () (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 (ert-run-tests-batch)) ;; Local Variables: ;; read-symbol-shorthands: (("solution-" . "advent-of-code-day-3-") ("aoc-" . "advent-of-code-")) ;; End: