This commit is contained in:
Dory 2023-12-20 03:55:55 -08:00
parent 3475d23962
commit 63921cea50
4 changed files with 1171 additions and 0 deletions

58
20/graphviz.dot Normal file
View File

@ -0,0 +1,58 @@
hb -> mj
mx -> mt, NANDxz
xh -> qc
tg -> cq
kp -> NANDxz, nj
mj -> NANDjj, lv
cq -> jm
mt -> sj, NANDxz
NANDjj -> hb, lz, NANDrk, xv, vj, vh, lv
rm -> NANDbz, xq
hx -> NANDbz
xv -> lz
xx -> kp, NANDxz
pt -> vx
NANDxz -> bq, gr, sj, rv, NANDzf
vx -> NANDgf, cv
xb -> NANDxz, bq
xk -> NANDgf, rd
lv -> zk
NANDrk -> NANDgh
kn -> NANDgf, tz
NANDgh -> rx
sj -> vp
jm -> vm, NANDbz
rr -> rv, NANDxz
tz -> rz
gg -> kn
NANDcd -> NANDgh
qc -> kh, NANDbz
kb -> NANDgf
vp -> NANDxz, xx
fb -> NANDbz, tg
rd -> cp
qn -> vh, NANDjj
xr -> NANDjj
tp -> rm, NANDbz
cp -> gg
NANDbz -> NANDqx, cq, xh, fb, tg
qq -> pt, NANDgf
xq -> NANDbz, hx
gx -> NANDjj, qv
bq -> rr
cv -> NANDgf, kb
zk -> NANDjj, xv
NANDzf -> NANDgh
NANDqx -> NANDgh
vh -> gx
qv -> xr, NANDjj
lz -> qn
broadcaster -> fb, xk, gr, vj
nj -> NANDxz
gr -> NANDxz, xb
kh -> tp, NANDbz
vm -> NANDbz, xh
rz -> qq, NANDgf
NANDgf -> tz, NANDcd, rd, xk, pt, cp, gg
rv -> mx
vj -> hb, NANDjj

82
20/part2.pl Normal file
View File

@ -0,0 +1,82 @@
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- initialization(main, main).
% This code was only used to confirm the theory. Don't run it. Look at part2.txt.
run_until(Circuit, _, Node, State, N) :- member(_-Node-State-_, Circuit), write(N), nl, !.
run_until(Circuit, Init, Node, State, N) :-
maplist(extract(Circuit), [xr, qv, gx, vh, qn, lz, xv, zk, lv, mj, hb, vj], S1),
maplist(extract(Circuit), [nj, kp, xx, vp, sj, mt, mx, rv, rr, bq, xb, gr], S2),
maplist(extract(Circuit), [hx, xq, rm, tp, kh, qc, xh, vm, jm, cq, tg, fb], S3),
maplist(extract(Circuit), [kb, cv, vx, pt, qq, rz, tz, kn, gg, cp, rd, xk], S4),
write(N), write(": "),
string_codes(Str1, S1), string_codes(Str2, S2), string_codes(Str3, S3), string_codes(Str4, S4),
write(Str1), write(" "), write(Str2), write(" "), write(Str3), write(" "), write(Str4), nl,
run([Init], Circuit, CircuitNext),
NextN is N + 1,
run_until(CircuitNext, Init, Node, State, NextN).
extract(Circuit, Cell, AsciiState) :-
member(_-Cell-StateX-_, Circuit),
AsciiState is StateX + 48.
% code to run one circuit
run([], Circuit, Circuit) :- !.
run([Src-Level-Target|Pulses], CircuitIn, CircuitOut) :-
( member(Type-Target-State-Dests, CircuitIn)
-> /*write([Src, Level, Target]), write(" -> "),
write([Type, Target, State, Dests]), write(" = "),*/
call(Type, Src, Level, State, NewState, Out),
send(Target, Out, Dests, AdditionalPulses),
% write(NewState-AdditionalPulses), nl,
select(Type-Target-State-Dests, CircuitIn,
Type-Target-NewState-Dests, Circuit1),
append(Pulses, AdditionalPulses, NewPulses),
run(NewPulses, Circuit1, CircuitOut)
; run(Pulses, CircuitIn, CircuitOut)
).
broadcaster(_, l, x, x, l).
ff(_, h, State, State, none).
ff(_, l, 0, 1, h).
ff(_, l, 1, 0, l).
nand(Src, Level, State, NewState, OutLevel) :-
select(Src-_, State, Src-Level, NewState),
(maplist([_-h]>>(true), NewState) -> OutLevel = l; OutLevel = h).
send(_, none, _, []).
send(From, Level, Dests, Pulses) :-
\+ Level = none,
maplist({Level}/[Dest, From-Level-Dest]>>(true), Dests, Pulses).
% input initialization
prefill_nands([], Circuit, Circuit).
prefill_nands([_-Src-_-Dests|Nodes], CircuitIn, CircuitOut) :-
convlist(
{CircuitIn}/[Dest, Dest]>>(member(nand-Dest-_-_, CircuitIn)),
Dests, NandDests),
foldl(fill_one_nand(Src), NandDests, CircuitIn, Circuit1),
prefill_nands(Nodes, Circuit1, CircuitOut).
fill_one_nand(Src, Nand, CIn, COut) :-
select(nand-Nand-State-Dests, CIn, nand-Nand-[Src-l|State]-Dests, COut).
% input parsing stuff below
input(FileName, Circuit) :-
phrase_from_file(modules(EmptyCircuit), FileName),
prefill_nands(EmptyCircuit, EmptyCircuit, Circuit).
modules([]) --> eos, !.
modules([Module|Modules]) --> module(Module), "\n", modules(Modules).
module(broadcaster-broadcaster-x-Dests) --> "broadcaster -> ", dests(Dests).
module(ff-Name-0-Dests) --> "%", node(Name), " -> ", dests(Dests).
module(nand-Name-[]-Dests) --> "&", node(Name), " -> ", dests(Dests).
dests([Dest]) --> node(Dest).
dests([Dest|Dests]) --> node(Dest), ", ", dests(Dests).
node(Name) --> string_without(", \n", NameStr), {atom_codes(Name, NameStr)}.

1015
20/part2.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 52 KiB

16
20/part2.txt Normal file
View File

@ -0,0 +1,16 @@
gh <- [qx, zf, cd, rk]
Each of the 4 wings is a 12-bit counter, with custom resets at...
xr, qv, gx, vh, qn, lz, xv, zk, lv, mj, hb, vj (jj) <- (111010010101) vj, qv, zk, gx, xr, qn, mj
nj, kp, xx, vp, sj, mt, mx, rv, rr, bq, xb, gr (xz) <- (111101101011) gr, nj, vp, rr, xb, xx, mt, kp, mx
hx, xq, rm, tp, kh, qc, xh, vm, jm, cq, tg, fb (bz) <- (111111011001) vm, kh, xq, tp, fb, qc, jm, hx, rm
kb, cv, vx, pt, qq, rz, tz, kn, gg, cp, rd, xk (gf) <- (111011010001) rz, cv, qq, kb, kn, xk, vx
111010010101 = 3733
111101101011 = 3947
111111011001 = 4057
111011010001 = 3793
All 4 are primes.
LCD = 226732077152351