Problem 349: Langton's ant

An ant moves on a regular grid of squares that are coloured either black or white.
The ant is always oriented in one of the cardinal directions (left, right, up or down) and moves from square to adjacent square according to the following rules:

if it is on a black square, it flips the color of the square to white, rotates 90 degrees counterclockwise and moves forward one square.

if it is on a white square, it flips the color of the square to black, rotates 90 degrees clockwise and moves forward one square.

Starting with a grid that is entirely white, how many squares are black after 10^18 moves of the ant?

My Algorithm

I had no idea how to solve this problem until I read the Wikipedia page about Langton's ant: en.wikipedia.org/wiki/Langton's_ant
One thing was especially interesting: the initial movement of the ant may be chaotic, but after a while a certain pattern develops which repeats after 104 steps.

My program simulates the movement of the ant on a 64x64 grid (starting in the middle).
It counts the number of black squares and every 104 steps (= 1 cycle) it computes the delta compared to the number of black squares 104 steps ago.
As soon as I observe the same difference over at least 10 cycles I can easily find out how many cycles are needed for 10^18 steps (minus the steps already taken).

To simplify my computation I don't check the difference at the start of a cycle but after the 40th steps of a cycle because 10^18 mod 104 = 40.

Note

The recurring pattern appears after about 10000 steps.

Interactive test

You can submit your own input to my program and it will be instantly processed at my server:

Input data (separated by spaces or newlines):Note: Enter the number of moves.

This is equivalent toecho 10 | ./349

Output:

(please click 'Go !')

Note: the original problem's input 1000000000000000000cannot be enteredbecause just copying results is a soft skill reserved for idiots.

(this interactive test is still under development, computations will be aborted after one second)

My code

… was written in C++11 and can be compiled with G++, Clang++, Visual C++. You can download it, too.

#include<iostream>

#include<vector>

intmain()

{

auto limit = 1000000000000000000LL;

std::cin>> limit;

// colors encoded with a single bit

constbool White = false;

constbool Black = true;

// 128x128 squares (nice power of two with sufficient squares for the first 10000 steps)

constunsignedint Size = 128;

// 2D grid stored as a 1D array: pos = y * Size + x;

std::vector<bool>grid(Size * Size, White);

// initial position of the ant

unsignedint x = Size / 2;

unsignedint y = Size / 2;

// delta movement when ant moves Up, Left, Down, Right

constshort DeltaX[] = { 0, +1, 0, -1 };

constshort DeltaY[] = { +1, 0, -1, 0 };

// direction 0..3 (see DeltaX and DeltaY)

short direction = 0; // any direction is fine

// a pattern with cycle length 104 emerges after a while (see https://en.wikipedia.org/wiki/Langton%27s_ant )

constauto Cycle = 104;

constauto Remainder = limit % Cycle; // = 40

// number of black squares

auto count = 0LL;

// and the value of "count" 104 steps ago

auto last = count;

// keep track of the deltas (which are "count-last")

std::vector<int> lastDeltas = { 0 };

// assume a "steady state" if the most recent 10 cycles have the same deltas

Benchmark

The correct solution to the original Project Euler problem was found in less than 0.01 seconds on an Intel® Core™ i7-2600K CPU @ 3.40GHz.
(compiled for x86_64 / Linux, GCC flags: -O3 -march=native -fno-exceptions -fno-rtti -std=gnu++11 -DORIGINAL)

Those links are just an unordered selection of source code I found with a semi-automatic search script on Google/Bing/GitHub/whatever.
You will probably stumble upon better solutions when searching on your own.
Maybe not all linked resources produce the correct result and/or exceed time/memory limits.

Heatmap

Please click on a problem's number to open my solution to that problem:

green

solutions solve the original Project Euler problem and have a perfect score of 100% at Hackerrank, too

yellow

solutions score less than 100% at Hackerrank (but still solve the original problem easily)

gray

problems are already solved but I haven't published my solution yet

blue

solutions are relevant for Project Euler only: there wasn't a Hackerrank version of it (at the time I solved it) or it differed too much

orange

problems are solved but exceed the time limit of one minute or the memory limit of 256 MByte

red

problems are not solved yet but I wrote a simulation to approximate the result or verified at least the given example - usually I sketched a few ideas, too

black

problems are solved but access to the solution is blocked for a few days until the next problem is published

[new]

the flashing problem is the one I solved most recently

I stopped working on Project Euler problems around the time they released 617.

The 310 solved problems (that's level 12) had an average difficulty of 32.6&percnt; at Project Euler and
I scored 13526 points (out of 15700 possible points, top rank was 17 out of &approx;60000 in August 2017)
at Hackerrank's Project Euler+.

My username at Project Euler is stephanbrumme while it's stbrumme at Hackerrank.

Copyright

I hope you enjoy my code and learn something - or give me feedback how I can improve my solutions.All of my solutions can be used for any purpose and I am in no way liable for any damages caused.You can even remove my name and claim it's yours. But then you shall burn in hell.

The problems and most of the problems' images were created by Project Euler.Thanks for all their endless effort !!!

more about me can be found on my homepage,
especially in my coding blog.
some names mentioned on this site may be trademarks of their respective owners.
thanks to the KaTeX team for their great typesetting library !