Watch
1
0
Fork
You've already forked advent-of-code
0

2022 Day 18 Part 1

This commit is contained in:
Jeremy Kaplan 2022-12-17 21:14:50 -08:00
commit 6031a9f16e

50
2022/src/bin/day18.rs Normal file
View file

@ -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<Cube> = 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
}
}