-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday18.py
44 lines (37 loc) · 1.18 KB
/
day18.py
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
def is_safe(row, pos):
section = row[pos - 1 : pos + 2]
if section == "^^." or section == ".^^" or section == "^.." or section == "..^":
return False
return True
def generate_board(first_row: str, rows: int) -> list:
board = [first_row]
row_len = len(first_row)
current_row = "." + first_row + "."
for i in range(rows - 1):
next_row = ""
for j in range(1, row_len + 1):
next_row += "." if is_safe(current_row, j) else "^"
board.append(next_row)
current_row = "." + next_row + "."
return board
def puzzles():
board = generate_board(
".^^^.^.^^^^^..^^^..^..^..^^..^.^.^.^^.^^....^.^...^.^^.^^.^^..^^..^.^..^^^.^^...^...^^....^^.^^^^^^^",
40,
)
num_safe = 0
for row in board:
num_safe += row.count(".")
# print(row)
print("safe:", num_safe)
board = generate_board(
".^^^.^.^^^^^..^^^..^..^..^^..^.^.^.^^.^^....^.^...^.^^.^^.^^..^^..^.^..^^^.^^...^...^^....^^.^^^^^^^",
400000,
)
num_safe = 0
for row in board:
num_safe += row.count(".")
# print(row)
print("safe:", num_safe)
if __name__ == "__main__":
puzzles()