# Egg-Balancing Puzzle

### Background

I came up with the puzzle a month or two ago while making French toast. I was removing eggs from a full egg carton, and I thought to myself "Is it possible to remove 3 eggs from the carton and have it remain balanced?" That's an interesting question, and this puzzle is an evolution of that question. Everyone should be able to solve this puzzle, so I have tried to word the instructions in a general way. There are additional instructions for programmers (as this is a good programming exercise that shouldn't take very long), but in either case it's the same puzzle. It's only a matter of whether you solve each problem by looking at it or by writing an algorithm to do it for you.

### Explanation

Given an egg carton with some eggs in it, decide whether the carton could be balanced at its center. In other words, decide if the center of mass of the eggs is at the center point of the carton. If so, simply find the position of the egg carton's center of gravity. If not, find the position where one new egg could be placed in the carton to make it balanced, or decide that it could not possibly be balanced by adding one egg.

Notes: An egg can only be added to the empty spaces in the carton. Each egg has the same weight as all others. The weight of the carton is negligable.

There are 6 problems below to start, but theoretically there are thousands of possible problems. You might be surprised by a couple of the answers. If so, I have succeeded in creating this puzzle. Enjoy! ~Alan

### Programming Instructions

If you don't want to program a solution to this puzzle, you can skip straight to the problems. You can ignore the "Input" section, and when you think you have solved it, highlight the "Output" section for the answer. If you do want to program a solution, this is where I describe the input and output formatting.

#### Input

Since this puzzle can be for any size egg carton, first you are given the width and height of the carton. These are both integers between 1 and 99. Next, you are given a list with the ($x, y$) coordinates of each egg in the carton. It is given in JSON format so that the input is programming-language-independent. Here is an example of an egg list:
```[[1,1], [3,3], [9,3], [11,1]]
```

Note that the lower-left corner is the origin (0, 0) and the upper-right corner is at (2·$width$, 2·$height$). This is done so that the center of the egg carton has integer coordinates and is located at the point ($width, height$), and the coordinates ($x, y$) of each egg are always odd integers within these ranges:

• 1 ≤ $x$ < 2·$width$
• 1 ≤ $y$ < 2·$height$
For those of you who understand pictures more easily than words (like me), these are the coordinates of each position in a 6×2 egg carton:

 (1, 3) (3, 3) (5, 3) (7, 3) (9, 3) (11, 3) (1, 1) (3, 1) (5, 1) (7, 1) (9, 1) (11, 1)

By the way, you can also get all the input as a single text file or JSON. Just right-click on a link and select "Save Link As...".

#### Output

The only output should be one of the following sentences:

• `Egg carton is already balanced at (x, y).`
where $x$ and $y$ are the center of the egg carton
• `Need to add an egg at (x, y) to balance carton.`
where $x$ and $y$ are the coordinates of the new egg
• `Egg carton cannot be balanced by adding one egg.`

Okay, now get to programming already! And don't look at the output until you've tested your program. ;-)

## Problem 1

#### Input

```{
"width": 6
"height": 2
"eggs": [[1,1], [3,3], [9,3], [11,1]]
}```

#### Output (select to view)

```Egg carton is already balanced at (6, 2).
```

## Problem 2

#### Input

```{
"width": 6
"height": 2
"eggs": [[3,1], [9,3], [11,1]]
}```

#### Output (select to view)

```Need to add an egg at (1, 3) to balance carton.
```

## Problem 3

#### Input

```{
"width": 6
"height": 2
"eggs": [[1,3], [3,1], [7,3]]
}```

#### Output (select to view)

```Egg carton cannot be balanced by adding one egg.
```

## Problem 4

#### Input

```{
"width": 6
"height": 2
"eggs": [[7,1], [7,3], [9,3]]
}```

#### Output (select to view)

```Need to add an egg at (1, 1) to balance carton.
```

## Problem 5

#### Input

```{
"width": 4
"height": 3
"eggs": [[1,3], [3,5], [5,1], [5,3], [5,5]]
}```

#### Output (select to view)

```Egg carton cannot be balanced by adding one egg.
```

## Problem 6

#### Input

```{
"width": 6
"height": 3
"eggs": [[1,3], [3,1], [3,5], [7,5], [11,3]]
}```

#### Output (select to view)

```Need to add an egg at (11, 1) to balance carton.
```