-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcset.py
34 lines (26 loc) · 1 KB
/
cset.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
# Fixing an Inconsistent Character Set
# We can find conflicts if there's an intersection in all implied sets
# between two character rows
from collections import defaultdict
from itertools import product
def conflict(c1, c2):
for a, b in product([1, 0], repeat=2):
s1 = set(i for i, c in enumerate(c1) if c == a)
s2 = set(i for i, c in enumerate(c2) if c == b)
if len(s1.intersection(s2)) == 0:
return False
return True
def conflicts(characters):
count = defaultdict(int)
for i in range(len(characters)):
for j in range(i + 1, len(characters)):
if conflict(characters[i], characters[j]):
count[i] += 1
count[j] += 1
return count
def main(file):
lines = open(file).read().splitlines()
characters = [[int(x) for x in list(ch)] for ch in lines]
count = conflicts(characters)
rm = [k for k, v in count.items() if v == max(count.values())][0]
print(*(lines[:rm] + lines[rm + 1 :]), sep="\n")