1- function run ( a : number , b : number , c : number , program : number [ ] ) {
1+ function run ( { a , b, c } : Record < "a" | "b" | "c" , bigint > , program : number [ ] ) {
22 const output : number [ ] = [ ] ;
3- function combo ( operand : number ) {
4- if ( operand < 4 ) return operand ;
3+ function combo ( operand : number ) : bigint {
54 switch ( operand ) {
65 case 0 :
76 case 1 :
87 case 2 :
9- return operand ;
8+ case 3 :
9+ return BigInt ( operand ) ;
1010 case 4 :
1111 return a ;
1212 case 5 :
@@ -18,7 +18,7 @@ function run(a: number, b: number, c: number, program: number[]) {
1818 }
1919 }
2020 function div ( operand : number ) {
21- return Math . trunc ( a / 2 ** combo ( operand ) ) ;
21+ return a / 2n ** combo ( operand ) ;
2222 }
2323 for ( let ip = 0 ; ip in program ; ) {
2424 const opcode = program [ ip ++ ] ;
@@ -28,10 +28,10 @@ function run(a: number, b: number, c: number, program: number[]) {
2828 a = div ( operand ) ;
2929 break ;
3030 case 1 : // bxl
31- b = b ^ operand ;
31+ b = b ^ BigInt ( operand ) ;
3232 break ;
3333 case 2 : // bst
34- b = combo ( operand ) % 8 ;
34+ b = combo ( operand ) % 8n ;
3535 break ;
3636 case 3 : // jnz
3737 if ( a ) ip = operand ;
@@ -40,7 +40,7 @@ function run(a: number, b: number, c: number, program: number[]) {
4040 b ^= c ;
4141 break ;
4242 case 5 : // out
43- output . push ( combo ( operand ) % 8 ) ;
43+ output . push ( Number ( combo ( operand ) % 8n ) ) ;
4444 break ;
4545 case 6 : // bdv
4646 b = div ( operand ) ;
@@ -55,7 +55,7 @@ function run(a: number, b: number, c: number, program: number[]) {
5555
5656export default function solve ( input : string ) {
5757 const [ top , bottom ] = input . split ( "\n\n" ) ;
58- const [ a , b , c ] = top . matchAll ( / \d + / g) ! . map ( ( [ v ] ) => + v ) ;
58+ const [ a , b , c ] = top . matchAll ( / \d + / g) ! . map ( ( [ v ] ) => BigInt ( v ) ) ;
5959 const program = bottom . split ( ": " ) [ 1 ] . split ( "," ) . map ( Number ) ;
60- return run ( a , b , c , program ) . join ( "," ) ;
60+ return run ( { a, b, c } , program ) . join ( "," ) ;
6161}
0 commit comments