···
-
fn get_at(world: &Vec<bool>, size: usize, x: usize, y: usize) -> u8 {
// benefits from the integer overflow to simplify code
if x >= size || y >= size {
// this is in known bounds
-
unsafe { *world.get_unchecked(y * size + x) as u8 }
-
fn get_at_bool(world: &Vec<bool>, size: usize, x: usize, y: usize) -> bool {
-
// this is in known bounds
unsafe { *world.get_unchecked(y * size + x) }
-
fn generations(times: u32, mut world: Vec<bool>, size: usize, stuck: bool) -> Vec<bool> {
-
let mut new_world = vec![false; size * size];
-
world[(size * size) - 1] = true;
-
world[size * sizem] = true;
···
let xm = xo.wrapping_sub(1);
-
let was = get_at_bool(&world, size, xo, yo);
let neighbours = get_at(&world, size, xm, ym)
+ get_at(&world, size, xo, ym)
+ get_at(&world, size, xp, ym)
···
+ get_at(&world, size, xm, yp)
+ get_at(&world, size, xo, yp)
+ get_at(&world, size, xp, yp);
-
new_world[yo * size + xo] = neighbours == 3 || (neighbours == 2 && was);
···
// i hate the duplication here :(
-
world[(size * size) - 1] = true;
-
world[size * sizem] = true;
···
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
-
let input: Vec<bool> = input.replace("\n", "").chars().map(|v| v == '#').collect();
let part_1 = generations(100, input.clone(), size, false)
println!("Part 1: {}", part_1);
let part_2 = generations(100, input.clone(), size, true)
println!("Part 2: {}", part_2);
···
+
fn get_at(world: &Vec<u8>, size: usize, x: usize, y: usize) -> u8 {
// benefits from the integer overflow to simplify code
if x >= size || y >= size {
// this is in known bounds
unsafe { *world.get_unchecked(y * size + x) }
+
fn generations(times: u32, mut world: Vec<u8>, size: usize, stuck: bool) -> Vec<u8> {
+
let mut new_world = vec![0_u8; size * size];
+
world[(size * size) - 1] = 1;
+
world[size * sizem] = 1;
···
let xm = xo.wrapping_sub(1);
+
let was = get_at(&world, size, xo, yo) == 1;
let neighbours = get_at(&world, size, xm, ym)
+ get_at(&world, size, xo, ym)
+ get_at(&world, size, xp, ym)
···
+ get_at(&world, size, xm, yp)
+ get_at(&world, size, xo, yp)
+ get_at(&world, size, xp, yp);
+
new_world[yo * size + xo] = (neighbours == 3 || (neighbours == 2 && was)) as u8;
···
// i hate the duplication here :(
+
world[(size * size) - 1] = 1;
+
world[size * sizem] = 1;
···
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
+
let input: Vec<u8> = input
+
.map(|v| (v == '#') as u8)
let part_1 = generations(100, input.clone(), size, false)
println!("Part 1: {}", part_1);
let part_2 = generations(100, input.clone(), size, true)
println!("Part 2: {}", part_2);