···
4
-
fn get_at(world: &Vec<Vec<bool>>, size: usize, x: isize, y: isize) -> u8 {
4
+
fn get_at(world: &Vec<bool>, size: usize, x: isize, y: isize) -> u8 {
let isize = size as isize;
if x == isize || x == -1 || y == isize || y == -1 {
11
-
.expect("invalid position")
13
-
.expect("invalid position")
9
+
let (x, y) = (x as usize, y as usize);
10
+
if *world.get(y * size + x).expect("invalid position") {
···
22
-
fn get_at_bool(world: &Vec<Vec<bool>>, x: usize, y: usize) -> bool {
25
-
.expect("invalid position")
27
-
.expect("invalid position")
18
+
fn get_at_bool(world: &Vec<bool>, size: usize, x: usize, y: usize) -> bool {
19
+
*world.get(y * size + x).expect("invalid position")
30
-
fn generations(times: u32, mut world: Vec<Vec<bool>>, size: usize, stuck: bool) -> Vec<Vec<bool>> {
31
-
let mut new_world = vec![vec![false; size]; size];
22
+
fn generations(times: u32, mut world: Vec<bool>, size: usize, stuck: bool) -> Vec<bool> {
23
+
let mut new_world = vec![false; size * size];
24
+
let sizem = size - 1;
33
-
let sizem = size - 1;
35
-
world[0][sizem] = true;
36
-
world[sizem][0] = true;
37
-
world[sizem][sizem] = true;
27
+
world[sizem] = true;
28
+
world[(size * size) - 1] = true;
29
+
world[size * sizem] = true;
···
50
-
let was = get_at_bool(&world, x, y);
42
+
let was = get_at_bool(&world, size, x, y);
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);
59
-
new_world[y][x] = neighbours == 3 || (neighbours == 2 && was);
51
+
new_world[y * size + x] = neighbours == 3 || (neighbours == 2 && was);
···
// i hate the duplication here :(
67
-
let sizem = size - 1;
69
-
world[0][sizem] = true;
70
-
world[sizem][0] = true;
71
-
world[sizem][sizem] = true;
60
+
world[sizem] = true;
61
+
world[(size * size) - 1] = true;
62
+
world[size * sizem] = true;
···
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
80
-
let input: Vec<Vec<bool>> = input
82
-
.map(|line| line.chars().map(|v| v == '#').collect())
71
+
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);