summaryrefslogtreecommitdiff
path: root/2020/aoc2020-d24.py
diff options
context:
space:
mode:
Diffstat (limited to '2020/aoc2020-d24.py')
-rw-r--r--2020/aoc2020-d24.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/2020/aoc2020-d24.py b/2020/aoc2020-d24.py
new file mode 100644
index 0000000..c886be1
--- /dev/null
+++ b/2020/aoc2020-d24.py
@@ -0,0 +1,85 @@
+#advent of code 2020
+#day 24
+# https://www.redblobgames.com/grids/hexagons/
+# nested ifs in CheckAdjacent function aren't really slower than
+# making it a function, so i'm leaving it bc its more readable that way
+
+def CheckAdjacent(point, blackset):
+ black = 0;
+ py,px,pz = point;
+ for dkey in directions:
+ dx,dy,dz = directions[dkey];
+ p = (py+dy,px+dx, pz+dz);
+ if p in blackset:
+ black += 1;
+
+ if point in blackset:
+ if black == 0 or black > 2:
+ return False;
+ else:
+ return True;
+ else:
+ if black == 2:
+ return True;
+ else:
+ return False;
+
+tilesteps = [];
+
+PuzzleInput = open("24.in","r")
+for line in PuzzleInput:
+ line = line.replace("\n","");
+ instructions = [];
+ i = 0;
+ while i < len(line):
+ if line[i] in ["n","s"]:
+ instructions.append(line[i:i+2]);
+ i += 2;
+ else:
+ instructions.append(line[i]);
+ i += 1;
+ tilesteps.append(instructions);
+PuzzleInput.close();
+
+directions = dict();
+directions["e"] = (1,0,-1);
+directions["w"] = (-1,0,1);
+directions["ne"] = (1,-1,0);
+directions["nw"] = (0,-1,1);
+directions["sw"] = (-1,1,0);
+directions["se"] = (0,1,-1);
+
+grid = dict();
+# 0 - white
+# 1 - black
+
+for tilestep in tilesteps:
+ p = (0,0,0);
+ for step in tilestep:
+ py,px,pz = p;
+ dy,dx,dz = directions[step];
+ p = (py+dy,px+dx, pz+dz);
+ grid[p] = ( grid.get(p,0) + 1)%2;
+
+blacks = set();
+part1 = 0;
+for g in grid:
+ if grid[g] == 1:
+ part1 += 1;
+ blacks.add(g);
+
+print("part 1 =", part1);
+
+days = 100;
+for day in range(days):
+ NewBlacks = set();
+ for b in blacks:
+ if CheckAdjacent(b,blacks): NewBlacks.add(b);
+ for coord in directions.values():
+ n = (b[0] + coord[0],b[1] + coord[1],b[2] + coord[2]);
+ if CheckAdjacent(n,blacks): NewBlacks.add(n);
+ blacks = set();
+ blacks.update(NewBlacks);
+
+part2 = len(blacks);
+print("part 2 =", part2);