easyctf-2017/count.py

48 lines
1.5 KiB
Python
Raw Normal View History

2017-03-03 02:33:16 +00:00
#!/usr/bin/env python
import os
import yaml
import traceback
from collections import Counter
problem_names = os.listdir(os.path.dirname(os.path.abspath(__file__)))
problems = []
2017-03-11 07:51:03 +00:00
failed = []
2017-03-13 05:50:22 +00:00
total = 0
2017-03-11 07:51:03 +00:00
2017-03-03 02:33:16 +00:00
for problem_name in problem_names:
2017-03-11 07:51:03 +00:00
folder = os.path.dirname(os.path.abspath(__file__)) + os.sep + problem_name
if not (os.path.exists(folder) and os.path.isdir(folder)): continue
2017-03-03 02:33:16 +00:00
try:
2017-03-11 07:51:03 +00:00
metadata_file = folder + os.sep + "problem.yml"
2017-03-03 02:33:16 +00:00
with open(metadata_file, "r") as f:
metadata_raw = f.read()
metadata = yaml.load(metadata_raw)
if "category" in metadata:
problems.append(metadata)
except:
2017-03-11 07:51:03 +00:00
failed.append(problem_name)
2017-03-03 02:33:16 +00:00
2017-03-06 07:58:08 +00:00
problems.sort(key=lambda p: p.get("value"), reverse=True)
print("Grand Total: %d" % len(problems))
print("Category Breakdown:")
2017-03-03 02:33:16 +00:00
2017-03-07 22:04:58 +00:00
maxtitle = max(map(lambda p: len(p.get("title")), problems)) + 3
maxauthor = max(map(lambda p: len(p.get("author")), problems)) + 3
2017-03-03 02:33:16 +00:00
c = Counter(map(lambda p: p.get("category", ""), problems))
categories = sorted(c.items(), key=lambda c: c[1], reverse=True)
for category, count in categories:
print(" %s: %s" % (category, count))
2017-03-03 02:33:16 +00:00
for problem in problems:
if problem.get("category") != category: continue
2017-03-13 05:50:22 +00:00
total += int(problem.get("value"))
print(" %s %s %sp" % (problem.get("title") + " " * (maxtitle - len(problem.get("title"))), problem.get("author") + " " * (maxauthor - len(problem.get("author"))), problem.get("value")))
2017-03-11 07:51:03 +00:00
2017-03-13 05:50:22 +00:00
print ("\nTOTAL VALUE: %d" % total)
2017-03-11 07:51:03 +00:00
print("\nThe following problems failed to parse.")
for title in failed:
if title in [".git"]: continue
print(" %s" % title)