Wednesday, December 02, 2026

Advent of Code 2016 - Day 25: Clock Signal

Part 1

Description

The input is a program for the same little computer as in Days 12 and 23, with one more instruction, out x, which sends the value of x as a signal. Part 1 asks for the lowest positive number to put in register a so that the program sends the signal 0, 1, 0, 1, 0, 1, … forever. 

Solution

I didn't run the program, I read it. It has two parts.

Lines 1 to 8 start with cpy a d. Then two nested loops add 1 to d a certain number of times: the first loop repeats 633 times, and the outer one repeats 4 times (the numbers on lines 2 and 3 of my program). So at the end d is a + 4 × 633, which is a + 2532.

Lines 9 to 30 are an endless loop:

  1. Copy d into a.
  2. Divide a by 2 (the loops count how many times 2 fits into it) and send the remainder with out. The remainder is the lowest binary digit.
  3. Do step 2 again with the quotient, until it is 0.
  4. Start again from the top with d.

So the program sends the binary digits of d, lowest digit first, over and over.

For the signal to be 0, 1, 0, 1, …, the binary digits of d have to alternate, with a 0 at the bottom and a 1 at the top: 10, 1010, 101010 and so on, which are the numbers 2, 10, 42, 170, 682, 2730, … Which numbers have alternating digits? The one with n digits is the number with all n digits set, 2^n − 1, minus the alternating number with n − 1 digits. So in a table, H is n, I is 2^n − 1, and J is I minus the previous J:

H3: =SEQUENCE(20,,0)
I3: =POWER(2,H3)-1
J3: 0
J4: =I4-J3            ← fill down to J22

J then reads 0, 1, 2, 5, 10, 21, 42, 85, 170, … and only the even numbers end in a 0, so only those start the signal with 0.

Because a has to be at least 1, d is at least 1 + 2532. So d is the first even number in column J that is larger than 2532, and a is that number minus 2532:

P10: =4*633
O10: =LET(j,J3:J22,MIN(FILTER(j,(j>P10)*ISEVEN(j))))
O11: =O10-P10

O11 is the answer. To check it, put the value in register a and run the program in the interpreter from Day 12 or Day 23: the signal alternates 0, 1, 0, 1, … In one-cell formula, it will look like:

=LET(
 input, $A$4:$A$33,
 split, TEXTSPLIT(TEXTJOIN("|",1,input)," ","|",),
 fac_1, --INDEX(split,2,2),
 fac_2, --INDEX(split,3,2),
 mul, fac_1*fac_2,
 sq, SEQUENCE(20,,0),
 jac, (2^(sq+2)-3-(-1)^sq)/6,
 MIN(FILTER(jac,(jac>mul)*ISNUMBER(jac)*(MOD(jac,2)=0)))-mul
)

Part 2

Description

There is no puzzle for the second star on Day 25. It is given automatically once all the other stars have been collected.


Share:

0 komentar:

Recent Posts

Categories

Blog Archive