From 6031a9f16ee43d849f47cba93ed36d6fe8276bb7 Mon Sep 17 00:00:00 2001 From: Jeremy Kaplan Date: Sat, 17 Dec 2022 21:14:50 -0800 Subject: [PATCH] 2022 Day 18 Part 1 --- 2022/src/bin/day18.rs | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2022/src/bin/day18.rs diff --git a/2022/src/bin/day18.rs b/2022/src/bin/day18.rs new file mode 100644 index 0000000..1a34fee --- /dev/null +++ b/2022/src/bin/day18.rs @@ -0,0 +1,50 @@ +const INPUT: &str = include_str!("../../input/day18.txt"); + +fn main() { + println!("{}", part1(INPUT)); +} + +fn part1(input: &str) -> usize { + let cubes: Vec = aoc::lines(input) + .map(|line| { + let coords: Vec<&str> = line.split(',').collect(); + let x = coords[0].parse().unwrap(); + let y = coords[1].parse().unwrap(); + let z = coords[2].parse().unwrap(); + Cube::new(x, y, z) + }) + .collect(); + + let mut shared_faces = 0; + for (c1, c2) in itertools::iproduct!(&cubes, &cubes) { + if c1 == c2 { + continue; + } + + if c1.touches(c2) { + shared_faces += 1; + } + } + + 6 * cubes.len() - shared_faces +} + +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +struct Cube { + x: u32, + y: u32, + z: u32, +} + +impl Cube { + fn new(x: u32, y: u32, z: u32) -> Self { + Self { x, y, z } + } + + fn touches(&self, other: &Self) -> bool { + let dx = (self.x as i32 - other.x as i32).abs(); + let dy = (self.y as i32 - other.y as i32).abs(); + let dz = (self.z as i32 - other.z as i32).abs(); + (dx + dy + dz) == 1 + } +}