DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 093

Challenge 093

TASK #1 › Max Points

Task

You are given set of co-ordinates @N.

Write a script to count maximum points on a straight line when given co-ordinates plotted on 2-d plane.

My solution

I broke this task into smaller parts.

  • Read the input separate into an array of numbers, and make sure there are an even number of values.
  • Plot the points on into @grid by taking 1 off each value. This makes the point (0,0) the bottom right.
  • Create a graph. Although not part of the task, it's only four lines of code :)
  • Like with the word search task, go through each point in four directions (up, right, diagonally up and right, diagonally down and right) and find the longest points from that starting point. There is no point going in the other four directions as they are just opposites of the above four.
  • Print the longest points to the screen.

Examples

» ./ch-1.pl "(1,1), (2,2), (3,3)"
|     x
|   x
| x
+ - - -

Output is: 3

» ./ch-1.pl "(1,1), (2,2), (3,1), (1,3), (5,3)"
|     x
|
| x
|   x
| x   x
+ - - -

Output is: 3
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Sum Path

Task

You are given binary tree containing numbers 0-9 only.

Write a script to sum all possible paths from root to leaf.

My solution

This task was a little more complex than I expected. It didn't help that I had a little bug in my code that took me a while to figure out.

Even though my solution is 64 lines long, it is pretty straight forward.

  • First I read the input from STDIN.
  • The core of the code is then recursively calling the _walk_path sub routine storing the current path in the @$this_path array. If we hit the end of a node (i.e. it has no children), then I add it to the @$paths array.
  • Finally I display the sum, and the paths we visited.

Examples

(using the examples from the website)

» ./ch-2.pl < example-1.txt 
Output is 13
Paths: (1, 2, 3), (1, 2, 4)

» ./ch-2.pl < example-2.txt 
Output is 26
Paths: (1, 2, 4), (1, 3, 5), (1, 3, 6)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)