Robbie Hatley's Solutions To The Weekly Challenge #281

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 2024-08-04 through 2024-08-10 is #281. Its tasks are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 281-1: Check Color
Submitted by: Mohammad S Anwar
Write a script which, given the coordinates of a square on a
chessboard in algebraic notation, prints "true" if the square
is white, "false" if the square is black.
Example 1: Input: 'd3'  Output: 'true'
Example 2: Input: 'g5'  Output: 'false'
Example 3: Input: 'e6'  Output: 'true'

The is just a matter of calculating the parity of the sum of horizontal and vertical distances relative to square a1 (black). If the combined parity is 0, the output is 0/black/false. If the combined parity is 1, the output is 1/white/true.

   sub parity ($x) {
      return 'error' if $x !~ m/^[a-h][1-8]$/;
      ((ord(substr($x,0,1))-97)+(ord(substr($x,1,1))-49))%2
   }

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 281-2: Knight’s Move
Submitted by: Peter Campbell Smith
Write a script which calculates the minimum number of moves for
a knight to get from one square to another on a chess board,
given the start and end locations in algebraic notation.

Example 1:
Input: ['g2', 'a8']
Ouput: 4
Path:  g2 -> e3 -> d5 -> c7 -> a8

Example 2:
Input: ['g2', 'h2']
Ouput: 3
Path:  g2 -> e3 -> f1 -> h2

I suppose I could use recursion for this and use recursion depth as "hops" counter, but that would involve accessing global variables from within functions (which seems nasty) and deep stacks (which I don't like). So I'll use a variant of Breadth-First Search. I'll make a hash "%Squares" of all 64 squares, with their keys being their algebraic notations and their values being [visited, hops] all initialized to [0,inf] except for start which is [0,0]. I'll make a variable $current to keep track of "current hops from start" and initialize it to $start. I'll then process the itinerary for so long as its not empty by doing the following:

  1. Shift each square in the itinerary from the left into variable "$next".
  2. Set "hops" for next to min(existing, current+1).
  3. Set current = next.
  4. Set current as "visited".
  5. Get "octopus" of squares which can be visited next.
  6. For each "arm", set its hops to min(existing, 1+current).
  7. Push each unvisited arm onto right of itinerary.

After the queue empties, $squares{$end}->[1] should contain "minimum hops to get from start to end".

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

That's it for challenge 281; see you on challenge 282!

Comments

Popular posts from this blog

Robbie Hatley's Solutions To The Weekly Challenge #262

Robbie Hatley's Solutions To The Weekly Challenge #239

Robbie Hatley's Solutions To The Weekly Challenge #266