2022 day14/rust: add solution

This commit is contained in:
Xiretza 2022-12-14 19:20:45 +01:00
parent 7db15c9cf7
commit f41c0177d8
4 changed files with 102 additions and 0 deletions

View file

@ -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"

View file

@ -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<Vec2>, ()> {
separated_list1(
tag(" -> "),
separated_pair(i32, tag(","), i32).map(Vec2::from),
)(s)
}
fn simulate(structures: BTreeSet<Vec2>, floor: Option<usize>) -> 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::<Vec<_>>()
});
let points: BTreeSet<Vec2> = lines.flat_map(|line| line.points().unwrap()).collect();
println!("{}", simulate(points.clone(), None));
println!("{}", simulate(points, Some(2)));
}

8
Cargo.lock generated
View file

@ -593,6 +593,14 @@ dependencies = [
"nom", "nom",
] ]
[[package]]
name = "rust_2022_14"
version = "0.1.0"
dependencies = [
"aoc",
"nom",
]
[[package]] [[package]]
name = "ryu" name = "ryu"
version = "1.0.11" version = "1.0.11"

View file

@ -23,4 +23,5 @@ members = [
"2022/day11/rust", "2022/day11/rust",
"2022/day12/rust", "2022/day12/rust",
"2022/day13/rust", "2022/day13/rust",
"2022/day14/rust",
] ]