summaryrefslogtreecommitdiff
path: root/day11/day11.py
blob: ddc88a86e980870f9fd9b9471e96c8b4827a7958 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""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_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_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 = 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:
            line += x
        print(line)


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:
            if x == OCCUPIED:
                count += 1
    return count

# Part 1
initial = CHART
count = 0
while True:
    new_chart = iterate_over_chart_part1(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


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