You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
219 lines
4.7 KiB
219 lines
4.7 KiB
#![feature(repr_simd)]
|
|
|
|
#[macro_use] extern crate log;
|
|
|
|
use std::{
|
|
str,
|
|
io,
|
|
iter,
|
|
path::Path,
|
|
};
|
|
|
|
use color_eyre::{
|
|
eyre::{
|
|
self, eyre,
|
|
WrapErr as _,
|
|
},
|
|
Help as _, SectionExt as _,
|
|
};
|
|
|
|
use linebuffer::prelude::*;
|
|
|
|
#[cfg(feature="jemallocator")]
|
|
const _:() = {
|
|
use jemallocator::Jemalloc;
|
|
|
|
#[global_allocator]
|
|
static GLOBAL: Jemalloc = Jemalloc;
|
|
};
|
|
|
|
mod grid;
|
|
|
|
/// A point is a vector
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
|
|
#[repr(simd)]
|
|
struct Point {
|
|
pub x: u64,
|
|
pub y: u64,
|
|
}
|
|
|
|
impl str::FromStr for Point
|
|
{
|
|
type Err = <u64 as str::FromStr>::Err;
|
|
#[inline]
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let (x, y) = s.split_once(',').unwrap_or((s, "0"));
|
|
Ok(Self {
|
|
x: x.parse()?,
|
|
y: y.parse()?,
|
|
})
|
|
}
|
|
}
|
|
|
|
/// A `Line` is potentially two X any Ys
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
|
|
struct Line {
|
|
start: Point,
|
|
end: Point,
|
|
}
|
|
|
|
/// A single parsed line into `Line`s.
|
|
#[derive(Debug, Clone, Default)]
|
|
struct Lines {
|
|
lines: Vec<Line>,
|
|
}
|
|
|
|
impl IntoIterator for Lines
|
|
{
|
|
type Item= Line;
|
|
type IntoIter = std::vec::IntoIter<Line>;
|
|
|
|
#[inline]
|
|
fn into_iter(self) -> Self::IntoIter
|
|
{
|
|
self.lines.into_iter()
|
|
}
|
|
}
|
|
|
|
|
|
impl Lines {
|
|
#[inline]
|
|
pub fn concat(mut self, next: impl IntoIterator<Item = Line>) -> Self
|
|
{
|
|
self.lines.extend(next);
|
|
self
|
|
}
|
|
#[inline(always)]
|
|
pub fn flattening<'a>(lines: impl IntoIterator<Item = Self> + 'a) -> impl IntoIterator<Item = Line> + 'a
|
|
{
|
|
lines.into_iter().map(|x| x.lines.into_iter()).flatten()
|
|
}
|
|
#[inline]
|
|
pub fn flatten(lines: impl IntoIterator<Item = Self>) -> Self
|
|
{
|
|
Self {
|
|
lines: Self::flattening(lines).into_iter().collect(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl str::FromStr for Lines
|
|
{
|
|
type Err = <Point as str::FromStr>::Err;
|
|
#[inline]
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
let mut next = s.split(" -> ").peekable();
|
|
let mut lines = Vec::new();
|
|
while let Some(start) = next.next()
|
|
{
|
|
let Some(second) = next.peek() else { continue };
|
|
lines.push(Line {
|
|
start: start.parse()?,
|
|
end: second.parse()?
|
|
})
|
|
}
|
|
Ok(Self{lines})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
struct Input {
|
|
all: Lines,
|
|
}
|
|
|
|
impl Input
|
|
{
|
|
/// Size of all non-unzipped (H+V) lines
|
|
#[inline]
|
|
pub fn zipped_len(&self) -> usize
|
|
{
|
|
self.all.lines.len()
|
|
}
|
|
}
|
|
|
|
impl FromIterator<Lines> for Input
|
|
{
|
|
#[inline]
|
|
fn from_iter<I: IntoIterator<Item=Lines>>(iter: I) -> Self
|
|
{
|
|
Self{ all: Lines::flatten(iter.into_iter()) }
|
|
}
|
|
}
|
|
|
|
|
|
impl Input {
|
|
#[inline(always)]
|
|
pub fn parse<R: io::Read>(reader: R) -> eyre::Result<Self>
|
|
{
|
|
Self::from_parser(ParsedLines::new(reader))
|
|
}
|
|
#[inline]
|
|
pub fn from_parser<R: io::Read>(lines: ParsedLines<R, forward::FromStr<Lines>>) -> eyre::Result<Self>
|
|
{
|
|
struct ParsedIter<T: io::Read>(ParsedLines<T, forward::FromStr<Lines>>);
|
|
impl<T: io::Read> Iterator for ParsedIter<T>
|
|
{
|
|
type Item = Result<Lines, linebuffer::lines::LineParseError<std::num::ParseIntError>>;
|
|
#[inline(always)]
|
|
fn next(&mut self) -> Option<Self::Item>
|
|
{
|
|
self.0.try_next()
|
|
}
|
|
}
|
|
impl<T: io::Read> iter::FusedIterator for ParsedIter<T>
|
|
where ParsedLines<T, forward::FromStr<Lines>>: iter::FusedIterator{}
|
|
|
|
ParsedIter(lines).enumerate().map(|(i,x)| x.wrap_err("Failed to parse single line").with_section(move || i.header("Line number is"))).collect()
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
fn load_input_from(from: impl AsRef<Path>) -> eyre::Result<impl io::Read + 'static>
|
|
{
|
|
std::fs::OpenOptions::new()
|
|
.read(true)
|
|
.open(&from)
|
|
.with_section(|| format!("{:?}", from.as_ref()).header("File name was"))
|
|
}
|
|
|
|
/// Input filename
|
|
const INPUT_FILENAME: &'static str = "input";
|
|
/// Starting point of sand falling
|
|
const ORIGIN_POINT: Point = Point { x: 500, y: 0 };
|
|
|
|
fn init() -> eyre::Result<()>
|
|
{
|
|
color_eyre::install()?;
|
|
#[cfg(feature="status")]
|
|
pretty_env_logger::init();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn main() -> eyre::Result<()> {
|
|
init().wrap_err("Failed to install handlers and hooks")?;
|
|
|
|
// Input
|
|
let input = load_input_from(INPUT_FILENAME)
|
|
.wrap_err("Failed to open input file")?;
|
|
|
|
// Parse input
|
|
let input = Input::parse(input)
|
|
.wrap_err("Failed to parse input")?;
|
|
|
|
if cfg!(feature="status") {
|
|
debug!("Read {} zipped lines from input", input.zipped_len());
|
|
|
|
trace!("Input is:\n```{input:#?}```");
|
|
}
|
|
|
|
todo!("Deconstruction, draw, and simulation from `ORIGIN_POINT`");
|
|
//TODO: Deconstruct each `Line` into the horizontal and vertical movements
|
|
|
|
//TODO: Draw lines into grid before starting simulation of sand from `ORIGIN_POINT`.
|
|
|
|
//TODO: Simulate falling, adding and counting each stopped particle until the sand drops below the bottom of the lowest vertical line.
|
|
|
|
Ok(())
|
|
}
|