Robbie Hatley's Solutions To The Weekly Challenge #223
For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle, usually with two parts, cycling every Sunday. You can find it here:
This week (2023-06-25 through 2023-07-01) is weekly challenge #223.
Task 1 is as follows:
"You are given a positive integer, $n. Write a script to find the total count of primes less than or equal to the given integer."
This is just a matter of counting all the odd numbers up to $n which are prime (plus 1 to account for 2):
Robbie Hatley's Solution to The Weekly Challenge 223-1
Task 2 is as follows:
You are given an array representing box coins, @box. Write a script to collect the maximum coins until you took out all boxes. If we pick box[i] then we collect the coins $box[i-1] * $box[i] * $box[i+1]. If $box[i+1] or $box[i-1] is out of bound then treat it as 1 coin.
Example 1: Input: @box = (3, 1, 5, 8) Output: 167 Step 1: pick box [i=1] and collected coins 3 * 1 * 5 => 15. Boxes available (3, 5, 8). Step 2: pick box [i=1] and collected coins 3 * 5 * 8 => 120. Boxes available (3, 8). Step 3: pick box [i=0] and collected coins 1 * 3 * 8 => 24. Boxes available (8). Step 4: pick box [i=0] and collected coins 1 * 8 * 1 => 8. No more box available. Example 2: Input: @box = (1, 5) Output: 10 Step 1: pick box [i=0] and collected coins 1 * 1 * 5 => 5. Boxes available (5). Step 2: pick box [i=0] and collected coins 1 * 5 * 1 => 5. No more box available.
Like all such "maximum" problem, this is just a matter of trying all possible orders of doing things to see which one yields the most-desirable results. Instead of actually removing emptied coin boxes from the array, I'll just mark them "X", which dramatically simplifies things by retaining the same set of indices always. Then it's just a matter of looking at all possible permutations of that set and seeing what results I get. As usual, Math::Combinatorics will come to my aid:
Robbie Hatley's Solution to The Weekly Challenge 223-2
That's it for 223; see you on 224!
Comments
Post a Comment