summaryrefslogtreecommitdiff
path: root/day11/day11.py
blob: 6c3b711217060a2321709434d706dc0a5bec94eb (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
"""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()