Skip to content

Commit 5a2a9fb

Browse files
committed
Horror, death, and misery.
1 parent c489933 commit 5a2a9fb

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

2023/day_12/Python/springs.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
1+
from tqdm import trange
12

3+
def load_data(file_path: str) -> list:
4+
with open(file_path, 'r') as file:
5+
data = file.readlines()
26

7+
springs = []
8+
instructions = []
9+
for dat in data:
10+
dat = dat.strip()
11+
spring, instr = dat.split(" ")
12+
springs.append(spring)
13+
instructions.append(instr)
314

4-
def calculate_arrangements(input_line: str):
5-
groups = input_line.split('.')
15+
return springs, instructions
16+
17+
def calculate_arrangements(string: str, arrangement_idx: int, N_quest: int) -> str:
18+
19+
arrangement_idx = bin(arrangement_idx)[2:].zfill(N_quest)
20+
qidx = 0
21+
result = ""
22+
for char in string:
23+
if char == "?":
24+
if arrangement_idx[qidx] == "1":
25+
result += "#"
26+
else:
27+
result += "."
28+
qidx += 1
29+
else:
30+
result += char
31+
32+
return result
33+
34+
if __name__ == "__main__":
35+
springs, instructions = load_data("input.txt")
36+
37+
spring = springs[0]
38+
instr = instructions[0]
39+
40+
count_q = spring.count("?")
41+
results = []
42+
for arr_idx in trange(0, 2**instr.count("?")):
43+
results.append(calculate_arrangements(spring, arr_idx, count_q))
44+
print(len(results))
45+
46+
# Note for next time: Sorry
47+
# We have a function to replace ? with stuff
48+
# This slow method gives us every possible case
49+
# Filter by obeying instructions
50+
# Get money

0 commit comments

Comments
 (0)