summaryrefslogtreecommitdiff
path: root/2020/aoc2020-d17.py
blob: d26439dde4d3f9e7a61d35414e98cbc6fbb8a3fa (plain)
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
#advent of code 2020
#day 17
#bruteforce bros win again
#im not sure how to calculate both parts at the same time
#since the bounds will definitely be different between 3D and 4D 

import itertools

PocketDimension1 = set();
PocketDimension2 = set();
limit = 6;
PuzzleInput = open("17.in","r");
for y,line in enumerate(PuzzleInput):
	for x,c in enumerate(line[:-1]):
		if c == "#": 
			PocketDimension1.add((x,y,0)); 
			PocketDimension2.add((x,y,0,0)); 
PuzzleInput.close();

print("3D");
for turn in range(limit):
	CopiedDimension1 = set();
	bounds = [];
	for i in range(3):
		lo = min([p[i] for p in PocketDimension1]);
		hi = max([p[i] for p in PocketDimension1]);
		bounds.append([lo,hi]);
	
	for x in range(bounds[0][0]-1, bounds[0][1]+2):
		for y in range(bounds[1][0] -1, bounds[1][1] +2):
			for z in range(bounds[2][0] -1, bounds[2][1] +2):
				neighbors = 0;
				for dx,dy,dz in itertools.product([-1,0,1],repeat=3):
					if dx==dy==dz==0 : continue;
					if (x+dx,y+dy,z+dz) in PocketDimension1: neighbors+=1;
				if (x,y,z) in PocketDimension1:
					if 2 <= neighbors <= 3: CopiedDimension1.add((x,y,z));
				else:
					if neighbors == 3: CopiedDimension1.add((x,y,z));
	
	PocketDimension1 = CopiedDimension1.copy();
	print(f'turn {turn+1}/{limit}'); 

print("4D");
for turn in range(limit):
	CopiedDimension2 = set();
	bounds = [];
	for i in range(4):
		lo = min([p[i] for p in PocketDimension2]);
		hi = max([p[i] for p in PocketDimension2]);
		bounds.append([lo,hi]);
	
	for x in range(bounds[0][0]-1, bounds[0][1]+2):
		for y in range(bounds[1][0] -1, bounds[1][1] +2):
			for z in range(bounds[2][0] -1, bounds[2][1] +2):
				for w in range(bounds[3][0] -1, bounds[3][1] +2):
					neighbors = 0;
					for dx,dy,dz,dw in itertools.product([-1,0,1],repeat=4):
						if dx==dy==dz==dw==0 : continue;
						if (x+dx,y+dy,z+dz,w+dw) in PocketDimension2: neighbors+=1;
					if (x,y,z,w) in PocketDimension2:
						if 2 <= neighbors <= 3: CopiedDimension2.add(( x,y,z,w));
					else:
						if neighbors == 3: CopiedDimension2.add((x,y,z,w));
	
	PocketDimension2 = CopiedDimension2.copy();
	print(f'turn {turn+1}/{limit}'); #it takes a while so im leaving this as a countdown

p1 = len(PocketDimension1);
p2 = len(PocketDimension2);
print("part 1 =",p1);
print("part 2 =",p2);