Day 7: Some Assembly Required

This commit is contained in:
Correl Roush 2015-12-14 17:20:05 -05:00
parent 90076448b4
commit cb954fa139

View file

@ -3173,3 +3173,533 @@ For example:
#+RESULTS[ceaf378dab9cbe4211307b258b4fe66d2a04482b]: #+RESULTS[ceaf378dab9cbe4211307b258b4fe66d2a04482b]:
: 14110788 : 14110788
* Day 7: Some Assembly Required
This year, Santa brought little Bobby Tables a set of wires and
[[https://en.wikipedia.org/wiki/Bitwise_operation][bitwise logic gates]]! Unfortunately, little Bobby is a little under the
recommended age range, and he needs help assembling the circuit.
Each wire has an identifier (some lowercase letters) and can carry a
[[https://en.wikipedia.org/wiki/16-bit][16-bit signal]] (a number from =0= to =65535=). A signal is provided to
each wire by a gate, another wire, or some specific value. Each wire
can only get a signal from one source, but can provide its signal to
multiple destinations. A gate provides no signal until all of its
inputs have a signal.
The included instructions booklet describes how to connect the parts
together: =x AND y -> z= means to connect wires =x= and =y= to an
=AND= gate, and then connect its output to wire =z=.
For example:
- =123 -> x= means that the signal =123= is provided to wire =x=.
- =x AND y -> z= means that the [[https://en.wikipedia.org/wiki/Bitwise_operation#AND][bitwise AND]] of wire =x= and wire =y=
is provided to wire =z=.
- =p LSHIFT 2 -> q= means that the value from wire =p= is [[https://en.wikipedia.org/wiki/Logical_shift][left-shifted]]
by =2= and then provided to wire =q=.
- =NOT e -> f= means that the =bitwise complement= of the value from
wire =e= is provided to wire =f=.
Other possible gates include =OR= ([[https://en.wikipedia.org/wiki/Bitwise_operation#OR][bitwise OR]]) and =RSHIFT=
([[https://en.wikipedia.org/wiki/Logical_shift][right-shift]]). If, for some reason, you'd like to *emulate* the
circuit instead, almost all programming languages (for example, [[https://en.wikipedia.org/wiki/Bitwise_operations_in_C][C]],
[[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators][JavaScript]], or [[https://wiki.python.org/moin/BitwiseOperators][Python]]) provide operators for these gates.
For example, here is a simple circuit:
#+BEGIN_EXAMPLE
123 -> x
456 -> y
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 -> g
NOT x -> h
NOT y -> i
#+END_EXAMPLE
After it is run, these are the signals on the wires:
#+BEGIN_EXAMPLE
d: 72
e: 507
f: 492
g: 114
h: 65412
i: 65079
x: 123
y: 456
#+END_EXAMPLE
In little Bobby's kit's instructions booklet (provided as your puzzle
input), what signal is ultimately provided to *wire a*?
----------------------------------------------------------------------
:HIDDEN:
#+name: 7-input
#+BEGIN_EXAMPLE
af AND ah -> ai
NOT lk -> ll
hz RSHIFT 1 -> is
NOT go -> gp
du OR dt -> dv
x RSHIFT 5 -> aa
at OR az -> ba
eo LSHIFT 15 -> es
ci OR ct -> cu
b RSHIFT 5 -> f
fm OR fn -> fo
NOT ag -> ah
v OR w -> x
g AND i -> j
an LSHIFT 15 -> ar
1 AND cx -> cy
jq AND jw -> jy
iu RSHIFT 5 -> ix
gl AND gm -> go
NOT bw -> bx
jp RSHIFT 3 -> jr
hg AND hh -> hj
bv AND bx -> by
er OR es -> et
kl OR kr -> ks
et RSHIFT 1 -> fm
e AND f -> h
u LSHIFT 1 -> ao
he RSHIFT 1 -> hx
eg AND ei -> ej
bo AND bu -> bw
dz OR ef -> eg
dy RSHIFT 3 -> ea
gl OR gm -> gn
da LSHIFT 1 -> du
au OR av -> aw
gj OR gu -> gv
eu OR fa -> fb
lg OR lm -> ln
e OR f -> g
NOT dm -> dn
NOT l -> m
aq OR ar -> as
gj RSHIFT 5 -> gm
hm AND ho -> hp
ge LSHIFT 15 -> gi
jp RSHIFT 1 -> ki
hg OR hh -> hi
lc LSHIFT 1 -> lw
km OR kn -> ko
eq LSHIFT 1 -> fk
1 AND am -> an
gj RSHIFT 1 -> hc
aj AND al -> am
gj AND gu -> gw
ko AND kq -> kr
ha OR gz -> hb
bn OR by -> bz
iv OR jb -> jc
NOT ac -> ad
bo OR bu -> bv
d AND j -> l
bk LSHIFT 1 -> ce
de OR dk -> dl
dd RSHIFT 1 -> dw
hz AND ik -> im
NOT jd -> je
fo RSHIFT 2 -> fp
hb LSHIFT 1 -> hv
lf RSHIFT 2 -> lg
gj RSHIFT 3 -> gl
ki OR kj -> kk
NOT ak -> al
ld OR le -> lf
ci RSHIFT 3 -> ck
1 AND cc -> cd
NOT kx -> ky
fp OR fv -> fw
ev AND ew -> ey
dt LSHIFT 15 -> dx
NOT ax -> ay
bp AND bq -> bs
NOT ii -> ij
ci AND ct -> cv
iq OR ip -> ir
x RSHIFT 2 -> y
fq OR fr -> fs
bn RSHIFT 5 -> bq
0 -> c
14146 -> b
d OR j -> k
z OR aa -> ab
gf OR ge -> gg
df OR dg -> dh
NOT hj -> hk
NOT di -> dj
fj LSHIFT 15 -> fn
lf RSHIFT 1 -> ly
b AND n -> p
jq OR jw -> jx
gn AND gp -> gq
x RSHIFT 1 -> aq
ex AND ez -> fa
NOT fc -> fd
bj OR bi -> bk
as RSHIFT 5 -> av
hu LSHIFT 15 -> hy
NOT gs -> gt
fs AND fu -> fv
dh AND dj -> dk
bz AND cb -> cc
dy RSHIFT 1 -> er
hc OR hd -> he
fo OR fz -> ga
t OR s -> u
b RSHIFT 2 -> d
NOT jy -> jz
hz RSHIFT 2 -> ia
kk AND kv -> kx
ga AND gc -> gd
fl LSHIFT 1 -> gf
bn AND by -> ca
NOT hr -> hs
NOT bs -> bt
lf RSHIFT 3 -> lh
au AND av -> ax
1 AND gd -> ge
jr OR js -> jt
fw AND fy -> fz
NOT iz -> ja
c LSHIFT 1 -> t
dy RSHIFT 5 -> eb
bp OR bq -> br
NOT h -> i
1 AND ds -> dt
ab AND ad -> ae
ap LSHIFT 1 -> bj
br AND bt -> bu
NOT ca -> cb
NOT el -> em
s LSHIFT 15 -> w
gk OR gq -> gr
ff AND fh -> fi
kf LSHIFT 15 -> kj
fp AND fv -> fx
lh OR li -> lj
bn RSHIFT 3 -> bp
jp OR ka -> kb
lw OR lv -> lx
iy AND ja -> jb
dy OR ej -> ek
1 AND bh -> bi
NOT kt -> ku
ao OR an -> ap
ia AND ig -> ii
NOT ey -> ez
bn RSHIFT 1 -> cg
fk OR fj -> fl
ce OR cd -> cf
eu AND fa -> fc
kg OR kf -> kh
jr AND js -> ju
iu RSHIFT 3 -> iw
df AND dg -> di
dl AND dn -> do
la LSHIFT 15 -> le
fo RSHIFT 1 -> gh
NOT gw -> gx
NOT gb -> gc
ir LSHIFT 1 -> jl
x AND ai -> ak
he RSHIFT 5 -> hh
1 AND lu -> lv
NOT ft -> fu
gh OR gi -> gj
lf RSHIFT 5 -> li
x RSHIFT 3 -> z
b RSHIFT 3 -> e
he RSHIFT 2 -> hf
NOT fx -> fy
jt AND jv -> jw
hx OR hy -> hz
jp AND ka -> kc
fb AND fd -> fe
hz OR ik -> il
ci RSHIFT 1 -> db
fo AND fz -> gb
fq AND fr -> ft
gj RSHIFT 2 -> gk
cg OR ch -> ci
cd LSHIFT 15 -> ch
jm LSHIFT 1 -> kg
ih AND ij -> ik
fo RSHIFT 3 -> fq
fo RSHIFT 5 -> fr
1 AND fi -> fj
1 AND kz -> la
iu AND jf -> jh
cq AND cs -> ct
dv LSHIFT 1 -> ep
hf OR hl -> hm
km AND kn -> kp
de AND dk -> dm
dd RSHIFT 5 -> dg
NOT lo -> lp
NOT ju -> jv
NOT fg -> fh
cm AND co -> cp
ea AND eb -> ed
dd RSHIFT 3 -> df
gr AND gt -> gu
ep OR eo -> eq
cj AND cp -> cr
lf OR lq -> lr
gg LSHIFT 1 -> ha
et RSHIFT 2 -> eu
NOT jh -> ji
ek AND em -> en
jk LSHIFT 15 -> jo
ia OR ig -> ih
gv AND gx -> gy
et AND fe -> fg
lh AND li -> lk
1 AND io -> ip
kb AND kd -> ke
kk RSHIFT 5 -> kn
id AND if -> ig
NOT ls -> lt
dw OR dx -> dy
dd AND do -> dq
lf AND lq -> ls
NOT kc -> kd
dy AND ej -> el
1 AND ke -> kf
et OR fe -> ff
hz RSHIFT 5 -> ic
dd OR do -> dp
cj OR cp -> cq
NOT dq -> dr
kk RSHIFT 1 -> ld
jg AND ji -> jj
he OR hp -> hq
hi AND hk -> hl
dp AND dr -> ds
dz AND ef -> eh
hz RSHIFT 3 -> ib
db OR dc -> dd
hw LSHIFT 1 -> iq
he AND hp -> hr
NOT cr -> cs
lg AND lm -> lo
hv OR hu -> hw
il AND in -> io
NOT eh -> ei
gz LSHIFT 15 -> hd
gk AND gq -> gs
1 AND en -> eo
NOT kp -> kq
et RSHIFT 5 -> ew
lj AND ll -> lm
he RSHIFT 3 -> hg
et RSHIFT 3 -> ev
as AND bd -> bf
cu AND cw -> cx
jx AND jz -> ka
b OR n -> o
be AND bg -> bh
1 AND ht -> hu
1 AND gy -> gz
NOT hn -> ho
ck OR cl -> cm
ec AND ee -> ef
lv LSHIFT 15 -> lz
ks AND ku -> kv
NOT ie -> if
hf AND hl -> hn
1 AND r -> s
ib AND ic -> ie
hq AND hs -> ht
y AND ae -> ag
NOT ed -> ee
bi LSHIFT 15 -> bm
dy RSHIFT 2 -> dz
ci RSHIFT 2 -> cj
NOT bf -> bg
NOT im -> in
ev OR ew -> ex
ib OR ic -> id
bn RSHIFT 2 -> bo
dd RSHIFT 2 -> de
bl OR bm -> bn
as RSHIFT 1 -> bl
ea OR eb -> ec
ln AND lp -> lq
kk RSHIFT 3 -> km
is OR it -> iu
iu RSHIFT 2 -> iv
as OR bd -> be
ip LSHIFT 15 -> it
iw OR ix -> iy
kk RSHIFT 2 -> kl
NOT bb -> bc
ci RSHIFT 5 -> cl
ly OR lz -> ma
z AND aa -> ac
iu RSHIFT 1 -> jn
cy LSHIFT 15 -> dc
cf LSHIFT 1 -> cz
as RSHIFT 3 -> au
cz OR cy -> da
kw AND ky -> kz
lx -> a
iw AND ix -> iz
lr AND lt -> lu
jp RSHIFT 5 -> js
aw AND ay -> az
jc AND je -> jf
lb OR la -> lc
NOT cn -> co
kh LSHIFT 1 -> lb
1 AND jj -> jk
y OR ae -> af
ck AND cl -> cn
kk OR kv -> kw
NOT cv -> cw
kl AND kr -> kt
iu OR jf -> jg
at AND az -> bb
jp RSHIFT 2 -> jq
iv AND jb -> jd
jn OR jo -> jp
x OR ai -> aj
ba AND bc -> bd
jl OR jk -> jm
b RSHIFT 1 -> v
o AND q -> r
NOT p -> q
k AND m -> n
as RSHIFT 2 -> at
#+END_EXAMPLE
:END:
#+BEGIN_SRC emacs-lisp :var input=7-input :exports both
(defun day7/parse-expression (expression-string)
(cond ((s-numeric? expression-string)
(string-to-int expression-string))
((s-contains? " " expression-string)
(let ((expression (-map #'day7/parse-expression
(split-string expression-string))))
(cond ((= 1 (length expression)) ;; single term
(car expression))
((= 3 (length expression)) ;; re-order infix operation
(list (cadr expression)
(car expression)
(caddr expression)))
(t expression))))
(t (intern (downcase expression-string)))))
(ert-deftest day7/parse-expression ()
(should (equal 123
(day7/parse-expression "123")))
(should (equal 'x
(day7/parse-expression "x")))
(should (equal 'ab
(day7/parse-expression "ab")))
(should (equal '(and x y)
(day7/parse-expression "x AND y"))))
(defun day7/parse-connection (connection-string)
(string-match "\\(.*?\\) -> \\(.*\\)"
connection-string)
(cons (day7/parse-expression (match-string 2 connection-string))
(day7/parse-expression (match-string 1 connection-string))))
(ert-deftest day7/parse-connection ()
(should (equal '(x . 123)
(day7/parse-connection "123 -> x")))
(should (equal '(d . (and x y))
(day7/parse-connection "x AND y -> d"))))
(defun day7/make-circuit (connection-strings)
(let ((expressions (-map #'day7/parse-connection
connection-strings))
(circuit (make-hash-table :size (length connection-strings))))
(-each expressions
(lambda (expr) (puthash (car expr)
(cdr expr)
circuit)))
circuit))
(defun day7/circuit-get (circuit wire)
(or (gethash wire circuit)
(error (format "Unknown wire: %s" wire))))
(defun day7/circuit-put (circuit wire value)
(puthash wire value circuit))
(defun day7/truncate-to-16bit (num)
(string-to-number
(substring (format "%04x" num) -4)
16))
(defun day7/and (a b)
(logand a b))
(defun day7/or (a b)
(logior a b))
(defun day7/not (n)
(day7/truncate-to-16bit (lognot n)))
(defun day7/rshift (n count)
(lsh n (- count)))
(defun day7/lshift (n count)
(day7/truncate-to-16bit (lsh n count)))
(defun day7/evaluate (circuit expr)
(cond ((numberp expr) (day7/truncate-to-16bit expr))
((symbolp expr)
;; Evaluate and store the result in the circuit table for
;; future lookups
(let ((result (day7/evaluate circuit
(day7/circuit-get circuit expr))))
(day7/circuit-put circuit expr result)
result))
((listp expr)
(let ((op (cond ((eq 'and (car expr)) #'day7/and)
((eq 'or (car expr)) #'day7/or)
((eq 'not (car expr)) #'day7/not)
((eq 'lshift (car expr)) #'day7/lshift)
((eq 'rshift (car expr)) #'day7/rshift)))
(args (-map (apply-partially 'day7/evaluate circuit)
(cdr expr))))
(apply op args)))
(t expr))
)
(ert-deftest day7/evaluate ()
(let* ((sample-input (list "123 -> x"
"456 -> y"
"x AND y -> d"
"x OR y -> e"
"x LSHIFT 2 -> f"
"y RSHIFT 2 -> g"
"NOT x -> h"
"NOT y -> i"))
(circuit (day7/make-circuit sample-input)))
(should (eq 72 (day7/evaluate circuit 'd)))
(should (eq 507 (day7/evaluate circuit 'e)))
(should (eq 492 (day7/evaluate circuit 'f)))
(should (eq 114 (day7/evaluate circuit 'g)))
(should (eq 65412 (day7/evaluate circuit 'h)))
(should (eq 65079 (day7/evaluate circuit 'i)))
(should (eq 123 (day7/evaluate circuit 'x)))
(should (eq 456 (day7/evaluate circuit 'y)))))
(let* ((connection-strings (split-string input "\n" t))
(circuit (day7/make-circuit connection-strings)))
(day7/evaluate circuit 'a))
#+END_SRC
#+RESULTS[0e096ea6071516679cc36a305883d2bdd808c930]:
: 956