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::().unwrap()}).collect::>(); if res.len() != 3 { panic!("malformatted line! {}", line); } (res[0], res[1], res[2]) }).collect::>(); 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>> = (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); }