;;; 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))))