Compare commits
2 Commits
a4856cf9ef
...
09327d5a35
Author | SHA1 | Date | |
---|---|---|---|
09327d5a35 | |||
3475d23962 |
58
20/graphviz.dot
Normal file
58
20/graphviz.dot
Normal 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
|
45
20/part1.pl
45
20/part1.pl
@ -2,51 +2,10 @@
|
||||
:- use_module(library(dcg/basics)).
|
||||
:- initialization(main, main).
|
||||
|
||||
% ================================ begin stupid ===============================
|
||||
main([FileName|_]) :-
|
||||
input(FileName, Circuit),
|
||||
cycle(Circuit, 0, [], [], First-Cycle-_, Counts),
|
||||
NCycles is 1000 div Cycle, Leftovers is 1000 mod Cycle,
|
||||
CycleEnd1 is First + Cycle,
|
||||
|
||||
slice(Counts, 0, First, FirstCounts),
|
||||
sumpairs(FirstCounts, FirstL-FirstH),
|
||||
slice(Counts, First, CycleEnd1, CycleCounts),
|
||||
sumpairs(CycleCounts, CycleL-CycleH),
|
||||
slice(Counts, First, Leftovers, LeftoverCounts),
|
||||
sumpairs(LeftoverCounts, LeftoverL-LeftoverH),
|
||||
|
||||
Answer is (FirstL + NCycles*CycleL + LeftoverL)*
|
||||
(FirstH + NCycles*CycleH + LeftoverH),
|
||||
write("answer="), write(Answer), nl.
|
||||
|
||||
sumpairs([], 0-0).
|
||||
sumpairs([A-B|List], SumA-SumB) :-
|
||||
sumpairs(List, A1-B1), SumA is A + A1, SumB is B + B1.
|
||||
|
||||
cycle(Circuit, N, Hist, Counts, First-Cycle-Hist, Counts) :-
|
||||
match(Circuit, Hist, Cycle),
|
||||
write("match. N="), write(N), write(", cycle="), write(Cycle), nl,
|
||||
First is N - Cycle,
|
||||
!.
|
||||
cycle(Circuit, N, Hist, Counts, X, Y) :-
|
||||
% \+ match(Circuit, Hist, _), % remove this for performance.
|
||||
run([button-l-broadcaster], Circuit, NewCircuit, Ls-Hs),
|
||||
NewN is N + 1,
|
||||
NewHist = [Circuit|Hist],
|
||||
% write(NewN), write(": "), write(Ls-Hs), write(" - "), write(NewCircuit), nl,
|
||||
cycle(NewCircuit, NewN, NewHist, [Ls-Hs|Counts], X, Y).
|
||||
|
||||
match(Map, [Map|_], 1) :- !.
|
||||
match(Map, [_|Entries], N) :-
|
||||
match(Map, Entries, NextN),
|
||||
N is NextN + 1.
|
||||
|
||||
slice(L, From, To, R):-
|
||||
ToX is To + 1,
|
||||
length(LFrom, From), length([_|LTo], ToX),
|
||||
append(LTo, _, L), append(LFrom, R, LTo).
|
||||
% ================================ end stupid ===============================
|
||||
run1000(Circuit, Answer),
|
||||
write(Answer), nl.
|
||||
|
||||
% part1
|
||||
run1000(Circuit, Answer) :-
|
||||
|
82
20/part2.pl
Normal file
82
20/part2.pl
Normal 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
1015
20/part2.svg
Normal file
File diff suppressed because it is too large
Load Diff
After (image error) Size: 52 KiB |
14
20/part2.txt
Normal file
14
20/part2.txt
Normal file
@ -0,0 +1,14 @@
|
||||
gh <- [qx, zf, cd, rk]
|
||||
|
||||
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
|
Loading…
Reference in New Issue
Block a user