Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #312 ("Minimum Time" and "Balls and Boxes")

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, cycling every Sunday. You can find it here:

The Weekly Challenge

The Weekly Challenge for the week of 2025-03-10 through 2025-03-16 is #312.

The tasks for challenge #312 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 312-1: Minimum Time
Submitted by: Mohammad Sajid Anwar
You are given a typewriter with lowercase english letters a to z
arranged in a circle. Typing a character takes 1 second. Moving
the pointer one character clockwise or anti-clockwise also takes
1 second. The pointer initially points to a. Write a script to
return minimum time it takes to print a given string.

Example #1:
Input: $str = "abc"
Output: 5
The pointer is at 'a' initially.
1 sec - type the letter 'a'
1 sec - move pointer clockwise to 'b'
1 sec - type the letter 'b'
1 sec - move pointer clockwise to 'c'
1 sec - type the letter 'c'

Example 2
Input: $str = "bza"
Output: 7
The pointer is at 'a' initially.
1 sec - move pointer clockwise to 'b'
1 sec - type the letter 'b'
1 sec - move pointer anti-clockwise to 'a'
1 sec - move pointer anti-clockwise to 'z'
1 sec - type the letter 'z'
1 sec - move pointer clockwise to 'a'
1 sec - type the letter 'a'

Example 3
Input: $str = "zjpc"
Output: 34

This is essentially a modular-arithmetic problem. Let $orig be the ASCII ordinal of the current location, and let $dest be the ASCII ordinal of the destination. Then if ($dest-$orig)%26 < 14, move pointer clockwise, else move pointer counterclockwise. That will give minimum distance traveled, and hence minimum time.

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 312-2: Balls and Boxes
Submitted by: Mohammad Sajid Anwar
There are $n balls of mixed colors: red, blue or green. They are
all distributed in 10 boxes labelled 0-9. You are given a string
describing the location of balls. Write a script to find the
number of boxes containing all three colors. Return 0 if none
are found.

Example #1:
Input: $str = "G0B1R2R0B0"
Output: 1
The given string describes there are 5 balls as below:
Box 0: Green(G0), Red(R0), Blue(B0) => 3 balls
Box 1: Blue(B1) => 1 ball
Box 2: Red(R2) => 1 ball

Example #2:
Input: $str = "G1R3R6B3G6B1B6R1G3"
Output: 3
The given string describes there are 9 balls as below:
Box 1: Red(R1), Blue(B1), Green(G1) => 3 balls
Box 3: Red(R3), Blue(B3), Green(G3) => 3 balls
Box 6: Red(R6), Blue(B6), Green(G6) => 3 balls

Example #3:
Input: $str = "B3B2G1B3"
Output: 0
Box 1: Green(G1) => 1 ball
Box 2: Blue(B2)  => 1 ball
Box 3: Blue(B3)  => 2 balls

I'll just make an array of boxes with one string in each box, then ".=" an R, G, or B to the appropriate box's string for each letter/number pair, then count Frecce Tricolori strings (that is, strings which have all three colors). All apparent allusions to Malvina Reynolds' song "Little Boxes", and to "313° Gruppo Addestramento Acrobatico Pattuglia Acrobatica Nazionale Frecce Tricolori", and to The Ramstein Airshow Disaster, are purely intentional. Some history should not be forgotten. (EG, we forgot Hitler then did it all over again on 5 November 2024, and now USA is controlled by a pair of mentally-unstable billionare oligarch fascists. Oops.)

Robbie Hatley's Perl Solution to The Weekly Challenge 312-2

That's it for challenge 312; see you on challenge 312!

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