*This kata is based on [Project Euler Problem #349](https://projecteuler.net/problem=349). You may want to start with solving [this kata](https://www.codewars.com/kata/langtons-ant) first.*
---
[Langton's ant](https://en.wikipedia.org/wiki/Langton%27s_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  according to the following rules:
- if it is on a black square, it flips the colour of the square to white, rotates 90 degrees counterclockwise and moves forward one square.
- if it is on a white square, it flips the colour 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 `n` moves of the ant?
**Note:** `n` will go as high as 10^(20)
---
## My other katas
If you enjoyed this kata then please try [my other katas](https://www.codewars.com/collections/katas-created-by-anter69)! :-)
#### *Translations are welcome!*
def langtons_ant(n):
    grid = [[0]]
    column = 0
    row = 0
    direction = 0
    move = {
        0: (-1, 0),
        1: ( 0, 1),
        2: ( 1, 0),
        3: ( 0,-1),
        }
    rows = len(grid)
    cols = len(grid[0])
    ap = (row, column)
    dir = direction
    n_black = 0
    
    max_simple_range = 11000
    n_black_highway_ref = []
    ref = 0
    
    for i in range(1, n + 1):        
        if (grid[ap[0]][ap[1]]) == 1:
            grid[ap[0]][ap[1]] = 0
            dir = (dir + 1) % 4
            n_black -= 1
        else:
            grid[ap[0]][ap[1]] = 1
            dir = (dir - 1) % 4
            n_black += 1
        ap = (ap[0] + move[dir][0], ap[1] + move[dir][1])
        
        if ap[0] >= rows:
            grid = grid + ([[0]*cols])
            rows += 1
        elif ap[0] < 0:
            grid = ([[0]*cols]) + grid
            rows += 1
            ap = (0, ap[1])
            
        if ap[1] >= cols:
            for j, r in enumerate(grid):
                grid[j] = r + [0]
            cols += 1
        elif ap[1] < 0:
            for j, r in enumerate(grid):
                grid[j] = [0] + r
            cols += 1
            
            ap = (ap[0], 0)
            
        if i >= max_simple_range:
            if i == max_simple_range:
                ref = n_black
            n_black_highway_ref.append(n_black - ref)
            
            if i == (max_simple_range + 104):
                break
    
    if n > (max_simple_range + 104):
        n_black += (n_black_highway_ref[-1] * (((n - max_simple_range) // 104) - 1))        
        n_black += (n_black_highway_ref[((n - max_simple_range) % 104)]) 
    return n_black
def langtons_ant(n):
    grid = [[0]]
    column = 0
    row = 0
    direction = 0
    move = {
        0: (-1, 0),
        1: ( 0, 1),
        2: ( 1, 0),
        3: ( 0,-1),
        }
    rows = len(grid)
    cols = len(grid[0])
    ap = (row, column)
    dir = direction
    n_black = 0
    
    max_simple_range = 11000
    n_black_highway_ref = []
    ref = 0
    
    for i in range(1, n + 1):        
        if (grid[ap[0]][ap[1]]) == 1:
            grid[ap[0]][ap[1]] = 0
            dir = (dir + 1) % 4
            n_black -= 1
        else:
            grid[ap[0]][ap[1]] = 1
            dir = (dir - 1) % 4
            n_black += 1
        ap = (ap[0] + move[dir][0], ap[1] + move[dir][1])
        
        if ap[0] >= rows:
            grid = grid + ([[0]*cols])
            rows += 1
        elif ap[0] < 0:
            grid = ([[0]*cols]) + grid
            rows += 1
            ap = (0, ap[1])
            
        if ap[1] >= cols:
            for j, r in enumerate(grid):
                grid[j] = r + [0]
            cols += 1
        elif ap[1] < 0:
            for j, r in enumerate(grid):
                grid[j] = [0] + r
            cols += 1
            
            ap = (ap[0], 0)
            
        if i >= max_simple_range:
            if i == max_simple_range:
                ref = n_black
            n_black_highway_ref.append(n_black - ref)
            
            if i == (max_simple_range + 104):
                break
    
    if n > (max_simple_range + 104):
        n_black += (n_black_highway_ref[-1] * (((n - max_simple_range) // 104) - 1))        
        n_black += (n_black_highway_ref[((n - max_simple_range) % 104)]) 
    return n_black