-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17.py
More file actions
109 lines (94 loc) · 3.01 KB
/
17.py
File metadata and controls
109 lines (94 loc) · 3.01 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import copy
import sys
debug = False
iterations = 6
fileName = ""
try:
fileName = sys.argv[1]
if len(fileName) < 1:
fileName = "17.txt"
except:
fileName = "17.txt"
if debug:
print(fileName)
cubes = []
def checkNeighbors(x, y, z):
count = 0
for cube in cubes:
if cube[0] >= (x - 1) and cube[0] <= (x + 1) and cube[1] >= (y - 1) and cube[1] <= (y + 1) and cube[2] >= (z - 1) and cube[2] <= (z + 1) and not (cube[0] == x and cube[1] == y and cube[2] == z):
count += 1
return count
def prettyPrint(minX, maxX, minY, maxY, minZ, maxZ):
for z in range(minZ,maxZ+1):
print("z=",z)
for y in range(minY,maxY+1):
for x in range(minX,maxX+1):
if [x,y,z] in cubes:
print("#",end="")
else:
print(".",end="")
print("")
print("")
with open(fileName) as file:
minX = 1000
maxX = 0
minY = 1000
maxY = 0
minZ = 0
maxZ = 0
# read in starting content
lines = file.readlines()
for y in range(len(lines)):
for x in range(len(lines[y])):
if lines[y][x] == "#":
coord = [x,y,0]
cubes.append(coord)
if x > maxX:
maxX = x
if x < minX:
minX = x
if y > maxY:
maxY = y
if y < minY:
minY = y
if debug:
print(0,"------")
prettyPrint(minX,maxX,minY,maxY,minZ,maxZ)
for i in range(iterations):
c = copy.deepcopy(cubes)
for cube in cubes:
#check if it becomes inactive
neighbors = checkNeighbors(cube[0],cube[1],cube[2])
if neighbors < 2 or neighbors > 3:
try:
c.remove(cube)
except:
pass
#print("removing",cube)
#check if any neighbors become active
for x in range(minX-1,maxX+2):
for y in range(minY-1,maxY+2):
for z in range(minZ-1,maxZ+2):
if [x,y,z] not in cubes and [x,y,z] not in c:
neighbors = checkNeighbors(x, y, z)
if neighbors == 3:
c.append([x,y,z])
if x > maxX:
maxX = x
if x < minX:
minX = x
if y > maxY:
maxY = y
if y < minY:
minY = y
if z > maxZ:
maxZ = z
if z < minZ:
minZ = z
#print("adding",[x,y,z])
cubes = c
if debug:
print(i,"-----")
prettyPrint(minX,maxX,minY,maxY,minZ,maxZ)
print(len(cubes)) #part1