summaryrefslogtreecommitdiffstats
path: root/20/py/d11.py
blob: 048367c9c00393806b022eba964229fc6c2e6c68 (plain) (blame)
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
#!/usr/bin/env python3
import aoc20
import sys


def read(lines):
    board = {}
    h = len(lines)
    w = len(lines[0])
    for y in range(h):
        for x in range(w):
            if lines[y][x] == "L":
                board[(x, y)] = 0
            elif lines[y][x] == ".":
                board[(x, y)] = -1
    return board, w, h


def amount_neighbours_occ(p, board):
    amount = 0
    for dy in range(-1, 1+1):
        for dx in range(-1, 1+1):
            if dy == dx == 0:
                continue
            new_p = (p[0] + dx, p[1] + dy)
            if new_p in board and board[new_p] == 1:
                amount += 1
    return amount


def pt1(_in):
    board, w, h = read(_in)
    while True:
        prev_board = board.copy()
        for p, state in prev_board.items():
            if state == 0:
                # empty
                if amount_neighbours_occ(p, prev_board) == 0:
                    board[p] = 1
            if state == 1:
                # occ
                if amount_neighbours_occ(p, prev_board) >= 4:
                    board[p] = 0
        if prev_board == board:
            break
        prev_board = board
    res = 0
    for _, state in board.items():
        if state == 1:
            res += 1
    return res


def amount_in_sight_occ(p, board, w, h):
    amount = 0
    dirs = ((1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1))
    for dir in dirs:
        step = 0
        while True:
            step += 1
            new_p = p[0] + dir[0] * step, p[1] + dir[1] * step
            if new_p not in board:
                break
            if board[new_p] == -1:
                continue
            if board[new_p] == 1:
                amount += 1
            break
    return amount


def pt2(_in):
    board, w, h = read(_in)
    while True:
        prev_board = board.copy()
        for p, state in prev_board.items():
            if state == 0:
                # empty
                if amount_in_sight_occ(p, prev_board, w, h) == 0:
                    board[p] = 1
            if state == 1:
                # occ
                if amount_in_sight_occ(p, prev_board, w, h) >= 5:
                    board[p] = 0
        if prev_board == board:
            break
        prev_board = board
    res = 0
    for _, state in board.items():
        if state == 1:
            res += 1
    return res


if __name__ == "__main__":
    input = aoc20.read_input(sys.argv[1:], 11)
    print(pt1(input))
    print(pt2(input))