@@ -17,6 +17,7 @@ class Monkey
17
17
protected int $ ifTrueMonkey = 0 ;
18
18
protected int $ ifFalseMonkey = 0 ;
19
19
protected int $ inspections = 0 ;
20
+ protected int $ commonDivisor = 1 ;
20
21
21
22
public function __construct (array $ data )
22
23
{
@@ -49,7 +50,7 @@ public function printInspections()
49
50
echo "Monkey {$ this ->nr }: {$ this ->inspections }\n" ;
50
51
}
51
52
52
- public function calculateTurn ($ monkeyList )
53
+ public function calculateTurn ($ monkeyList, $ dontWorryBeHappy )
53
54
{
54
55
$ itemCount = count ($ this ->items );
55
56
for ($ i = 0 ; $ i < $ itemCount ; $ i += 1 ) {
@@ -67,7 +68,16 @@ public function calculateTurn($monkeyList)
67
68
echo "\nUNKNOWN OPERATOR ' {$ this ->operationType }' \n" ;
68
69
exit (0 );
69
70
}
70
- $ item = (int )($ item / self ::WORRY_DIV );
71
+ if ($ dontWorryBeHappy ) {
72
+ $ item = (int )($ item / self ::WORRY_DIV );
73
+ } else {
74
+ if ($ this ->commonDivisor === 1 ) {
75
+ foreach ($ monkeyList as $ monkey ) {
76
+ $ this ->commonDivisor *= $ monkey ->getDivisionTestValue ();
77
+ }
78
+ }
79
+ $ item = (int )($ item % $ this ->commonDivisor );
80
+ }
71
81
if ($ item % $ this ->divisionTestValue == 0 ) {
72
82
$ monkeyList [$ this ->ifTrueMonkey ]->receiveItem ($ item );
73
83
} else {
@@ -85,4 +95,9 @@ public function getInspections(): int
85
95
{
86
96
return $ this ->inspections ;
87
97
}
98
+
99
+ public function getDivisionTestValue (): int
100
+ {
101
+ return $ this ->divisionTestValue ;
102
+ }
88
103
}
0 commit comments