AIOC Banner

Problem: Chimpanzee

Want to try solving this problem? You can submit your code online if you log in or register.


Input File: chimpin.txt
Output File: chimpout.txt
Time Limit: 1 second

It has been a long hard day trekking through the jungle, and it is almost dark. Exhausted, you sit down to rest and pull a book from your backpack for some light reading. Suddenly, with an excited cry, a chimpanzee leaps from a nearby tree and snatches the book from your hands.

In horror you watch as the chimpanzee runs through the jungle in a spiral pattern, ripping pages from your book as it goes. Soon your horror gives way to curiosity as you contemplate the very precise spiral pattern that the chimpanzee is following.

Consider the jungle to be a grid of squares in a coordinate system, where you are sitting at square (0,0). The chimpanzee follows a tight spiral out from (0,0) as illustrated below.

The pages of your book are numbered from 0 upwards. At each square on the spiral, the chimpanzee tears a new page from your book. Thus page 0 is left at square (0,0), page 1 is at square (1,0), page 2 is at square (1,1), page 3 is at square (0,1), page 4 is at square (-1,1) and so on.

Fortunately you have also brought your laptop on your jungle trek. Pulling it from your bag and plugging it into a nearby power point, you sit down to write a program that will help you put the pages together in the correct order.

Your specific task is to write a program that reads in a pair of (x,y) coordinates and outputs which page the chimpanzee has left in that square.


The input will consist of a single line containing integers x and y separated by a single space (-1000 <= x,y <= 1000).


Your output must be a single line containing a single integer, which is the page number that the chimpanzee left in square (x,y). You may assume that your book is very thick and the chimpanzee does not run out of pages.

Sample Input 1

-1 1

Sample Output 1


Sample Input 2

2 1

Sample Output 2



The score for each input file will be 100% if the correct answer is written to the output file and 0% otherwise.


Privacy statement
© Australian Mathematics Trust 2001-2023

Page generated: 24 March 2023,  7:43pm AEDT