summaryrefslogtreecommitdiffstats
path: root/prog/aoc/23/12/1.py
blob: 7e079350b7960a00634330a983da9be6c596e29b (plain) (blame)
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
#!/usr/bin/python3
springs = []
try:
	while True:
		s = input()
		springs.append((s.split(" ")[0], list(map(int, s.split(" ")[1].split(",")))))
except EOFError:
	pass
def counts(vhod):
	s = vhod + "$"
	prev = None
	r = []
	run = 0
	for c in s:
		if prev == None:
			prev = c
			run = 1
			continue
		if c == prev:
			run += 1
			continue
		if prev == "#":
			r.append(run)
		prev = c
		run = 1
	return r
"""
for spring in springs:
	print(counts(spring[0]), spring[1])
"""
def possibilities(x):
	r = 0
	countq = x[0].count("?")
	for bits in range(2**countq):
		copy = x[0]
		for i in range(countq):
			if bits & (1 << i):
				copy = copy.replace("?", ".", 1)
			else:
				copy = copy.replace("?", "#", 1)
		if counts(copy) == x[1]:
			r += 1
	return r
def possibilities_serial(array):
	s = 0
	for element in array:
		s += possibilities(element)
	return s
batch_size = 10
batches = [springs[x:x+batch_size] for x in range(0, len(springs), batch_size)]
from multiprocessing import Pool
with Pool(len(batches)) as p:
	print(sum(p.map(possibilities_serial, batches)))