From 1ca84c3abd40e81b4165b34ec0ca0a2ae317154c Mon Sep 17 00:00:00 2001 From: Oscar Najera Date: Tue, 13 Dec 2022 13:59:03 +0100 Subject: [AoC2022] Elisp 13 --- AoC2022/13/solver.el | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 AoC2022/13/solver.el (limited to 'AoC2022/13/solver.el') diff --git a/AoC2022/13/solver.el b/AoC2022/13/solver.el new file mode 100644 index 0000000..c7452bb --- /dev/null +++ b/AoC2022/13/solver.el @@ -0,0 +1,55 @@ +;;; solver.el --- DAy 13 -*- lexical-binding: t; -*- +;; +;; Copyright (C) 2022 Óscar Nájera +;; +;; Author: Óscar Nájera +;; Maintainer: Óscar Nájera +;; Created: December 13, 2022 +;; Modified: December 13, 2022 +;; +;; This file is not part of GNU Emacs. +;; +;;; Commentary: +;; +;; DAy 13 +;; +;;; Code: + +(with-temp-buffer + (insert-file-contents "input") + (cl-loop + for i from 1 + for a = (ignore-errors + (list + (json-parse-buffer :array-type 'list) + (json-parse-buffer :array-type 'list))) + while a + when (apply #'solver-compare a) + sum i + ;; when (= 84 i) + ;; do (apply #'solver-compare a) + )) + +(defun solver-compare (f s) + (cond + ((eq f s) 'next) + ((and (null f) (not (null s))) t) + ((and (not (null f)) (null s)) nil) + ((and (integerp f) (integerp s)) (< f s)) + ((and (integerp f) (consp s)) (solver-compare (list f) s)) + ((and (consp f) (integerp s)) (solver-compare f (list s))) + ((let ((result (solver-compare (car f) (car s)))) + (if (eq result 'next) (solver-compare (cdr f) (cdr s)) + result))))) + +(ert-deftest test-comparison () + (pcase-dolist (`(,f ,s ,r) + '((5 5 next) + (5 nil nil) + (nil 5 t) + (nil nil next) + ('(9) '((8 7)) nil) + ('(1 1 3 1 1) '(1 1 5 1 1) t) + ('((1) (2 3 4)) '((1) 4) t) + ('() '(5) t))) + (should (eq (solver-compare f s) r)))) -- cgit v1.2.3