-
Notifications
You must be signed in to change notification settings - Fork 4
/
a-improved.py
119 lines (87 loc) · 2.3 KB
/
a-improved.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
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
import sys; sys.dont_write_bytecode = True; from utils import *
"""
To do: ensure Code Runner works (in WSL), have preloaded the day and input in Chrome,
saved input into the folder, have utils on the side, collapse regions
Strings, lists, dicts:
lmap, ints, positive_ints, floats, positive_floats, words, keyvalues
Algorithms:
bisect, binary_search, hamming_distance, edit_distance
Data structures:
Linked, UnionFind
use deque for queue: q[0], q.append and q.popleft
List/Vector operations:
GRID_DELTA, OCT_DELTA
lget, lset, fst, snd
padd, pneg, psub, pmul, pdot, pdist1, pdist2sq, pdist2
Matrices:
matmat, matvec, matexp
Previous problems:
knot
Dict things:
dict.keys()
dict.values()
dict.items()
"""
def do_case(inp: str, sample=False):
# READ THE PROBLEM FROM TOP TO BOTTOM OK
def sprint(*a, **k): sample and print(*a, **k)
lines = inp.splitlines()
def to_hashable(grid):
return "".join("".join(line) for line in grid)
def generator():
grid = lmap(list, lines)
yield grid
n = len(grid)
while True:
new_grid = copy.deepcopy(grid)
for i in range(n):
for j in range(n):
neighbours = get_neighbours(grid, i, j, OCT_DELTA)
trees = neighbours.count("|")
yards = neighbours.count("#")
if grid[i][j] == ".":
if trees >= 3:
new_grid[i][j] = "|"
elif grid[i][j] == "|":
if yards >= 3:
new_grid[i][j] = "#"
else:
if not (yards >= 1 and trees >= 1):
new_grid[i][j] = "."
grid = new_grid
yield grid
seq = RepeatingSequence(generator(), to_hashable)
grid = flatten(seq[1000000000])
q = grid.count("|")
w = grid.count("#")
print(q,w,q*w)
return # RETURNED VALUE DOESN'T DO ANYTHING, PRINT THINGS INSTEAD
run_samples_and_actual([
# Part 1
r"""
.#.#...|#.
.....#|##|
.|..|...#.
..|#.....#
#.#|||#|#|
...#.||...
.|....|...
||...#|.#|
|.||||..|.
...#.|..|.
""",r"""
""",r"""
""",r"""
""",r"""
""",r"""
""",r"""
"""],[
# Part 2
r"""
""",r"""
""",r"""
""",r"""
""",r"""
""",r"""
""",r"""
"""], do_case)