;;; solver.el --- Day 03 -*- lexical-binding: t; -*- ;; ;; Copyright (C) 2022 Óscar Nájera ;; ;; Author: Óscar Nájera ;; Maintainer: Óscar Nájera ;; Created: December 04, 2022 ;; Modified: December 04, 2022 ;; ;; This file is not part of GNU Emacs. ;; ;;; Commentary: ;; ;; Day 03 ;; ;;; Code: (require 'subr-x) (require 'ert) (defsubst solver-recover-value (cross) (let ((result 0)) (while (= 0 (logand cross 1)) (setq cross (ash cross -1)) (cl-incf result)) result)) (ert-deftest test-recover () (should (eq ?t (+ 64 (solver-recover-value (ash 1 (- ?t 64)))))) (should (eq ?s (+ 64 (solver-recover-value (ash 1 (- ?s 64)))))) (should (eq ?C (+ 64 (solver-recover-value (ash 1 (- ?C 64))))))) ;; small cap starts at a=97, but already took 64 (defsubst solver-to-priority (value) (if (< value 31) ;; It is a capital letter in range [1;26] (+ value 26) (- value 32))) (defun solver-line-priority (str) (cl-assert (length> str 0)) (let ((mid (/ (length str) 2))) (thread-first (cl-intersection (string-to-list (substring str 0 mid)) (string-to-list (substring str mid))) (car) (- 64) (solver-to-priority)))) (ert-deftest test-line-priority () (should (= 16 (solver-line-priority "vJrwpWtwJgWrhcsFMMfFFhFp"))) (should (= 19 (solver-line-priority "hMHLcmGLMLhHmsRMsSvsQSqrsrlJTTdV" ))) (should (= 20 (solver-line-priority "ttgJtRGJQctTZtZT")))) (defun solver-badge-priority (group-packs) (thread-last group-packs (mapcar #'string-to-list) (cl-reduce #'cl-intersection) (car) (+ -64) (solver-to-priority))) (defun solver (task lines) (with-temp-buffer (insert-file-contents "input") (let ((packs (split-string (buffer-string)))) (apply #'+ (mapcar task (if (= lines 1) packs (seq-partition packs 3))))))) (ert-deftest test-problems () (should (= 8072 (solver #'solver-line-priority 1))) (should (= 2567 (solver #'solver-badge-priority 3))))