+19
-33
2015/18/rust/src/main.rs
+19
-33
2015/18/rust/src/main.rs
···
1
1
use std::mem::swap;
2
2
3
3
#[inline]
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 {
5
5
let isize = size as isize;
6
6
if x == isize || x == -1 || y == isize || y == -1 {
7
7
return 0;
8
8
};
9
-
if *world
10
-
.get(y as usize)
11
-
.expect("invalid position")
12
-
.get(x as usize)
13
-
.expect("invalid position")
14
-
{
9
+
let (x, y) = (x as usize, y as usize);
10
+
if *world.get(y * size + x).expect("invalid position") {
15
11
1
16
12
} else {
17
13
0
···
19
15
}
20
16
21
17
#[inline]
22
-
fn get_at_bool(world: &Vec<Vec<bool>>, x: usize, y: usize) -> bool {
23
-
*world
24
-
.get(y)
25
-
.expect("invalid position")
26
-
.get(x)
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")
28
20
}
29
21
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;
32
25
if stuck {
33
-
let sizem = size - 1;
34
-
world[0][0] = true;
35
-
world[0][sizem] = true;
36
-
world[sizem][0] = true;
37
-
world[sizem][sizem] = true;
26
+
world[0] = true;
27
+
world[sizem] = true;
28
+
world[(size * size) - 1] = true;
29
+
world[size * sizem] = true;
38
30
}
39
31
40
32
for _ in 0..times {
···
47
39
let xp = xo + 1;
48
40
let yp = yo + 1;
49
41
50
-
let was = get_at_bool(&world, x, y);
42
+
let was = get_at_bool(&world, size, x, y);
51
43
let neighbours = get_at(&world, size, xm, ym)
52
44
+ get_at(&world, size, xo, ym)
53
45
+ get_at(&world, size, xp, ym)
···
56
48
+ get_at(&world, size, xm, yp)
57
49
+ get_at(&world, size, xo, yp)
58
50
+ 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);
60
52
}
61
53
}
62
54
···
64
56
65
57
// i hate the duplication here :(
66
58
if stuck {
67
-
let sizem = size - 1;
68
-
world[0][0] = true;
69
-
world[0][sizem] = true;
70
-
world[sizem][0] = true;
71
-
world[sizem][sizem] = true;
59
+
world[0] = true;
60
+
world[sizem] = true;
61
+
world[(size * size) - 1] = true;
62
+
world[size * sizem] = true;
72
63
}
73
64
}
74
65
world
···
77
68
fn main() {
78
69
let input = include_str!("../../input.txt").trim();
79
70
let size = input.split_once("\n").expect("invalid input").0.len();
80
-
let input: Vec<Vec<bool>> = input
81
-
.split("\n")
82
-
.map(|line| line.chars().map(|v| v == '#').collect())
83
-
.collect();
71
+
let input: Vec<bool> = input.replace("\n", "").chars().map(|v| v == '#').collect();
84
72
85
73
let part_1 = generations(100, input.clone(), size, false)
86
74
.iter()
87
-
.flatten()
88
75
.filter(|v| **v)
89
76
.count();
90
77
println!("Part 1: {}", part_1);
91
78
92
79
let part_2 = generations(100, input.clone(), size, true)
93
80
.iter()
94
-
.flatten()
95
81
.filter(|v| **v)
96
82
.count();
97
83
println!("Part 2: {}", part_2);