19
1
Researchers recently discovered an interesting bee colony that lives in an infinite field of honeycomb:
Each cell can house a bee or not. In fact, the lives of those creatures appear to be a bit ... chaotic. It could be calculated that a colony always starts with the following pattern:
(Bee drawn by Emmanuel Boutet on Wikimedia Commons. This honeycomb-and-bees image thus is released under CC-By-SA. grumbles)
After that the bee's life cycles are divided into so-called generations. Each generation old bees die and new ones hatch and it primarily depends on their cell's neighbors:
- If a bee has less than two neighbors it dies due to loneliness.
- If a bee has more than three neighbors it dies due to overcrowding.
- If a cell has two, three or four living bees in neighboring cells a new bee hatches there in the next generation.
Dying bees don't die until the end of a generation so they still affect surrounding cells that might hatch bees in the next generation.
Now that we know how such a colony works, we can simulate it through any number of generations.
Input
Input is a single number N, given on standard input, terminated by a line break. 0 ≤ N ≤ 150. This is the number of generations to simulate.
Output
Output is a single number, on standard output and optionally followed by a single line break, which represents the number of living bees after N generations.
Additional output on standard error is ignored.
Sample inputs
0
5
42
100
Sample outputs
6
44
1029
5296
Winning condition
Shortest code wins, as is customary in golf. In case of a tie, the earlier solution wins.
Test cases
There are two tests scripts, containing identical test cases:
Invocation is in both cases: <test script> <my program> [arguments]
,
e.g. ./test ruby beehive.rb
or ./test.ps1 ./beehive.exe
.
I know there are only 22 tests instead of 151 (mainly because solutions are often quite slow). Please refrain from embedding the exact test cases instead of solving the task. These scripts are a convenience for you to test whether a change still causes the program to behave correctly; not that you can adapt your code to the specific test cases.
Another note
This task was part of a golf contest held at my university during 2011-W24. The scores and languages of our contestants were as follows:
- 336 – C
- 363 – C
- 387 – C
- 389 – Haskell
- 455 – C
Our own solution was
- 230 – Ruby
This sounds a bit like Conway's game of life. – Peter Olson – 2011-07-04T00:28:35.663
Of course; that's why it's tagged that way, too. It's very thinly veiled, indeed. – Joey – 2011-07-04T00:47:08.860