-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0687143
commit 89f6fcc
Showing
6 changed files
with
405 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#! /usr/bin/python3.5 | ||
|
||
# -*- coding: utf-8 -*- | ||
|
||
import sys | ||
|
||
import numpy as np | ||
|
||
modulo = 2 | ||
|
||
def generate_field_position_crosses(n): | ||
positions = np.zeros((n+2, n+2), dtype=object) | ||
|
||
for y in range(1, n+1): | ||
for x in range(1, n+1): | ||
positions[y, x] = np.array([[y, x], [y, x+1], [y, x-1], [y+1, x], [y-1, x]], dtype=np.int8) | ||
|
||
positions = positions[1:n+1, 1:n+1]-1 | ||
|
||
# now calc -1 and eliminate everything which contains 0 or n as a value | ||
y_x_coords = \ | ||
[(y, 0) for y in range(0, n)] + \ | ||
[(y, n-1) for y in range(0, n)] + \ | ||
[(0, x) for x in range(1, n-1)] + \ | ||
[(n-1, x) for x in range(1, n-1)] | ||
|
||
for y, x in y_x_coords: | ||
coordinates = positions[y, x] | ||
remove_idx = np.where(np.logical_or.reduce((coordinates==-1) | (coordinates==n), axis=1))[0] | ||
positions[y, x] = np.delete(coordinates, remove_idx, axis=0) | ||
|
||
for y in range(0, n): | ||
for x in range(0, n): | ||
positions[y, x] = list(map(tuple, positions[y, x].T.tolist())) | ||
|
||
return positions | ||
|
||
def apply_on_field(field, positions, y_x_coords): | ||
for y, x in y_x_coords: | ||
coordinates = positions[y, x] | ||
field[coordinates] = (field[coordinates]+1) % modulo | ||
|
||
def apply_on_field_once(field, positions, y, x): | ||
coordinates = positions[y, x] | ||
field[coordinates] = (field[coordinates]+1) % modulo | ||
|
||
def mix_field(field, positions): | ||
n = field.shape[0] | ||
|
||
mix_field_arr = np.random.randint(0, 2, (n, n)) | ||
print("mix_field_arr:\n{}".format(mix_field_arr)) | ||
y_x_coords = np.array(np.where(mix_field_arr == 1)).T | ||
apply_on_field(field, positions, y_x_coords) | ||
|
||
def solve_field(field, positions): | ||
n = field.shape[0] | ||
field_orig = field.copy() | ||
|
||
# first do something on the first row | ||
# second solve from the frist until the n-1 row | ||
# third check if the last row n is solved, if now, repeat from fist step | ||
# until it is done | ||
|
||
tries = 1 | ||
is_not_solved = True | ||
while is_not_solved: | ||
# print("tries: {}".format(tries)) | ||
field = field_orig.copy() | ||
moves_done = np.zeros((n, n), dtype=np.int8) | ||
|
||
# print("before solving:") | ||
# print("field:\n{}".format(field)) | ||
|
||
random_moves = np.random.randint(0, 2, (n, )) # for the last row (n-th row) | ||
for i in np.where(random_moves == 1)[0]: | ||
apply_on_field_once(field, positions, n-1, i) | ||
|
||
moves_done[-1] = random_moves | ||
|
||
# print("after last row finish") | ||
# print("field:\n{}".format(field)) | ||
|
||
for y in range(n-2, -1, -1): | ||
for x in range(0, n): | ||
if field[y+1, x] == 1: | ||
apply_on_field_once(field, positions, y, x) | ||
moves_done[y, x] = 1 | ||
|
||
# print("after row #: {}".format(y)) | ||
# print("field:\n{}".format(field)) | ||
|
||
if np.sum(field[0]) == 0: | ||
is_not_solved = False | ||
|
||
# sys.exit(0) | ||
|
||
tries += 1 | ||
|
||
print("last try at tries: {}".format(tries)) | ||
|
||
# print("moves_done:\n{}".format(moves_done)) | ||
|
||
return moves_done | ||
|
||
if __name__ == "__main__": | ||
n = 5 | ||
|
||
field = np.zeros((n, n), dtype=np.uint8) | ||
|
||
positions = generate_field_position_crosses(n) | ||
|
||
print("\nbefore mixing:") | ||
print("field:\n{}".format(field)) | ||
|
||
mix_field(field, positions) | ||
|
||
print("\nafter mixing:") | ||
print("field:\n{}".format(field)) | ||
|
||
moves_done = solve_field(field, positions) | ||
print("\nmoves_done:\n{}".format(moves_done)) | ||
|
||
apply_on_field(field, positions, np.array(np.where(moves_done == 1)).T) | ||
|
||
print("\nafter moving:") | ||
print("field:\n{}".format(field)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#! /usr/bin/python3.5 | ||
|
||
import decimal | ||
import math | ||
|
||
import numpy as np | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
from decimal import Decimal as D | ||
from functools import reduce | ||
|
||
decimal.getcontext().prec = 2000 | ||
|
||
# TODO: maybe interesting for Numberphile?! | ||
|
||
def remove_leading_zeros(l): | ||
l = np.array(l).copy() | ||
i = 0 | ||
while l[i] == 0: | ||
i += 1 | ||
l = l[i:] | ||
return l | ||
|
||
def find_pattern(l): | ||
l = remove_leading_zeros(l) | ||
|
||
length = l.shape[0] | ||
|
||
max_idx = length//2 | ||
n = l[0] | ||
start_idx = 1 | ||
while n == l[start_idx]: | ||
start_idx += 1 | ||
if start_idx >= max_idx: | ||
return np.array([n]) | ||
|
||
for i in range(start_idx, max_idx): | ||
if np.sum(l[:i] != l[i:i*2]) == 0: | ||
return l[:i] | ||
|
||
return np.array([]) | ||
|
||
if __name__ == "__main__": | ||
# print("Hello World!") | ||
print("Calculating numbers") | ||
num = 9 | ||
p = 1 | ||
nums = [] | ||
max_power = 7000 | ||
for i in range(1000, max_power): | ||
p *= num | ||
nums.append(list(map(int, str(p)))) | ||
|
||
print("Getting them into the matrix") | ||
matrix = np.zeros((len(nums), len(nums[-1])), dtype=np.int) | ||
for i in range(0, len(nums)): | ||
l = nums[i] | ||
matrix[i, :len(l)] = l[::-1] | ||
|
||
print("Finding the pattern") | ||
patterns = [(lambda x: (i, len(x), x))(find_pattern(l)) for i, l in enumerate(matrix.T[:int(np.sqrt(max_power))])] |
Oops, something went wrong.