diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/day18.rs | 44 | ||||
-rw-r--r-- | src/main.rs | 6 |
2 files changed, 48 insertions, 2 deletions
diff --git a/src/day18.rs b/src/day18.rs new file mode 100644 index 0000000..636b342 --- /dev/null +++ b/src/day18.rs @@ -0,0 +1,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); +}
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 47f41a7..e865636 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,8 @@ use std::fs; // mod day14; // mod day15; // mod day16; -mod day17; +//mod day17; +mod day18; fn main() { // day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!")); @@ -38,5 +39,6 @@ fn main() { // day14::run(fs::read_to_string("input/day14.txt").expect("Failed to read input file!")); // day15::run(fs::read_to_string("input/day15.txt").expect("Failed to read input file!")); // day16::run(fs::read_to_string("input/day16.txt").expect("Failed to read input file!")); - day17::run(fs::read_to_string("input/day17.txt").expect("Failed to read input file!")); + //day17::run(fs::read_to_string("input/day17.txt").expect("Failed to read input file!")); + day18::run(fs::read_to_string("input/day18.txt").expect("Failed to read input file!")); } |