From 080499077affaff005d4404910b64d487e2d685e Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sat, 24 Dec 2022 01:30:57 +0100 Subject: [PATCH] Day 18 part 1 --- src/day18.rs | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/days.rs | 5 +++ src/main.rs | 2 + 3 files changed, 111 insertions(+) create mode 100644 src/day18.rs diff --git a/src/day18.rs b/src/day18.rs new file mode 100644 index 0000000..a75ddb7 --- /dev/null +++ b/src/day18.rs @@ -0,0 +1,104 @@ +pub struct Cube { + x: i32, + y: i32, + z: i32, +} + +pub fn parse(input: &str) -> Vec { + input + .lines() + .map(|l| { + let xyz: Vec = l + .trim() + .split(',') + .map(|v| str::parse(v).unwrap()) + .collect(); + Cube { + x: xyz[0], + y: xyz[1], + z: xyz[2], + } + }) + .collect() +} + +pub fn surface(cubes: &[Cube]) -> i32 { + let mut matrix: Vec>> = Vec::new(); + + for c in cubes { + for _ in matrix.len()..=c.x as usize { + matrix.push(Vec::new()) + } + for _ in matrix[c.x as usize].len()..=c.y as usize { + matrix[c.x as usize].push(Vec::new()) + } + for _ in matrix[c.x as usize][c.y as usize].len()..=c.z as usize { + matrix[c.x as usize][c.y as usize].push(false) + } + matrix[c.x as usize][c.y as usize][c.z as usize] = true; + } + + let mut surface: i32 = 0; + for x in 0..matrix.len() as i32 { + for y in 0..matrix[x as usize].len() as i32 { + for z in 0..matrix[x as usize][y as usize].len() as i32 { + if matrix[x as usize][y as usize][z as usize] { + for (dx, dy, dz) in [ + (1, 0, 0), + (-1, 0, 0), + (0, 1, 0), + (0, -1, 0), + (0, 0, 1), + (0, 0, -1), + ] { + let (x, y, z) = (x + dx, y + dy, z + dz); + if x < 0 + || x >= matrix.len() as i32 + || y < 0 + || y >= matrix[x as usize].len() as i32 + || z < 0 + || z >= matrix[x as usize][y as usize].len() as i32 + || !matrix[x as usize][y as usize][z as usize] + { + surface = surface + 1; + } + } + } + } + } + } + + surface +} + +pub fn surface_without_trapped_air(cubes: &[Cube]) -> i32 { + 0 // TODO +} + +#[cfg(test)] +mod tests { + use super::*; + + static CUBES: &str = "2,2,2 + 1,2,2 + 3,2,2 + 2,1,2 + 2,3,2 + 2,2,1 + 2,2,3 + 2,2,4 + 2,2,6 + 1,2,5 + 3,2,5 + 2,1,5 + 2,3,5"; + + #[test] + fn part1() { + let cubes = parse(CUBES); + assert_eq!(surface(&cubes), 64); + } + + #[test] + fn part2() {} +} diff --git a/src/days.rs b/src/days.rs index c272123..2dc6343 100644 --- a/src/days.rs +++ b/src/days.rs @@ -178,3 +178,8 @@ pub fn day17() -> String { day17::height(1_000_000_000_000, &movements) ) } + +pub fn day18() -> String { + let cubes = day18::parse(&fs::read_to_string("data/day18.input").unwrap()); + format!("part1: {}, part2: {}", day18::surface(&cubes), 0) +} diff --git a/src/main.rs b/src/main.rs index c030be0..c74cfe5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ mod day14; mod day15; mod day16; mod day17; +mod day18; mod days; #[derive(Parser, Debug)] @@ -53,6 +54,7 @@ fn main() { days::day15, days::day16, days::day17, + days::day18, ]; let args = Args::parse(); -- 2.45.2