File tree Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Expand file tree Collapse file tree 1 file changed +4
-4
lines changed Original file line number Diff line number Diff line change @@ -44,16 +44,16 @@ function parse(input) {
4444//
4545// This solver does the reverse process:
4646// 1) Start from A=0
47- // 2) Find the smallest value that will output the end of the program
47+ // 2) Find the smallest value to add to A that will output the end of the program
4848// 3) A = A * 8
4949// 4) Repeat until it outputs the whole program
5050function solve2 ( program , registers , n = program . length - 1 ) {
51- if ( n < 0 ) return registers . A ;
5251 for ( let i = 0n ; i < 8n ; i ++ ) {
53- const rs = { ...registers , A : registers . A * 8n + i } ;
52+ const rs = { ...registers , A : registers . A + i } ;
5453 const { out } = run ( program , { ...rs } ) ;
5554 if ( out . join ( "," ) === program . slice ( n ) . join ( "," ) ) {
56- const result = solve2 ( program , rs , n - 1 ) ;
55+ if ( out . length === program . length ) return rs . A ;
56+ const result = solve2 ( program , { ...rs , A : rs . A * 8n } , n - 1 ) ;
5757 if ( result !== undefined ) return result ;
5858 }
5959 }
You can’t perform that action at this time.
0 commit comments