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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay