Last active
April 9, 2020 13:54
-
-
Save iliis/cebcb5ab82eefe989f8c2f2c0e88e3c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import copy | |
initial_board_raw = [ | |
list("AAAAAAAAAB"), | |
list("BCCDDDDEEE"), | |
list("EEEEEEEEEF"), | |
list("FGGGHHIIII"), | |
list("IIIIIJKLLL"), | |
list("LMMNNNNNNO"), | |
list("OOOOOOOPPQ"), | |
list("RRRRRRSSSS"), | |
list("TTTTTTUUUU"), | |
list("VVWWXYYZ**") | |
] | |
class Board: | |
def __init__(self, initial=initial_board_raw): | |
self.data = copy.deepcopy(initial) | |
def __repr__(self): | |
return "\n".join("".join(row) for row in self.data) | |
def shunt_row(self, row, shift): | |
r = self.data[row] | |
self.data[row] = r[-shift:] + r[:-shift] | |
def shunt_col(self, col, shift): | |
old_col = [row[col] for row in self.data] | |
for i in range(10): | |
self.data[i][col] = old_col[(i-shift)%10] | |
# expects a list of ten numbers from 0 to 9 | |
# shunts all columns then all rows by this amount | |
def vertical_shunting(self, seed): | |
for col, shift in enumerate(seed): | |
self.shunt_col(col, shift) | |
def horizontal_shunting(self, seed): | |
for row, shift in enumerate(seed): | |
self.shunt_row(row, shift) | |
# take every nth row and put it at the top | |
def shuffle_rows(self, nth): | |
self.data = list(reversed(self.data[1::nth])) \ | |
+ self.data[0::nth] | |
# take every nth column and put it to the left | |
def shuffle_cols(self, nth): | |
for r in range(10): | |
self.data[r] = list(reversed(self.data[r][1::nth])) + self.data[r][0::nth] | |
b = Board() | |
b.vertical_shunting([2, 4, 6, 0, 9, 3, 1, 4, 0, 5]) | |
b.horizontal_shunting([7, 4, 5, 1, 8, 1, 1, 2, 2, 8]) | |
# two shuffle | |
b.shuffle_rows(2) | |
b.shuffle_cols(2) | |
# Result: | |
# MLNOWGAU*R | |
# IJRGFSLCTO | |
# NKOWFBCNEE | |
# IEIRBUVGAZ | |
# TDRQSAEVME | |
# ITAYDARPOO | |
# HOAEDYUSTE | |
# A*ADITNILE | |
# PNRAIEEOHE | |
# LNTIIUOEXS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment