···
4
-
fn get_at(world: &Vec<bool>, size: usize, x: usize, y: usize) -> u8 {
4
+
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
10
-
unsafe { *world.get_unchecked(y * size + x) as u8 }
14
-
fn get_at_bool(world: &Vec<bool>, size: usize, x: usize, y: usize) -> bool {
15
-
// this is in known bounds
unsafe { *world.get_unchecked(y * size + x) }
19
-
fn generations(times: u32, mut world: Vec<bool>, size: usize, stuck: bool) -> Vec<bool> {
20
-
let mut new_world = vec![false; size * size];
13
+
fn generations(times: u32, mut world: Vec<u8>, size: usize, stuck: bool) -> Vec<u8> {
14
+
let mut new_world = vec![0_u8; size * size];
24
-
world[sizem] = true;
25
-
world[(size * size) - 1] = true;
26
-
world[size * sizem] = true;
19
+
world[(size * size) - 1] = 1;
20
+
world[size * sizem] = 1;
···
let xm = xo.wrapping_sub(1);
37
-
let was = get_at_bool(&world, size, xo, yo);
31
+
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);
46
-
new_world[yo * size + xo] = neighbours == 3 || (neighbours == 2 && was);
40
+
new_world[yo * size + xo] = (neighbours == 3 || (neighbours == 2 && was)) as u8;
···
// i hate the duplication here :(
55
-
world[sizem] = true;
56
-
world[(size * size) - 1] = true;
57
-
world[size * sizem] = true;
50
+
world[(size * size) - 1] = 1;
51
+
world[size * sizem] = 1;
···
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
66
-
let input: Vec<bool> = input.replace("\n", "").chars().map(|v| v == '#').collect();
60
+
let input: Vec<u8> = input
63
+
.map(|v| (v == '#') as u8)
let part_1 = generations(100, input.clone(), size, false)
68
+
.filter(|v| **v == 1)
println!("Part 1: {}", part_1);
let part_2 = generations(100, input.clone(), size, true)
74
+
.filter(|v| **v == 1)
println!("Part 2: {}", part_2);