summaryrefslogtreecommitdiffstats
path: root/2021/08/two.tl
blob: f43677909c00b67f7d9682840b633ddff1080e10 (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
(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)))