"""Download and independently check Brouwer's published constant-weight code."""
from itertools import combinations
from pathlib import Path
from urllib.request import urlopen

SOURCE = 'https://aeb.win.tue.nl/codes/cwc/d10/a58.10.6.99H'
destination = Path(__file__).with_name('brouwer-58-99.hex')
with urlopen(SOURCE, timeout=40) as request:
    data = request.read()
lines = data.decode('ascii').splitlines()
assert lines[0].strip() == '$BASE=16'
blocks = [int(s, 16) for s in lines[1:] if s.strip()]
assert len(set(blocks)) == len(blocks) == 99
assert all(b.bit_length() <= 58 and b.bit_count() == 6 for b in blocks)
assert all((a & b).bit_count() <= 1 for a, b in combinations(blocks, 2))
destination.write_bytes(data)
print('Verified 99 published blocks on 58 points and all 4851 intersections.')
