2022 day 18
parent
756cc390e1
commit
97f5cf42af
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,35 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead,BufReader};
|
||||
use std::str::FromStr;
|
||||
|
||||
|
||||
pub fn main() {
|
||||
let input_file = File::open("input").expect("File open");
|
||||
let reader = BufReader::new(input_file);
|
||||
let mut grid = HashSet::new();
|
||||
for line in reader.lines().map(|l| l.unwrap()) {
|
||||
let parts: Vec<&str> = line.split(",").collect();
|
||||
if parts.len() != 3 { panic!(); }
|
||||
let mut coords = [0;3];
|
||||
for (i, istr) in line.split(",").enumerate() {
|
||||
coords[i] = u8::from_str(istr).unwrap();
|
||||
}
|
||||
grid.insert(coords);
|
||||
}
|
||||
|
||||
let mut open_faces = 0;
|
||||
for coords in grid.iter() {
|
||||
for (i,c) in coords.iter().enumerate() {
|
||||
if !grid.contains(&with_coord(coords, i, c + 1)) { open_faces += 1; }
|
||||
if *c == 0 || !grid.contains(&with_coord(coords, i, c - 1)) { open_faces += 1; }
|
||||
}
|
||||
}
|
||||
println!("{open_faces}");
|
||||
}
|
||||
|
||||
fn with_coord(existing: &[u8;3], i: usize, new: u8) -> [u8;3] {
|
||||
let mut new_coord = existing.clone();
|
||||
new_coord[i] = new;
|
||||
new_coord
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead,BufReader};
|
||||
use std::str::FromStr;
|
||||
|
||||
|
||||
pub fn main() {
|
||||
let input_file = File::open("input").expect("File open");
|
||||
let reader = BufReader::new(input_file);
|
||||
let mut filled = HashSet::new();
|
||||
let mut min_coord = 0;
|
||||
let mut max_coord = 0;
|
||||
for line in reader.lines().map(|l| l.unwrap()) {
|
||||
let parts: Vec<&str> = line.split(",").collect();
|
||||
if parts.len() != 3 { panic!(); }
|
||||
let mut coords = [0;3];
|
||||
for (i, istr) in line.split(",").enumerate() {
|
||||
coords[i] = i8::from_str(istr).unwrap();
|
||||
if coords[i] < min_coord { min_coord = coords[i]; }
|
||||
if coords[i] > max_coord { max_coord = coords[i]; }
|
||||
}
|
||||
filled.insert(coords);
|
||||
}
|
||||
|
||||
let origin = [max_coord + 1;3];
|
||||
let mut queue = vec![origin];
|
||||
let mut exposed = HashSet::from([origin]);
|
||||
let mut open_faces = 0;
|
||||
while let Some(coords) = queue.pop() {
|
||||
for (i,c) in coords.iter().enumerate() {
|
||||
if *c <= max_coord {
|
||||
let pos = with_coord(&coords, i, c + 1);
|
||||
if filled.contains(&pos) {
|
||||
open_faces += 1;
|
||||
} else if exposed.insert(pos) {
|
||||
queue.push(pos);
|
||||
}
|
||||
}
|
||||
|
||||
if *c >= min_coord {
|
||||
let neg = with_coord(&coords, i, c - 1);
|
||||
if filled.contains(&neg) {
|
||||
open_faces += 1;
|
||||
} else if exposed.insert(neg) {
|
||||
queue.push(neg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{open_faces}");
|
||||
}
|
||||
|
||||
fn with_coord(existing: &[i8;3], i: usize, new: i8) -> [i8;3] {
|
||||
let mut new_coord = existing.clone();
|
||||
new_coord[i] = new;
|
||||
new_coord
|
||||
}
|
Loading…
Reference in New Issue