diff --git a/2022/day14/rust/Cargo.toml b/2022/day14/rust/Cargo.toml new file mode 100644 index 0000000..a05c5af --- /dev/null +++ b/2022/day14/rust/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust_2022_14" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +aoc = { path = "../../../common/rust" } +nom = "7.1.1" diff --git a/2022/day14/rust/src/main.rs b/2022/day14/rust/src/main.rs new file mode 100644 index 0000000..c674d18 --- /dev/null +++ b/2022/day14/rust/src/main.rs @@ -0,0 +1,83 @@ +#![warn(clippy::pedantic)] + +use std::{collections::BTreeSet, io::stdin}; + +use aoc::vec2::{Line, Vec2}; +use nom::{ + bytes::complete::tag, character::complete::i32, multi::separated_list1, + sequence::separated_pair, IResult, Parser, +}; + +fn parse_points(s: &str) -> IResult<&str, Vec, ()> { + separated_list1( + tag(" -> "), + separated_pair(i32, tag(","), i32).map(Vec2::from), + )(s) +} + +fn simulate(structures: BTreeSet, floor: Option) -> usize { + let mut points = structures; + let lowest = points.iter().map(|v| v.y).max().unwrap(); + let start = Vec2::new(500, 0); + + let mut pieces = 0; + 'pieces: loop { + let mut pos = start; + 'fall: loop { + let direction = [(0, 1), (-1, 1), (1, 1)] + .into_iter() + .map(Vec2::from) + .find(|&direction| !points.contains(&(pos + direction))); + + if let Some(direction) = direction { + pos += direction; + + match floor { + Some(offset) => { + if pos.y >= lowest + i32::try_from(offset).unwrap() - 1 { + // landed on the floor + break 'fall; + } + } + None => { + if pos.y >= lowest { + // fell into the void + break 'pieces; + } + } + } + } else { + // stuck + break 'fall; + } + } + + points.insert(pos); + pieces += 1; + + if pos == start { + // all filled up + break 'pieces; + } + } + + pieces +} + +fn main() { + let lines = stdin().lines().flat_map(|line| { + parse_points(&line.unwrap()) + .unwrap() + .1 + .windows(2) + .map(|x| Line { + start: x[0], + end: x[1], + }) + .collect::>() + }); + let points: BTreeSet = lines.flat_map(|line| line.points().unwrap()).collect(); + + println!("{}", simulate(points.clone(), None)); + println!("{}", simulate(points, Some(2))); +} diff --git a/Cargo.lock b/Cargo.lock index 656a1d1..cd7d67c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -593,6 +593,14 @@ dependencies = [ "nom", ] +[[package]] +name = "rust_2022_14" +version = "0.1.0" +dependencies = [ + "aoc", + "nom", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index 6c93ccb..7132ee8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,4 +23,5 @@ members = [ "2022/day11/rust", "2022/day12/rust", "2022/day13/rust", + "2022/day14/rust", ]