From 4a1745eb923031ffe75f275d8b3a2ded8a867257 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Thu, 22 Dec 2022 23:16:37 +0000 Subject: day18 pt1 --- src/day18.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 ++++-- 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/day18.rs (limited to 'src') 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::().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); +} \ 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!")); } -- cgit v1.2.3-ZIG