Skip to content

Instantly share code, notes, and snippets.

@jpjacobs
Last active June 12, 2025 20:14
Show Gist options
  • Select an option

  • Save jpjacobs/99fc32dad95a253d1975806a270e7562 to your computer and use it in GitHub Desktop.

Select an option

Save jpjacobs/99fc32dad95a253d1975806a270e7562 to your computer and use it in GitHub Desktop.
AoC Day 17 part 2 bug?
{{)n NB. This block has no code, just as explanation.
I couldn't come up with a more general solution, so I decompiled to code of my input, and converted to J:
i prog op arg J
0:2 4 bst A B =. 8 | A
1:1 3 bxl 3 B =. B xor 3
2:7 5 cdv B C =. A div 2^B
3:0 3 adv 3 A =. A div 2^3
4:1 5 bxl 5 B =. B xor 5
5:4 4 bxc C B =. B xor C
6:5 5 out B ob=. ob,8 | B
7:3 0 jnz 0 goto 0 if A~:0 => while. A do. ... end.
}}
NB. "check" is a simple version of the code above, combining the instructions where possible (see the numbers in the comments). It returns the output because the assignment to the output buffer is the last line of code executed.
check=:{{ NB. argument y is the initial value for A
'A B C'=. x: y, 0 0 NB. A, B, C registers
ob =. '' NB. output buffer, initially empty
'`xor div'=.22 b.`(<.@%) NB. shortcuts for xor and div.
while. A do. NB. 7 : jnz 0=restart while A
B =. 3 xor 8|A NB. 0 1: 3 xor of last 3 bits of A
C =. A div 2^B NB. 2
A =. A div 8 NB. 3
B =. C xor B xor 5 NB. 4 5: xor is symmetric...
ob =. ob , 8|B NB. 6
end.
}}
NB. "p2" solves part 2, by building up A from 0. It does so building up A 3 bits at a time, multiplying previous A by 8, and trying all 8 possible new lowest significance bits:
p2 =: {{ NB. solution to part 2; takes code to reproduce as y
A =. 0 NB. all possible partial A's so far, initially only 0
'`xor div'=. 22 b.`(<.@%) NB. xor and div, as before
B =. 3 xor i.8 NB. B candidates are always the same, as 3 bits to be added
B5=. 5 xor B NB. Same for B xor 5 in before-last line.
for_OV. |.y do. NB. Output values back-to-front in code (y).
AC=. 8 (i.@[ +/ *) A NB. add candidates for last 3 bits for each A (8xN)
C =. AC div 2^B NB. C candidates (8xN)
O =. 8|C xor B5 NB. Outputs for each candidate C
A =. ; AC <@:#~"1 O=OV NB. update A to keep A's where O is the OV needed.
end.
/:~ A NB. return a's sorted, so first should have been the solution.
}}
NB. When check and p2 are defined as above, you can check it is correct as shown below.
NB. Note, unindented lines are output, others are prompts:
code=: 2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0 NB.
],. As =: p2 code NB. solutions for A, ascending. the *second* one was accepted by AoC, not the first.
236539226447407
236539226447469
236539226447535
236539232738863
236539232738925
236539232738991
]code,_,check"0 As NB. original and generated code ~ As (_ as separator)
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
2 4 1 3 7 5 0 3 1 5 4 4 5 5 3 0
(code -: check)"0 As NB. Just to be sure, verify whether generated code is identical to original
1 1 1 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment