···
-
use std::{iter::once, mem::swap};
fn generations(times: u32, mut world: Vec<u8>, size: usize, stuck: bool) -> Vec<u8> {
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) }
-
let mut new_world = vec![0_u8; size * size];
-
world[(size * size) - 1] = 1;
-
world[size * sizem] = 1;
···
+ 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> = once(".".repeat(size))
-
.chain(input.split("\n"))
-
.chain(".".repeat(size))
-
.chain(line.chars().map(|v| (v == '#') as u8))
···
fn generations(times: u32, mut world: Vec<u8>, size: usize, stuck: bool) -> Vec<u8> {
+
fn pos(x: usize, y: usize, size: usize) -> usize {
+
(1 + y) * (size + 2) + (1 + x)
fn get_at(world: &Vec<u8>, size: usize, x: usize, y: usize) -> u8 {
// this is in known bounds
+
unsafe { *world.get_unchecked(pos(x, y, size)) }
+
let mut new_world = vec![0_u8; (size + 2).pow(2)];
+
world[pos(0, 0, size)] = 1;
+
world[pos(sizem, 0, size)] = 1;
+
world[pos(0, sizem, size)] = 1;
+
world[pos(sizem, sizem, size)] = 1;
···
+ get_at(&world, size, xm, yp)
+ get_at(&world, size, xo, yp)
+ get_at(&world, size, xp, yp);
+
new_world[pos(xo, yo, size)] = (neighbours == 3 || (neighbours == 2 && was)) as u8;
···
// i hate the duplication here :(
+
world[pos(0, 0, size)] = 1;
+
world[pos(sizem, 0, size)] = 1;
+
world[pos(0, sizem, size)] = 1;
+
world[pos(sizem, sizem, size)] = 1;
···
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
+
// reads the input but adds a line of buffer on the sides
+
let buffer_line = ".".repeat(size);
+
let input: Vec<u8> = format!("{buffer_line}\n{input}\n{buffer_line}")
+
.map(|line| -> Vec<u8> {
+
.map(|v| (v == '#') as u8)