aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/13/solver.el
blob: c7452bb32ab8f25d233a421d210ae1d15c8f32fc (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
;;; solver.el --- DAy 13 -*- 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 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))))