blob: 62b91e24d3582e9d88d4a6d76b32005da970ce3c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
;;; solver.el --- Day 03 -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; 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))))
|