aoc-2022/elisp/day-6/solution.el

74 lines
2.5 KiB
EmacsLisp
Executable File

#!/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 <https://www.gnu.org/licenses/>.
(add-to-list 'load-path default-directory)
(require 'cl-lib)
(require 'ert)
(require 'advent-of-code)
(defun -all-different? (items)
(cl-loop for (item . rest) on items
if (seq-contains rest item)
return nil
finally (return t)))
(defun -part-1 (input)
(cl-loop for (a b c d) on (string-to-list input)
for x from 0
while d
if (-all-different? (list a b c d))
return (+ x 4)))
(defun -part-2 (input)
(cl-loop for (a b c d e f g h i j k l m n) on (string-to-list input)
for x from 0
while d
if (-all-different? (list a b c d e f g h i j k l m n))
return (+ x 14)))
(ert-deftest -test-part-1-examples ()
(should (equal (-part-1 "mjqjpqmgbljsphdztnvjfqwrcgsmlb") 7))
(should (equal (-part-1 "bvwbjplbgvbhsrlpgdmjqwftvncz") 5))
(should (equal (-part-1 "nppdvjthqldpwncqszvftbrmjlhg") 6))
(should (equal (-part-1 "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg") 10))
(should (equal (-part-1 "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw") 11)))
(ert-deftest -test-part-1 ()
(should (equal (-part-1 (car (aoc-read-file-lines "input.txt"))) 1794)))
(ert-deftest -test-part-2-examples ()
(should (equal (-part-2 "mjqjpqmgbljsphdztnvjfqwrcgsmlb") 19))
(should (equal (-part-2 "bvwbjplbgvbhsrlpgdmjqwftvncz") 23))
(should (equal (-part-2 "nppdvjthqldpwncqszvftbrmjlhg") 23))
(should (equal (-part-2 "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg") 29))
(should (equal (-part-2 "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw") 26)))
(ert-deftest -test-part-2 ()
(should (equal (-part-2 (car (aoc-read-file-lines "input.txt"))) 2851)))
(when noninteractive
(ert-run-tests-batch))
;; Code
;; Local Variables:
;; read-symbol-shorthands: (("-" . "advent-of-code-day-6-") ("aoc-" . "advent-of-code-"))
;; End: