aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/13/solver.el
diff options
context:
space:
mode:
authorOscar Najera <hi@oscarnajera.com>2022-12-13 13:59:03 +0100
committerOscar Najera <hi@oscarnajera.com>2022-12-13 13:59:03 +0100
commit1ca84c3abd40e81b4165b34ec0ca0a2ae317154c (patch)
tree36d5b4b6ca634b4c4fce93249c880070368b44be /AoC2022/13/solver.el
parent07f009aef0270675ebdb2b2f7667515c8e6fbe14 (diff)
downloadscratch-1ca84c3abd40e81b4165b34ec0ca0a2ae317154c.tar.gz
scratch-1ca84c3abd40e81b4165b34ec0ca0a2ae317154c.tar.bz2
scratch-1ca84c3abd40e81b4165b34ec0ca0a2ae317154c.zip
[AoC2022] Elisp 13
Diffstat (limited to 'AoC2022/13/solver.el')
-rw-r--r--AoC2022/13/solver.el55
1 files changed, 55 insertions, 0 deletions
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 <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))))