Last active
March 3, 2017 05:57
-
-
Save intrd/ffcd6d88356caec02eb862800de4c484 to your computer and use it in GitHub Desktop.
Solution to crypto50-give_life_to_the_letters @ Shellter Labs - Carnaval Hacking 2017
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
#!/usr/bin/python3 | |
## Solution to crypto50-give_life_to_the_letters @ Shellter Labs - Carnaval Hacking 2017 | |
## Conway's Game of Life w/ a 5th custom rule | |
# @author intrd - http://dann.com.br/ (original GoL implementation by https://github.com/JulianLaval/game-of-life-python) | |
# @license Creative Commons Attribution-ShareAlike 4.0 International License - http://creativecommons.org/licenses/by-sa/4.0/ | |
from os import system, name | |
from sys import stdin | |
from copy import deepcopy | |
from html import parser | |
import time | |
f = open("input_formated.txt") | |
input = f.read().splitlines() | |
global inputArray | |
inputArray = [] | |
for line in input: | |
inputArray.append(list(line)) | |
def get_highest_value_from_array(arrayy): | |
return(max(arrayy)) | |
def nextGen(inputArray): | |
newArray = deepcopy(inputArray) | |
for lineKey, line in enumerate(inputArray): | |
for charKey, char in enumerate(line): | |
if inputArray[lineKey][charKey] != " " and neighbours(lineKey, charKey) < 2: | |
newArray[lineKey][charKey] = " " | |
elif inputArray[lineKey][charKey] != " " and neighbours(lineKey, charKey) > 3: | |
newArray[lineKey][charKey] = " " | |
elif inputArray[lineKey][charKey] == " " and neighbours(lineKey, charKey) == 3: | |
#newArray[lineKey][charKey] = "x" | |
neis = get_neighbours(lineKey, charKey) | |
maxval=get_highest_value_from_array(neis) | |
newArray[lineKey][charKey] = maxval | |
#print(maxval) | |
return newArray | |
def neighbours(y, x): | |
counter = 0 | |
try: | |
if x-1 >= 0 and y-1 >= 0 and inputArray[y-1][x-1] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x >= 0 and y-1 >= 0 and inputArray[y-1][x] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x+1 >= 0 and y-1 >= 0 and inputArray[y-1][x+1] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x-1 >= 0 and y >= 0 and inputArray[y][x-1] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x+1 >= 0 and y >= 0 and inputArray[y][x+1] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x-1 >= 0 and y+1 >= 0 and inputArray[y+1][x-1] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x >= 0 and y+1 >= 0 and inputArray[y+1][x] != " ": | |
counter += 1 | |
except: | |
pass | |
try: | |
if x+1 >= 0 and y+1 >= 0 and inputArray[y+1][x+1] != " ": | |
counter += 1 | |
except: | |
pass | |
return counter | |
def get_neighbours(y, x): #fast pog :) | |
counter = 0 | |
neigs = [] | |
try: | |
if x-1 >= 0 and y-1 >= 0 and inputArray[y-1][x-1] != " ": | |
neigs.append(inputArray[y-1][x-1]) | |
except: | |
pass | |
try: | |
if x >= 0 and y-1 >= 0 and inputArray[y-1][x] != " ": | |
neigs.append(inputArray[y-1][x]) | |
except: | |
pass | |
try: | |
if x+1 >= 0 and y-1 >= 0 and inputArray[y-1][x+1] != " ": | |
neigs.append(inputArray[y-1][x+1]) | |
except: | |
pass | |
try: | |
if x-1 >= 0 and y >= 0 and inputArray[y][x-1] != " ": | |
neigs.append(inputArray[y][x-1]) | |
except: | |
pass | |
try: | |
if x+1 >= 0 and y >= 0 and inputArray[y][x+1] != " ": | |
neigs.append(inputArray[y][x+1]) | |
except: | |
pass | |
try: | |
if x-1 >= 0 and y+1 >= 0 and inputArray[y+1][x-1] != " ": | |
neigs.append(inputArray[y+1][x-1]) | |
except: | |
pass | |
try: | |
if x >= 0 and y+1 >= 0 and inputArray[y+1][x] != " ": | |
neigs.append(inputArray[y+1][x]) | |
except: | |
pass | |
try: | |
if x+1 >= 0 and y+1 >= 0 and inputArray[y+1][x+1] != " ": | |
neigs.append(inputArray[y+1][x+1]) | |
except: | |
pass | |
return neigs | |
gen = 0 | |
while True: | |
newPrint = deepcopy(inputArray) | |
print("Generation: ",str(gen),"\n\n") | |
print( '\n'.join([''.join(line) for line in newPrint])) | |
gen+=1 | |
inputArray = nextGen(inputArray) | |
if gen == 785: | |
time.sleep(9999) | |
exit() | |
time.sleep(0.05) | |
system('cls' if name=='nt' else 'clear') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment