-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12.py
More file actions
84 lines (72 loc) · 1.58 KB
/
Copy pathday12.py
File metadata and controls
84 lines (72 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import ctypes
import pdb
PAD = 2000
state = ctypes.create_string_buffer('.' * PAD + '#.#.#..##.#....#.#.##..##.##..#..#...##....###..#......###.#..#.....#.###.#...#####.####...#####.#.#' + '.' * PAD)
rules_str = """..#.. => .
#...# => .
.#... => #
#.##. => .
..#.# => #
#.#.# => .
###.. => #
###.# => #
..... => .
....# => .
.##.. => #
##### => .
####. => .
..##. => .
##.#. => #
.#..# => #
##..# => .
.##.# => .
.#### => #
..### => .
...## => #
#..## => #
#.... => .
##.## => .
#.#.. => .
##... => .
.#.## => #
.###. => #
...#. => .
#.### => .
#..#. => #
.#.#. => .
"""
def count_all(val):
count = 0
for x in xrange(len(val)):
if val[x] == '#':
count += (x-PAD)
return count
class Rule(object):
def __init__(self, matrix, state):
self.matrix = matrix
self.state = state
rules = []
for rule in rules_str.split('\n'):
matrix = rule[0:5]
mystate = rule[-1:]
rules.append(Rule(matrix,mystate))
def test_pot(array, array_new, idx, rules):
for rule in rules:
if len([i for i, j in zip(array[idx-2:idx+3], rule.matrix) if i == j]) == 5:
array_new[idx] = rule.state
debug = False
generations = 200
old_count = count_all(state.value)
if debug:
print "%d: %s" % (0, state.value)
new_state = state
for g in xrange(generations):
if debug:
print "%d: %s" % (g+1, state.value)
state = new_state
new_state = ctypes.create_string_buffer(len(state.value) * '.')
for x in xrange(len(state)):
test_pot(state, new_state, x, rules)
count = count_all(new_state.value)
print "gen = %s count = %s diff = %s" % (g+1, count, count-old_count)
old_count = count