···
1
-
use std::{iter::once, mem::swap};
fn generations(times: u32, mut world: Vec<u8>, size: usize, stuck: bool) -> Vec<u8> {
5
+
fn pos(x: usize, y: usize, size: usize) -> usize {
6
+
(1 + y) * (size + 2) + (1 + x)
fn get_at(world: &Vec<u8>, size: usize, x: usize, y: usize) -> u8 {
6
-
// benefits from the integer overflow to simplify code
7
-
if x >= size || y >= size {
// this is in known bounds
11
-
unsafe { *world.get_unchecked(y * size + x) }
12
+
unsafe { *world.get_unchecked(pos(x, y, size)) }
14
-
let mut new_world = vec![0_u8; size * size];
17
+
let mut new_world = vec![0_u8; (size + 2).pow(2)];
19
-
world[(size * size) - 1] = 1;
20
-
world[size * sizem] = 1;
20
+
world[pos(0, 0, size)] = 1;
21
+
world[pos(sizem, 0, size)] = 1;
22
+
world[pos(0, sizem, size)] = 1;
23
+
world[pos(sizem, sizem, size)] = 1;
24
-
for yo in 1..=size {
27
-
for xo in 1..=size {
···
+ get_at(&world, size, xm, yp)
+ get_at(&world, size, xo, yp)
+ get_at(&world, size, xp, yp);
40
-
new_world[yo * size + xo] = (neighbours == 3 || (neighbours == 2 && was)) as u8;
43
+
new_world[pos(xo, yo, size)] = (neighbours == 3 || (neighbours == 2 && was)) as u8;
···
// i hate the duplication here :(
50
-
world[(size * size) - 1] = 1;
51
-
world[size * sizem] = 1;
51
+
world[pos(0, 0, size)] = 1;
52
+
world[pos(sizem, 0, size)] = 1;
53
+
world[pos(0, sizem, size)] = 1;
54
+
world[pos(sizem, sizem, size)] = 1;
···
let input = include_str!("../../input.txt").trim();
let size = input.split_once("\n").expect("invalid input").0.len();
60
-
let input: Vec<u8> = once(".".repeat(size))
61
-
.chain(input.split("\n"))
62
-
.chain(".".repeat(size))
65
-
.chain(line.chars().map(|v| (v == '#') as u8))
63
+
// reads the input but adds a line of buffer on the sides
64
+
let buffer_line = ".".repeat(size);
65
+
let input: Vec<u8> = format!("{buffer_line}\n{input}\n{buffer_line}")
67
+
.map(|line| -> Vec<u8> {
68
+
format!(".{}.", line)
70
+
.map(|v| (v == '#') as u8)