summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--day11/day11.py150
1 files changed, 114 insertions, 36 deletions
diff --git a/day11/day11.py b/day11/day11.py
index 6c3b711..ddc88a8 100644
--- a/day11/day11.py
+++ b/day11/day11.py
@@ -5,10 +5,10 @@ with open("input") as f:
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:
@@ -34,36 +34,43 @@ def get_neighbours(x, y):
return pairs
-def get_seat(x, y, chart):
- """so we can call x, then y."""
- return CHART[y][x]
+def get_new_seat(seat, neighbours, occupied_limit):
+ """Get the new seat."""
+ 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):
+ return OCCUPIED
+ elif seat == OCCUPIED and occupied_sum >= occupied_limit:
+ return EMPTY
+ else:
+ return seat
+
+
+def iterate_over_chart_part1(chart, p=False):
+ """Iterate over a chart for part 1."""
+ new_map = []
+ for y, row in enumerate(chart):
+ new_map.append([])
+ for x, col in enumerate(row):
+ seat = chart[y][x]
+ neighbours = tuple(chart[pair[1]][pair[0]] for pair in get_neighbours(x, y))
+ new_map[y].append(get_new_seat(seat, neighbours, 4))
+ return new_map
-def iterate_over_chart(chart, p=False):
+def iterate_over_chart_part2(chart, p=False):
+ """Iterate over a chart for part 2."""
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()
+ neighbours = find_first_seat(x, y, chart)
+ new_map[y].append(get_new_seat(seat, neighbours, 5))
return new_map
+
def print_chart(chart):
+ """Pretty print a chart."""
for y in chart:
line = ""
for x in y:
@@ -72,12 +79,14 @@ def print_chart(chart):
def are_equal(chart1, chart2):
+ """Check if two charts are equal."""
for idx, _ in enumerate(chart1):
if chart1[idx] != chart2[idx]:
return False
return True
def count_occupied(chart):
+ """Count occupied seats in a chart."""
count = 0
for row in chart:
for x in row:
@@ -85,13 +94,11 @@ def count_occupied(chart):
count += 1
return count
-
-
-
+# Part 1
initial = CHART
count = 0
while True:
- new_chart = iterate_over_chart(initial)
+ new_chart = iterate_over_chart_part1(initial)
if are_equal(new_chart, initial):
print(count_occupied(new_chart))
print("done")
@@ -103,14 +110,85 @@ while True:
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()
+def find_first_seat(x, y, chart):
+ """Find the first seat."""
+ seen_seats = []
+ # print("top")
+ for new_y in range(y - 1, MIN - 1, -1):
+ seat = chart[new_y][x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("top right")
+ for idx, new_y in enumerate(range(y - 1, MIN - 1, -1)):
+ new_x = x + idx + 1
+ if new_x > MAX:
+ break
+ seat = chart[new_y][new_x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("right")
+ for idx in range(len(chart[y][x:]) - 1):
+ new_x = x + idx + 1
+ seat = chart[y][new_x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("down right")
+ for idx, new_x in enumerate(range(x + 1, MAX + 1)):
+ new_y = y + idx + 1
+ if new_y > MAX:
+ break
+ seat = chart[new_y][new_x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("down")
+ for new_y in range(y + 1, MAX + 1):
+ seat = chart[new_y][x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("down left")
+ for idx, new_x in enumerate(range(x - 1, MIN - 1, -1)):
+ new_y = y + idx + 1
+ if new_y > MAX:
+ break
+ seat = chart[new_y][new_x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("left")
+ for idx in range(len(chart[y][:x + 1]) - 1):
+ new_x = x - idx - 1
+ seat = chart[y][new_x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ # print("top left")
+ for idx, new_x in enumerate(range(x - 1, MIN - 1, -1)):
+ new_y = y - idx - 1
+ if new_y < MIN:
+ break
+ seat = chart[new_y][new_x]
+ if seat in [OCCUPIED, EMPTY]:
+ seen_seats.append(seat)
+ break
+ return seen_seats
+
+
+# Part 2
+initial = CHART
+count = 0
+while True:
+ new_chart = iterate_over_chart_part2(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