summaryrefslogtreecommitdiff
path: root/2020/aoc2020-d17.py
diff options
context:
space:
mode:
authorblenovo <bk@gmail.com>2025-08-02 03:13:50 +0200
committerblenovo <bk@gmail.com>2025-08-02 03:13:50 +0200
commit12f551dbfef138880b29ba6abe4576714ae942cb (patch)
treeb7d3b20f01df625b8f2be552314133ebe6c0d1d1 /2020/aoc2020-d17.py
parent99a7d62c30069a5ffe2210a72c7cf81e76a1f241 (diff)
summer warmup sesh part 2
Diffstat (limited to '2020/aoc2020-d17.py')
-rw-r--r--2020/aoc2020-d17.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/2020/aoc2020-d17.py b/2020/aoc2020-d17.py
new file mode 100644
index 0000000..d26439d
--- /dev/null
+++ b/2020/aoc2020-d17.py
@@ -0,0 +1,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);