Day 18 aoc in progress, not liking rust today
This commit is contained in:
32
advent_of_code/2022/18/Cargo.lock
generated
Normal file
32
advent_of_code/2022/18/Cargo.lock
generated
Normal file
@@ -0,0 +1,32 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
9
advent_of_code/2022/18/Cargo.toml
Normal file
9
advent_of_code/2022/18/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
nom = "7.1.3"
|
||||
2804
advent_of_code/2022/18/input.txt
Normal file
2804
advent_of_code/2022/18/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
124
advent_of_code/2022/18/src/main.rs
Normal file
124
advent_of_code/2022/18/src/main.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use std::cmp::max;
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use std::vec;
|
||||
|
||||
use nom::{
|
||||
bytes::streaming::tag,
|
||||
character::complete::{self, multispace1},
|
||||
multi::separated_list1,
|
||||
sequence::{terminated, tuple},
|
||||
IResult, Parser,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Coord {
|
||||
x: u32,
|
||||
y: u32,
|
||||
z: u32,
|
||||
}
|
||||
|
||||
impl Display for Coord {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result {
|
||||
write!(f, "{},{},{}", self.x, self.y, self.z)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct LavaBlob {
|
||||
coord: Coord,
|
||||
number_of_neighbours: u32,
|
||||
}
|
||||
|
||||
type LavaGrid = Vec<Vec<Vec<Option<LavaBlob>>>>;
|
||||
|
||||
fn parse_input(input: &str) -> IResult<&str, Vec<Coord>> {
|
||||
let (input, coords) = separated_list1(
|
||||
multispace1,
|
||||
tuple((
|
||||
terminated(complete::u32, tag(",")),
|
||||
terminated(complete::u32, tag(",")),
|
||||
complete::u32,
|
||||
))
|
||||
.map(|(x, y, z)| Coord { x, y, z }),
|
||||
)(input)?;
|
||||
|
||||
Ok((input, coords))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input_text = include_str!("../test.txt");
|
||||
let (_rest, coordinates) = parse_input(input_text).unwrap();
|
||||
|
||||
// Test the parsing
|
||||
println!("Testing the parsing");
|
||||
for coord in &coordinates {
|
||||
println!("{}", coord);
|
||||
}
|
||||
println!();
|
||||
|
||||
let max_grid_value = coordinates
|
||||
.iter()
|
||||
.map(|&coord| max(max(coord.x, coord.y), coord.z))
|
||||
.max()
|
||||
.unwrap() as usize;
|
||||
let mut lava_grid: LavaGrid =
|
||||
vec![vec![vec![None; max_grid_value + 1]; max_grid_value + 1]; max_grid_value + 1];
|
||||
|
||||
for coord in &coordinates {
|
||||
let mut number_of_neighbours = 6;
|
||||
let x = coord.x;
|
||||
let y = coord.y;
|
||||
let z = coord.z;
|
||||
|
||||
let mut coords_to_check = vec![];
|
||||
if x > 0 {
|
||||
coords_to_check.push(Coord { x: x - 1, y, z });
|
||||
}
|
||||
if x < max_grid_value as u32 {
|
||||
coords_to_check.push(Coord { x: x + 1, y, z });
|
||||
}
|
||||
if y > 0 {
|
||||
coords_to_check.push(Coord { x, y: y - 1, z });
|
||||
}
|
||||
if y < max_grid_value as u32 {
|
||||
coords_to_check.push(Coord { x, y: y + 1, z });
|
||||
}
|
||||
if z > 0 {
|
||||
coords_to_check.push(Coord { x, y, z: z - 1 })
|
||||
}
|
||||
if z < max_grid_value as u32 {
|
||||
coords_to_check.push(Coord { x, y, z: z + 1 });
|
||||
}
|
||||
|
||||
for coord_to_check in &coords_to_check {
|
||||
match lava_grid[coord_to_check.x as usize][coord_to_check.y as usize]
|
||||
[coord_to_check.z as usize]
|
||||
{
|
||||
None => continue,
|
||||
Some(mut blob) => {
|
||||
number_of_neighbours -= 1;
|
||||
blob.number_of_neighbours -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lava_grid[x as usize][y as usize][z as usize] = Some(LavaBlob {
|
||||
coord: coord.clone(),
|
||||
number_of_neighbours,
|
||||
})
|
||||
}
|
||||
|
||||
let mut surface_area = 0;
|
||||
for layer in &lava_grid {
|
||||
for row in layer {
|
||||
for optional_blob in row {
|
||||
match optional_blob {
|
||||
None => continue,
|
||||
Some(blob) => surface_area += blob.number_of_neighbours,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Surface area: {}", surface_area);
|
||||
}
|
||||
13
advent_of_code/2022/18/test.txt
Normal file
13
advent_of_code/2022/18/test.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
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
|
||||
Reference in New Issue
Block a user