C solution.

To compile and run:

gcc goal.c -o goal && ./goal