Created
August 9, 2020 17:07
-
-
Save keegancsmith/20e61a2b20ae6904eddde18bbfa74dbf to your computer and use it in GitHub Desktop.
Magnus Distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# We read from stdin a list of lichess games of the format winner loser gameid. | |
# We calculate the "erdos number", but for people who have defeated magnus. | |
# We output number of people with a specific magnus number. Uses a BFS. | |
import fileinput | |
from collections import defaultdict, deque | |
# G is a graph. name -> { opponents won } | |
G = defaultdict(set) | |
for line in fileinput.input(): | |
winner, loser, game = line.split() | |
G[loser].add(winner) | |
start = 'DrNykterstein' | |
counts = defaultdict(int) | |
seen = set() | |
q = deque([(start, 0)]) | |
while q: | |
name, d = q.popleft() | |
counts[d] += 1 | |
for winner in G[name]: | |
if winner not in seen: | |
seen.add(winner) | |
q.append((winner, d + 1)) | |
for v, count in sorted(counts.items()): | |
print(v, count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment