-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-2.py
139 lines (112 loc) · 3.12 KB
/
14-2.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
DIRS = [-1, 0]
TRANS = {"#": 1, ".": 0, "O": 2}
R_TRANS = {1: "#", 0: ".", 2: "O"}
def get_input():
with open("14.txt", "r") as f:
lines = f.readlines()
return [list(l.strip()) for l in lines if l != ""]
def move_boulder(rocks, coords, i):
rocks = np.array(rocks)
di = i % 4
rocks = np.rot90(rocks, -1 * di)
c = coords
r = rocks
while True:
if r[c[0]][c[1]] != "O":
r = np.rot90(r, di)
return r
next_ = [c[0] + DIRS[0], c[1] + DIRS[1]]
if (
next_[0] < 0
or next_[0] > len(r) - 1
or next_[1] < 0
or next_[1] > len(rocks) - 1
):
r = np.rot90(r, di)
return r
if r[next_[0]][next_[1]] == "#":
r = np.rot90(r, di)
return r
elif r[next_[0]][next_[1]] == "O":
r = np.rot90(r, di)
return r
elif r[next_[0]][next_[1]] == ".":
r[c[0]][c[1]] = "."
r[next_[0]][next_[1]] = "O"
c = next_
def count_rows(rocks):
count = 0
for i, l in enumerate(rocks):
for c in l:
if c == "O":
count += len(rocks) - i
return count
def rotate(rocks, times):
r = np.array(rocks)
r = np.rot90(r, times)
return r
def get_str_rep(rocks):
stri = ""
i = 0
for l in rocks:
for r in l:
stri += str(TRANS[r])
i += 1
return stri
def create_hash(rocks):
hash = []
for ind in range(2000):
for i, l in enumerate(rocks):
for j, r in enumerate(l):
rocks = move_boulder(rocks, [i, j], ind)
str_rep = get_str_rep(rocks)
if ind % 4 == 3 and ind != 0:
hash.append(str_rep)
with open("hash.txt", "w") as f:
for h in hash:
f.write(h + "\n")
return hash
def translate_hash(hash, line_len):
rocks = []
row = []
hash = [int(i) for i in str(hash)]
j = 0
while j < len(hash):
for i in range(line_len):
int_rep = hash[j]
row.append(R_TRANS[int_rep])
j += 1
rocks.append(row)
row = []
return rocks
def find_repeating(hash):
max_ = int(len(hash)) - 1
min_ = 30
poss = []
for i in range(min_, max_):
j = 0
while j < int(len(hash)) - (min_ * 2):
comp_1 = hash[j : j + i]
comp_2 = hash[j + i : j + (i * 2)]
if comp_1 == comp_2:
poss.append([j, i])
j += i
else:
j += 1
return poss[0]
def get_hash_from_file():
with open("hash.txt", "r") as f:
return [l.replace("\n", "") for l in f.readlines()]
def main():
rocks = get_input()
l = len(rocks[0])
hash = create_hash(rocks)
# uncomment below after first run to avoid having to recreate hash each time
# HASH = get_hash_from_file()
start, length = find_repeating(hash)
pattern = hash[start : start + length]
i = (1000000000 - (start + 1)) % (length)
print(count_rows(translate_hash(pattern[i], l)))
main()
# 103445