1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
fn adjescent(p: &(usize,usize,usize)) -> Vec<(usize,usize,usize)> {
let mut v = vec![];
v.push((p.0+1,p.1,p.2));
v.push((p.0,p.1+1,p.2));
v.push((p.0,p.1,p.2+1));
if p.0 > 0 {
v.push((p.0-1,p.1,p.2));
}
if p.1 > 0 {
v.push((p.0,p.1-1,p.2));
}
if p.2 > 0 {
v.push((p.0,p.1,p.2-1));
}
v
}
pub fn run(input:String) {
let cubes = input.lines()
.map(|line| {
let res = line.split(",").map(|t| {t.parse::<usize>().unwrap()}).collect::<Vec<usize>>();
if res.len() != 3 {
panic!("malformatted line! {}", line);
}
(res[0], res[1], res[2])
}).collect::<Vec<(usize,usize,usize)>>();
let mx: usize = cubes.iter().map(|t| {vec![t.0, t.1, t.2]}).flatten().max().unwrap() + 3;
// Allocate a 3d space..
let mut grid: Vec<Vec<Vec<bool>>> = (0..mx).map(|_| {(0..mx).map(|_| {(0..mx).map(|_| {false}).collect()}).collect()}).collect();
for cube in cubes.iter() {
grid[cube.0][cube.1][cube.2] = true;
}
// Now count cubes. Each contributes 6,minus number of connections
let surface_area: usize = cubes.iter().map(|cube| {
let mut score = 6;
for p in adjescent(cube) {
if grid[p.0][p.1][p.2] {
score -= 1;
}
}
score
}).sum();
println!("Day 18: {}", surface_area);
}
|