From e9660f065175f96ad4f800502fcf2f3577c2e4b8 Mon Sep 17 00:00:00 2001
From: Runxi Yu <me@runxiyu.org>
Date: Thu, 27 Mar 2025 19:39:37 +0800
Subject: Make the python solver detect when there are missing arguments

---
 .editorconfig    |  4 ++++
 static/solver.py | 28 ++++++++++++++++------------
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/.editorconfig b/.editorconfig
index 093c47b..b51a366 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -17,3 +17,7 @@ indent_size = 2
 [*.html]
 tab_size = 2
 indent_size = 2
+
+[*.py]
+tab_size = 4
+indent_size = 4
diff --git a/static/solver.py b/static/solver.py
index 83841fb..d067f8b 100644
--- a/static/solver.py
+++ b/static/solver.py
@@ -7,23 +7,27 @@ import sys
 import struct
 
 def validate_bit_zeros(bs: bytes, n: int) -> bool:
-	q, r = divmod(n, 8)
-	if any(b != 0 for b in bs[:q]):
-		return False
-	if r and (bs[q] & (0xFF << (8 - r))):
-		return False
-	return True
+    q, r = divmod(n, 8)
+    if any(b != 0 for b in bs[:q]):
+        return False
+    if r and (bs[q] & (0xFF << (8 - r))):
+        return False
+    return True
+
+if len(sys.argv) < 3:
+	print(f"usage: {sys.argv[0]} <base64 identifier> <difficulty>", file=sys.stderr)
+	sys.exit(1)
 
 decoded = base64.b64decode(sys.argv[1])
 difficulty = int(sys.argv[2])
 next_val = 0
 
 while True:
-	h = hashlib.sha256(decoded + struct.pack("Q", next_val)).digest()
-	if validate_bit_zeros(h, difficulty):
-		break
-	next_val = (next_val + 1) & 0xFFFFFFFFFFFFFFFF
-	if next_val == 0:
-		raise ValueError("overflow")
+    h = hashlib.sha256(decoded + struct.pack("Q", next_val)).digest()
+    if validate_bit_zeros(h, difficulty):
+        break
+    next_val = (next_val + 1) & 0xFFFFFFFFFFFFFFFF
+    if next_val == 0:
+        raise ValueError("overflow")
 
 print(base64.b64encode(struct.pack("Q", next_val)).decode())
-- 
cgit v1.2.3