summaryrefslogtreecommitdiffstats
path: root/2021/08/two.tl
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-11-06 09:58:38 -0800
committerKaz Kylheku <kaz@kylheku.com>2022-11-06 09:58:38 -0800
commit4fd1aae518076adc8b97735225c678d6a362328d (patch)
tree97d61b659fc3cac628d0cdee71128a0baee2cb73 /2021/08/two.tl
downloadadvent-4fd1aae518076adc8b97735225c678d6a362328d.tar.gz
advent-4fd1aae518076adc8b97735225c678d6a362328d.tar.bz2
advent-4fd1aae518076adc8b97735225c678d6a362328d.zip
Kazinator's Advent of Code stuff.
Diffstat (limited to '2021/08/two.tl')
-rw-r--r--2021/08/two.tl42
1 files changed, 42 insertions, 0 deletions
diff --git a/2021/08/two.tl b/2021/08/two.tl
new file mode 100644
index 0000000..f436779
--- /dev/null
+++ b/2021/08/two.tl
@@ -0,0 +1,42 @@
+(defstruct entry ()
+ digs
+ value)
+
+(defun read-input (: (name "input"))
+ (flet ((str-to-syms (str)
+ (flow str
+ (spl " ")
+ (mapcar (opip (tuples 1)
+ (mapcar intern-fb))))))
+ (flow
+ (file-get-lines name)
+ (mapcar (do match `@a | @b` @1
+ (new entry
+ digs (str-to-syms a)
+ value (str-to-syms b)))))))
+
+(defun match-dig (syms)
+ (match-case syms
+ ((a b c e f g) 0)
+ ((c f) 1)
+ ((a c d e g) 2)
+ ((a c d f g) 3)
+ ((b c d f) 4)
+ ((a b d f g) 5)
+ ((a b d e f g) 6)
+ ((a c f) 7)
+ ((a b c d e f g) 8)
+ ((a b c d f g) 9)))
+
+(defmeth entry solve (e)
+ (each ((p (perm '(a b c d e f g))))
+ (let* ((map (relate '(a b c d e f g) p))
+ (interp (mapcar (opip (mapcar map) sort) e.digs))
+ (digs (remq nil [mapcar match-dig interp])))
+ (if (eql 10 (len digs))
+ (return (flow e.value
+ (mapcar (opip (mapcar map) sort match-dig))
+ (poly 10)))))))
+
+(defun solve (input)
+ (sum input .(solve)))