@@ -56,7 +56,7 @@ public function testPattern():int{
5656 $ penalty = 0 ;
5757
5858 for ($ level = 1 ; $ level <= 4 ; $ level ++){
59- $ penalty += call_user_func ([$ this , 'testLevel ' .$ level ]);
59+ $ penalty += call_user_func_array ([$ this , 'testLevel ' .$ level], [ $ this -> matrix -> matrix ( true ) ]);
6060 }
6161
6262 return (int )$ penalty ;
@@ -65,12 +65,12 @@ public function testPattern():int{
6565 /**
6666 * Checks for each group of five or more same-colored modules in a row (or column)
6767 *
68- * @return float
68+ * @return int
6969 */
70- protected function testLevel1 (): float {
70+ protected function testLevel1 (array $ m ): int {
7171 $ penalty = 0 ;
7272
73- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
73+ foreach ($ m as $ y => $ row ){
7474 foreach ($ row as $ x => $ val ){
7575 $ count = 0 ;
7676
@@ -82,11 +82,11 @@ protected function testLevel1():float{
8282
8383 for ($ rx = -1 ; $ rx <= 1 ; $ rx ++){
8484
85- if (($ ry === 0 && $ rx === 0 ) || ($ x + $ rx < 0 || $ this ->moduleCount <= $ x + $ rx )){
85+ if (($ ry === 0 && $ rx === 0 ) || (( $ x + $ rx) < 0 || $ this ->moduleCount <= ( $ x + $ rx) )){
8686 continue ;
8787 }
8888
89- if ($ this -> matrix -> check ( $ x + $ rx , $ y + $ ry ) === ( $ val >> 8 > 0 ) ){
89+ if ($ m [ $ y + $ ry ][ $ x + $ rx ] === $ val ){
9090 $ count ++;
9191 }
9292
@@ -106,107 +106,90 @@ protected function testLevel1():float{
106106 /**
107107 * Checks for each 2x2 area of same-colored modules in the matrix
108108 *
109- * @return float
109+ * @return int
110110 */
111- protected function testLevel2 (): float {
111+ protected function testLevel2 (array $ m ): int {
112112 $ penalty = 0 ;
113113
114- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
114+ foreach ($ m as $ y => $ row ){
115115
116- if ($ y > $ this ->moduleCount - 2 ){
116+ if ($ y > ( $ this ->moduleCount - 2 ) ){
117117 break ;
118118 }
119119
120120 foreach ($ row as $ x => $ val ){
121121
122- if ($ x > $ this ->moduleCount - 2 ){
122+ if ($ x > ( $ this ->moduleCount - 2 ) ){
123123 break ;
124124 }
125125
126- $ count = 0 ;
127-
128- if ($ val >> 8 > 0 ){
129- $ count ++;
130- }
131-
132- if ($ this ->matrix ->check ($ y , $ x + 1 )){
133- $ count ++;
134- }
135-
136- if ($ this ->matrix ->check ($ y + 1 , $ x )){
137- $ count ++;
138- }
139-
140- if ($ this ->matrix ->check ($ y + 1 , $ x + 1 )){
141- $ count ++;
126+ if (
127+ $ val === $ m [$ y ][$ x + 1 ]
128+ && $ val === $ m [$ y + 1 ][$ x ]
129+ && $ val === $ m [$ y + 1 ][$ x + 1 ]
130+ ){
131+ $ penalty ++;
142132 }
143-
144- if ($ count === 0 || $ count === 4 ){
145- $ penalty += 3 ;
146- }
147-
148133 }
149134 }
150135
151- return $ penalty ;
136+ return 3 * $ penalty ;
152137 }
153138
154139 /**
155- * Checks if there are patterns that look similar to the finder patterns
140+ * Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio)
156141 *
157- * @return float
142+ * @return int
158143 */
159- protected function testLevel3 (): float {
160- $ penalty = 0 ;
144+ protected function testLevel3 (array $ m ): int {
145+ $ penalties = 0 ;
161146
162- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
147+ foreach ($ m as $ y => $ row ){
163148 foreach ($ row as $ x => $ val ){
164149
165- if ($ x <= $ this ->moduleCount - 7 ){
166- if (
167- $ this ->matrix ->check ($ x , $ y )
168- && !$ this ->matrix ->check ($ x + 1 , $ y )
169- && $ this ->matrix ->check ($ x + 2 , $ y )
170- && $ this ->matrix ->check ($ x + 3 , $ y )
171- && $ this ->matrix ->check ($ x + 4 , $ y )
172- && !$ this ->matrix ->check ($ x + 5 , $ y )
173- && $ this ->matrix ->check ($ x + 6 , $ y )
174- ){
175- $ penalty += 40 ;
176- }
150+ if (
151+ ($ x + 6 ) < $ this ->moduleCount
152+ && $ val
153+ && !$ m [$ y ][$ x + 1 ]
154+ && $ m [$ y ][$ x + 2 ]
155+ && $ m [$ y ][$ x + 3 ]
156+ && $ m [$ y ][$ x + 4 ]
157+ && !$ m [$ y ][$ x + 5 ]
158+ && $ m [$ y ][$ x + 6 ]
159+ ){
160+ $ penalties ++;
177161 }
178162
179- if ($ y <= $ this ->moduleCount - 7 ){
180- if (
181- $ this ->matrix ->check ($ x , $ y )
182- && !$ this ->matrix ->check ($ x , $ y + 1 )
183- && $ this ->matrix ->check ($ x , $ y + 2 )
184- && $ this ->matrix ->check ($ x , $ y + 3 )
185- && $ this ->matrix ->check ($ x , $ y + 4 )
186- && !$ this ->matrix ->check ($ x , $ y + 5 )
187- && $ this ->matrix ->check ($ x , $ y + 6 )
188- ){
189- $ penalty += 40 ;
190- }
163+ if (
164+ ($ y + 6 ) < $ this ->moduleCount
165+ && $ val
166+ && !$ m [$ y + 1 ][$ x ]
167+ && $ m [$ y + 2 ][$ x ]
168+ && $ m [$ y + 3 ][$ x ]
169+ && $ m [$ y + 4 ][$ x ]
170+ && !$ m [$ y + 5 ][$ x ]
171+ && $ m [$ y + 6 ][$ x ]
172+ ){
173+ $ penalties ++;
191174 }
192175
193176 }
194177 }
195178
196- return $ penalty ;
179+ return $ penalties * 40 ;
197180 }
198181
199182 /**
200183 * Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference
201184 *
202185 * @return float
203186 */
204- protected function testLevel4 ():float {
187+ protected function testLevel4 (array $ m ):float {
205188 $ count = 0 ;
206189
207- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
190+ foreach ($ m as $ y => $ row ){
208191 foreach ($ row as $ x => $ val ){
209- if ($ val >> 8 > 0 ){
192+ if ($ val ){
210193 $ count ++;
211194 }
212195 }
0 commit comments