1
+ import { fhmain } from "../../../src/fheader" ;
2
+ fhmain ( __filename ) ;
3
+ /*
4
+ input: string, lines: string[], dblines: string[][]
5
+ copy(text: string) → clipboard
6
+ error(message: string) → thrown error
7
+ -5..mod(3) → @mod(-5, 3)
8
+ */
9
+
10
+ // Cardinals:
11
+ // [[1,0],[-1,0],[0,1],[0,-1]]
12
+ // +Diagonals:
13
+ // [[1,0],[-1,0],[0,1],[0,-1],[-1,-1],[-1,1],[1,-1],[1,1]]
14
+
15
+ export { } ;
16
+
17
+ const practice = `
18
+ 880086C3E88112
19
+ ` ;
20
+ // input = practice;
21
+ input = input . trim ( ) ;
22
+
23
+ const hex2bin = ( hex : string ) => {
24
+ return [ ...hex ] . map ( c => parseInt ( c , 16 ) . toString ( 2 ) . padStart ( 4 , "0" ) ) . join ( "" ) ;
25
+ } ;
26
+
27
+ let data = [ ...hex2bin ( input ) . dwth ( log ) ] ;
28
+
29
+ function parse ( ) {
30
+ const version = data . splice ( 0 , 3 ) . join ( "" ) . use ( v => parseInt ( v , 2 ) ) ;
31
+ const typeid = data . splice ( 0 , 3 ) . join ( "" ) . use ( v => parseInt ( v , 2 ) ) ;
32
+
33
+ if ( typeid === 4 ) {
34
+ let bins = "" ;
35
+ while ( true ) {
36
+ const bit = data . shift ( ) ;
37
+ bins += data . splice ( 0 , 4 ) . join ( "" ) ;
38
+ if ( bit !== "1" ) break ;
39
+ }
40
+ return parseInt ( bins , 2 ) ;
41
+ // return [version];
42
+ }
43
+
44
+ const lentid = data . splice ( 0 , 1 ) . join ( "" ) . use ( v => parseInt ( v , 2 ) ) ;
45
+
46
+ const res : number [ ] = [ ] ;
47
+ if ( lentid === 0 ) {
48
+ const length = data . splice ( 0 , 15 ) . join ( "" ) . use ( v => parseInt ( v , 2 ) ) ;
49
+
50
+ const resv = data . length - length ;
51
+ while ( data . length > resv ) {
52
+ res . push ( parse ( ) ) ;
53
+ }
54
+ } else {
55
+ const iterc = data . splice ( 0 , 11 ) . join ( "" ) . use ( v => parseInt ( v , 2 ) ) ;
56
+
57
+ for ( let i = 0 ; i < iterc ; i ++ ) {
58
+ res . push ( parse ( ) ) ;
59
+ }
60
+ }
61
+ console . log ( res , typeid ) ;
62
+
63
+ if ( typeid === 0 ) {
64
+ return res . reduce ( ( t , a ) => t + a , 0 ) ;
65
+ } else if ( typeid === 1 ) {
66
+ return res . reduce ( ( t , a ) => t * a , 1 ) ;
67
+ } else if ( typeid === 2 ) {
68
+ return res . reduce ( ( t , a ) => Math . min ( t , a ) , Infinity ) ;
69
+ } else if ( typeid === 3 ) {
70
+ return res . reduce ( ( t , a ) => Math . max ( t , a ) , - Infinity ) ;
71
+ } else if ( typeid === 5 ) {
72
+ return res [ 0 ] > res [ 1 ] ? 1 : 0 ;
73
+ } else if ( typeid === 6 ) {
74
+ return res [ 0 ] < res [ 1 ] ? 1 : 0 ;
75
+ } else if ( typeid === 7 ) {
76
+ return res [ 0 ] === res [ 1 ] ? 1 : 0 ;
77
+ } else throw new Error ( "bad" )
78
+ }
79
+
80
+ parse ( ) . dwth ( log ) ;
0 commit comments