Robbie Hatley's Perl Solutions To The Weekly Challenge #210

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:

The Weekly Challenge

This week (2023-03-26 through 2023-04-01) is weekly challenge #210.

Task 1 is as follows:

Write a script to get the maximum "points" possible from "taking out" or "killing" (removing) integers from a list. For each integer you remove, all integers exactly one-less or one-more will also be removed. The "points" will be the total of integers removed. What integers should you remove to get maximum points?

That's pretty straightforward as long as only positive integers are involved, but it gets a bit trickier if 0s or negative integers are present. For max score, one should manually remove all positive integers, and only positive integers, never 0s (might reduce score) or negative integers (ALWAYS reduces score).

As with many of my solutions, I used a 3-part loop:

Robbie Hatley's Perl Solution to The Weekly Challenge 210-1

Task 2 is as follows:

You are given an array of integers which can move to the right if they are positive or to the left if they are negative. If two of these integers collide, then the one with the smaller absolute value will explode. If both have same absolute value, then they both explode. All numbers move at the same speed, therefore any 2 numbers moving in the same direction will never collide. Write a script to find out who survives the collision.

If one-or-more zeros are present, then one has to make some assumptions, because the rules don't specify what to do about zeros. After thinking about it, I realized there are (at least) 15 different ways to interpret zeros. Firstly, are they left-moving, stationary, or right-moving? And for each of those, when they collide, do they ghost, block, destroy other, destroy self, or destroy both? That's 3x5=15 possibilities.

For my solution I'll assume that zeros are "moving rightward", because the rules say "all numbers move at same speed", and speed is absolute value of velocity, so zeros must move at either +v or -v. Because zeros are conceptually closer to being positive than negative (because "natural numbers" includes zero and positive integers, but not negative integers), I'll assume +v.

I'll also assume that zeros "collide" rather than "ghost" or "block" when they come into contact with other numbers, because the rules mention only "collision" as a possible way for these integers to interact.

I'll also assume:
0 + 0 => both explode, because equal absolute value
0 + positive => 0 explodes because it has less absolute value.
0 + negative => 0 explodes because it has less absolute value.

Yep, you guessed it, I used 3-part loop again, but this time with some fancy backtracking:
Start at 1 and see if previous+current will collide.
If destroy current only, splice and backtrack 1.
If destroy previous or both, splice and backtrack 2.
++$i at top of loop and keep looping while $i <= $#_ .

Robbie's Solution to TWC 210-2

That's it for 210; see you on 211!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #266

Robbie Hatley's Solutions To The Weekly Challenge #273