File size: 2,465 Bytes
0f06ae9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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



def main():
    import numpy as np
    from collections import defaultdict
    with open("/home/yassinealouini/Documents/code/advent_of_code/aoc/year_2021/data/4.txt") as f:
        tmp = f.read().rstrip().split("\n")

        draws = [int(e) for e in tmp[0].split(",")]

        # Make the bingo boards
        boards = defaultdict(lambda: np.zeros((5, 5)))
        board_id = 0
        data =  tmp[2:]
        for i, row in enumerate(data):
            if (i + 1) % 6 == 0:
                board_id += 1
                continue
            boards[board_id][i%6, :] = [int(e) for e in row.split(" ") if e != ""]
    
    board_won = {k: 0 for k in boards.keys()}
    for draw in draws:
        for board_id, board in boards.items():  
            board = np.where(board == draw, -1, board)
            boards[board_id] = board
            if (board.sum(axis=1) == -5).any() or (board.sum(axis=0) == -5).any():
                if sum(board_won.values())  == 0:
                    print("Solution for part I: ", board[(board > -1)].sum() * draw)
                board_won[board_id] = 1

            if sum(board_won.values()) == len(boards):
                print("Solution for part II: ", board[(board > -1)].sum() * draw)
                return

def streamlit_4(data_input):
    import numpy as np
    from collections import defaultdict
    import streamlit as st
    tmp = data_input.rstrip().split("\n")

    draws = [int(e) for e in tmp[0].split(",")]

    # Make the bingo boards
    boards = defaultdict(lambda: np.zeros((5, 5)))
    board_id = 0
    data =  tmp[2:]
    for i, row in enumerate(data):
        if (i + 1) % 6 == 0:
            board_id += 1
            continue
        boards[board_id][i%6, :] = [int(e) for e in row.split(" ") if e != ""]
    
    board_won = {k: 0 for k in boards.keys()}
    for draw in draws:
        for board_id, board in boards.items():  
            board = np.where(board == draw, -1, board)
            boards[board_id] = board
            if (board.sum(axis=1) == -5).any() or (board.sum(axis=0) == -5).any():
                if sum(board_won.values())  == 0:
                    st.write("Solution for part I: ", board[(board > -1)].sum() * draw)
                board_won[board_id] = 1

            if sum(board_won.values()) == len(boards):
                st.write("Solution for part II: ", board[(board > -1)].sum() * draw)
                return                

if __name__ == "__main__":
    main()