summaryrefslogtreecommitdiff
path: root/day11/day11.py
diff options
context:
space:
mode:
Diffstat (limited to 'day11/day11.py')
-rw-r--r--day11/day11.py116
1 files changed, 116 insertions, 0 deletions
diff --git a/day11/day11.py b/day11/day11.py
new file mode 100644
index 0000000..6c3b711
--- /dev/null
+++ b/day11/day11.py
@@ -0,0 +1,116 @@
+"""Day 11."""
+with open("input") as f:
+ CHART = tuple(tuple(x) for x in f.read().rstrip().split("\n"))
+
+EMPTY = 'L'
+OCCUPIED = '#'
+FLOOR = '.'
+
+MIN = 0
+MAX = len(CHART[0]) - 1
+
+def clamp(num, minimium, maximium):
+ """Clamp a number in between a range."""
+ if num <= minimium:
+ return minimium
+ if num >= maximium:
+ return maximium
+ return num
+
+
+def get_neighbours(x, y):
+ """Get neighbour cords for a point."""
+ pairs = set()
+ for i in range(x - 1, x + 2):
+ for j in range(y - 1, y + 2):
+ pairs.add(
+ (
+ clamp(i, MIN, MAX),
+ clamp(j, MIN, MAX)
+ )
+ )
+ # Remove self
+ pairs.remove((x, y))
+ return pairs
+
+
+def get_seat(x, y, chart):
+ """so we can call x, then y."""
+ return CHART[y][x]
+
+
+def iterate_over_chart(chart, p=False):
+ new_map = []
+ for y, row in enumerate(chart):
+ new_map.append([])
+ for x, col in enumerate(row):
+ seat = chart[y][x]
+ neighbours = [chart[pair[1]][pair[0]] for pair in get_neighbours(x, y)]
+ occupied_sum = sum(1 if x == OCCUPIED else 0 for x in neighbours)
+ if seat == EMPTY and not any(x == OCCUPIED for x in neighbours):
+ new_map[y].append(OCCUPIED)
+ continue
+ elif seat == OCCUPIED and occupied_sum >= 4:
+ new_map[y].append(EMPTY)
+ continue
+ else:
+ new_map[y].append(seat)
+ if p:
+ print(y, x, seat)
+ print([(pair[1], pair[0]) for pair in get_neighbours(x, y)])
+ print(neighbours)
+ print(occupied_sum)
+ print()
+ return new_map
+
+def print_chart(chart):
+ for y in chart:
+ line = ""
+ for x in y:
+ line += x
+ print(line)
+
+
+def are_equal(chart1, chart2):
+ for idx, _ in enumerate(chart1):
+ if chart1[idx] != chart2[idx]:
+ return False
+ return True
+
+def count_occupied(chart):
+ count = 0
+ for row in chart:
+ for x in row:
+ if x == OCCUPIED:
+ count += 1
+ return count
+
+
+
+
+initial = CHART
+count = 0
+while True:
+ new_chart = iterate_over_chart(initial)
+ if are_equal(new_chart, initial):
+ print(count_occupied(new_chart))
+ print("done")
+ break
+ else:
+ # print_chart(new_chart)
+ print(count)
+ count += 1
+ initial = new_chart
+
+
+# while True:
+# print_chart(CHART)
+# print()
+new_chart = iterate_over_chart(CHART)
+print(are_equal(initial, new_chart))
+# print()
+
+# # new_chart = print_iterate_over_chart(new_chart)
+# new_chart = iterate_over_chart(new_chart)
+# print_chart(new_chart)
+# print()