@@ -52,7 +52,7 @@ const parse = (input: string): Packet => {
52
52
return packet
53
53
}
54
54
55
- const compare = ( packet1 : Packet | undefined , packet2 : Packet | undefined , iterator1 : number = 0 , iterator2 : number = 0 ) : boolean | undefined => {
55
+ const compare = ( packet1 : Packet | undefined , packet2 : Packet | undefined , iterator1 : number = 0 , iterator2 : number = 0 , single1 : boolean = false , single2 : boolean = false ) : boolean | undefined => {
56
56
const data1 = packet1 ?. data
57
57
const data2 = packet2 ?. data
58
58
if ( data1 === undefined ) {
@@ -61,29 +61,33 @@ const compare = (packet1: Packet | undefined, packet2: Packet | undefined, itera
61
61
return false
62
62
}
63
63
console . log ( 'compare levels' , packet1 ?. level , packet2 ?. level )
64
- const len1 = data1 . length
65
- const len2 = data2 . length
64
+ const len1 = single1 ? 1 : data1 . length
65
+ const len2 = single2 ? 1 : data2 . length
66
66
let it1 = iterator1
67
67
let it2 = iterator2
68
68
let result : boolean | undefined = undefined ;
69
69
while ( true ) {
70
+ console . log ( it1 , len1 , it2 , len2 , result )
70
71
if ( it1 === len1 && it2 === len2 ) {
71
72
return result
72
73
}
73
- if ( it1 === len1 ) {
74
- return true
74
+ if ( it1 === len1 && it2 < len2 ) {
75
+ return result === undefined ? true : false
75
76
}
76
- if ( it2 === len2 ) {
77
+ if ( it1 < len1 && it2 === len2 ) {
77
78
return false
78
79
}
79
80
const tmp1 = data1 [ it1 ]
80
81
const tmp2 = data2 [ it2 ]
81
82
if ( tmp1 instanceof Packet && tmp2 instanceof Packet ) {
83
+ console . log ( 'next packet' , 'next packet' )
82
84
result = compare ( tmp1 , tmp2 )
83
85
} else if ( tmp1 instanceof Packet && ! ( tmp2 instanceof Packet ) ) {
84
- result = compare ( tmp1 , packet2 , 0 , it2 )
86
+ console . log ( 'next packet' , it2 )
87
+ result = compare ( tmp1 , packet2 , 0 , it2 , false , true )
85
88
} else if ( ! ( tmp1 instanceof Packet ) && tmp2 instanceof Packet ) {
86
- result = compare ( packet1 , tmp2 , it1 , 0 )
89
+ console . log ( it1 , 'next packet' )
90
+ result = compare ( packet1 , tmp2 , it1 , 0 , false , false )
87
91
} else if ( typeof tmp1 === 'number' && typeof tmp2 === 'number' ) {
88
92
console . log ( '\t' , tmp1 , tmp2 )
89
93
if ( tmp1 < tmp2 ) {
@@ -95,8 +99,8 @@ const compare = (packet1: Packet | undefined, packet2: Packet | undefined, itera
95
99
throw 'something went wrong'
96
100
}
97
101
98
- if ( ! ! result ) {
99
- return true
102
+ if ( result === true || result === false ) {
103
+ return result
100
104
}
101
105
102
106
it1 += 1
@@ -134,6 +138,10 @@ class Solve13 extends FileReader {
134
138
pair . part1 = parse ( data [ rowIndex ] )
135
139
} else if ( + rowIndex % 3 === 1 ) {
136
140
pair ! . part2 = parse ( data [ rowIndex ] )
141
+ if ( pairs . length === 60 ) {
142
+ console . log ( data [ + rowIndex - 1 ] )
143
+ console . log ( data [ rowIndex ] )
144
+ }
137
145
}
138
146
}
139
147
@@ -144,8 +152,8 @@ class Solve13 extends FileReader {
144
152
let result = 0
145
153
pairs . forEach ( ( p : Pair , index : number ) => {
146
154
const cmp = compare ( p . part1 , p . part2 )
147
- console . log ( '*** ' , index + 1 , cmp )
148
- if ( cmp ) {
155
+ console . log ( index + 1 , cmp )
156
+ if ( cmp === true ) {
149
157
result += index + 1
150
158
}
151
159
} )
0 commit comments