-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconwei.rs
106 lines (93 loc) · 2.49 KB
/
conwei.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
fn main_() {
let mut front: Grid = [[0; COLS]; ROWS];
let mut back: Grid = [[0; COLS]; ROWS];
initialize(&mut front, Init::Seeded(999));
if FFWD {
let max = 10_000;
for i in 0..=max {
display(&front);
next_generation(&front, &mut back);
front.copy_from_slice(&back);
println!("{}/{}", i, max);
print!("\x1B[{}A", ROWS + 1);
}
}
loop {
display(&front);
next_generation(&front, &mut back);
front.copy_from_slice(&back);
print!("\x1B[{}A", ROWS);
thread::sleep(Duration::from_millis(100));
}
}
use rand::prelude::*;
use std::{thread, time::Duration};
const FFWD: bool = true;
const ROWS: usize = 32;
const COLS: usize = ROWS * 2;
type Grid = [[u8; COLS]; ROWS];
#[allow(unused)]
enum Init {
Glider,
Random,
Seeded(u64),
}
fn initialize(grid: &mut Grid, init_type: Init) {
let mut randomize = |mut rng: StdRng| {
for row in grid.iter_mut() {
for cell in row.iter_mut() {
*cell = if rng.gen_bool(0.5) { 1 } else { 0 };
}
}
};
match init_type {
Init::Glider => {
grid[0][1] = 1;
grid[1][2] = 1;
grid[2][0] = 1;
grid[2][1] = 1;
grid[2][2] = 1;
}
Init::Random => {
randomize(rand::rngs::StdRng::from_entropy());
}
Init::Seeded(seed) => {
randomize(rand::rngs::StdRng::seed_from_u64(seed));
}
}
}
fn display(grid: &Grid) {
for row in grid.iter() {
for &cell in row.iter() {
print!("{}", if cell == 1 { '#' } else { '.' });
}
println!();
}
}
fn next_generation(front: &Grid, back: &mut Grid) {
for y in 0..ROWS {
for x in 0..COLS {
let neighbors = count_neighbors(front, x, y);
back[y][x] = match (front[y][x], neighbors) {
(1, 2) | (1, 3) => 1,
(1, _) => 0,
(0, 3) => 1,
_ => 0,
};
}
}
}
fn count_neighbors(grid: &Grid, cx: usize, cy: usize) -> u8 {
let mut count = 0;
for dy in -1..=1 {
for dx in -1..=1 {
if dx == 0 && dy == 0 {
continue;
}
let x = (cx as isize + dx).rem_euclid(COLS as isize) as usize;
let y = (cy as isize + dy).rem_euclid(ROWS as isize) as usize;
count += grid[y][x];
}
}
count
}