Advent of Code, 2021.

 new new list compose Reply to this message Top page
Attachments:
+ (text/plain)

Delete this message
Author: Kaz Kylheku
Date:  
To: TXR Users
Subject: Advent of Code, 2021.
Hi All,

Anyone doing AoC?

Here is some TXR Lisp code for Day 8 (unscrambling seven segment display
wiring).

I put the input into a file called "input", which is the default arg of
some functions.

First part:

(defun solve (: (name "input"))
   (flow
     (file-get-lines name)
     (mapcar (do match `@a | @b` @1 b))
     (mapcar (opip (spl " ") (mapcar len)))
     (flatten)
     (count-if (op meql @1 2 4 3 7))))


Second part:

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