Compare commits

..

No commits in common. 'master' and 'cpp-attempt' have entirely different histories.

6
.gitignore vendored

@ -2,9 +2,3 @@
**/obj/ **/obj/
*.o *.o
*-debug *-debug
**/input
**/target/
perf.*
flamegraph.svg
*.png

14
day1/Cargo.lock generated

@ -1,14 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "0.1.0"
dependencies = [
"linebuffer",
]
[[package]]
name = "linebuffer"
version = "0.1.0"

@ -1,9 +0,0 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
linebuffer = { path = "/home/avril/work/linebuffer" }

@ -0,0 +1,117 @@
# Generic C and C++ Makefile project template
# Contains targets for `release', `debug', and `clean'.
PROJECT=day1
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
VERSION=0.0.0
SRC_C = $(wildcard src/*.c)
SRC_CXX = $(wildcard src/*.cpp)
INCLUDE=include
# Link to these libraries dynamicalls
SHARED_LIBS=
# Link to these libraries statically
STATIC_LIBS=
override __VERSION_SPLIT:= $(subst ., ,$(VERSION))
override __VERSION_REVISION:=$(word 3,$(__VERSION_SPLIT)) 0
override __VERSION_SPLIT:= MAJOR:$(word 1,$(__VERSION_SPLIT)) MINOR:$(word 2,$(__VERSION_SPLIT)) BUGFIX:$(word 1,$(subst r, ,$(__VERSION_REVISION))) REVISION:$(word 2,$(subst r, ,$(__VERSION_REVISION))) REVISION_STRING:$(word 3,$(__VERSION_SPLIT))
COMMON_FLAGS+= -W -Wall -Wstrict-aliasing -fno-strict-aliasing $(addprefix -I,$(INCLUDE))
COMMON_FLAGS+= $(addprefix -D_VERSION_,$(subst :,=,$(__VERSION_SPLIT))) '-D_VERSION="$(VERSION)"'
# Target arch. Set to blank for generic
ARCH?=native
# Enable OpenMP and loop parallelisation? (dyn-links to openmp)
PARALLEL?=yes
OPT_FLAGS?= -fgraphite \
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \
-fno-stack-check
ifneq ($(ARCH),)
OPT_FLAGS+= $(addprefix -march=,$(ARCH))
endif
ifeq ($(PARALLEL),yes)
OPT_FLAGS+= -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4
endif
CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors
CSTD?=gnu2x
CXXSTD?=gnu++23
CFLAGS += $(COMMON_FLAGS) --std=$(CSTD)
CXXFLAGS += $(COMMON_FLAGS) --std=$(CXXSTD)
LDFLAGS += $(addsuffix .a,$(addprefix -l:lib,$(STATIC_LIBS))) $(addprefix -l,$(SHARED_LIBS))
STRIP=strip
RELEASE_COMMON_FLAGS+= -fno-bounds-check
DEBUG_COMMON_FLAGS+= -ggdb -gz -fanalyzer -ftrapv -fbounds-check
ifneq ($(TARGET_SPEC_FLAGS),no)
RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS)
RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS)
RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto
DEBUG_CFLAGS?= -Og
DEBUG_CXXFLAGS?= -Og
DEBUG_LDFLAGS?=
endif
DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS)
DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) -fasynchronous-unwind-tables
RELEASE_CFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS)
RELEASE_CXXFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS)
# Objects
OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o))
OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o))
OBJ = $(OBJ_C) $(OBJ_CXX)
# Phonies
.PHONY: release
release: | dirs $(PROJECT)-release
.PHONY: debug
debug: | dirs $(PROJECT)-debug
# Targets
dirs:
@mkdir -p obj/c{,xx}/src
obj/c/%.o: %.c
$(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS)
obj/cxx/%.o: %.cpp
$(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS)
$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS)
$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS)
$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS)
$(PROJECT)-release: $(OBJ)
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
$(STRIP) $@
$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS)
$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS)
$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS)
$(PROJECT)-debug: $(OBJ)
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
clean-rebuild:
rm -rf obj
clean: clean-rebuild
rm -f $(PROJECT)-{release,debug,pgo}

@ -1,2 +0,0 @@
# Rework a more efficient one usinc `memchr2()`
We can `memchr2('\n','\n', mapped_file_memory)` on a `mmap()`'d input file to find the grouping seperators, and process each group in parallel

File diff suppressed because it is too large Load Diff

@ -0,0 +1,12 @@
#!/bin/bash
echo "constexpr size_t INPUT[] = {"
while IFS= read -r line; do
if [[ -z "$line" ]]; then
echo " 0,"
else
echo " ${line},"
fi
done
echo " 0,";
echo "};";

@ -0,0 +1,62 @@
#include <string>
#include <vector>
#include <optional>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cstdint>
#include <input.h>
class [[gnu::visibility("hidden")]] Elf {
std::vector<size_t> _lines;
public:
Elf() = default;
Elf(Elf const&) = default;
Elf(Elf&&) = default;
Elf& operator=(Elf&&) = default;
Elf& operator=(Elf const&) = default;
~Elf() = default;
using ptr_t = decltype(INPUT)*;
template<size_t N>
static std::unique_ptr<Elf> read_from_input(const ptr_t (&input)[N])
{
Elf elf;
size_t n=0;
for(;n = **input;(*input)++) elf._lines.push_back(n);
if(n)
return std::make_unique<Elf>(std::move(elf));
else
return nullptr;
}
size_t sum() const noexcept
{
size_t s =0;
for(size_t sz : _lines) s+=sz;
return s;
}
};
static std::unique_ptr<Elf> read_elf()
{
constinit static const auto input = INPUT;
return Elf::read_from_input(&input);
}
int main() {
size_t max;
std::unique_ptr<Elf> e;
while(e = read_elf()) {
auto sum = e->sum();
if(sum>max) max = sum;
}
printf("%lu\n", max);
return 0;
}

@ -1,50 +0,0 @@
use linebuffer::{
buf::forward,
ParsedLines,
};
use std::{
io,
str,
fs,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Copy)]
struct MaybeU64(Option<u64>);
impl str::FromStr for MaybeU64
{
type Err = <u64 as str::FromStr>::Err;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
Ok(Self(None))
} else {
Ok(Self(Some(s.parse()?)))
}
}
}
fn input_reader<R: io::Read>(input: R) -> ParsedLines<R, forward::FromStr<MaybeU64>>
{
ParsedLines::new(input)
}
fn main() -> Result<(), Box<dyn std::error::Error+ 'static>> {
let mut reader = input_reader(fs::OpenOptions::new().read(true).open("input")?);
let mut last_elf = 0;
let mut max_elf = 0;
while let Some(res) = reader.try_next()
{
match res.expect("Failed to parse line").0 {
Some(value) => last_elf += value,
None if last_elf > max_elf => {
max_elf = std::mem::replace(&mut last_elf, 0);
},
_ => last_elf = 0,
}
}
println!("{}", std::cmp::max(max_elf, last_elf));
Ok(())
}

312
day14/Cargo.lock generated

@ -1,312 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76fd60b23679b7d19bd066031410fb7e458ccc5e958eb5c325888ce4baedc97"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [
"memchr",
]
[[package]]
name = "arrayvec"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "backtrace"
version = "0.3.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "233d376d6d185f2a3093e58f283f60f880315b6c60075b01f36b3b85154564ca"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "cc"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color-eyre"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
dependencies = [
"backtrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
]
[[package]]
name = "day14"
version = "0.1.0"
dependencies = [
"color-eyre",
"jemallocator",
"linebuffer",
"log",
"pretty_env_logger",
]
[[package]]
name = "env_logger"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "eyre"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "fs_extra"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]]
name = "gimli"
version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dec7af912d60cdbd3677c1af9352ebae6fb8394d165568a2234df0fa00f87793"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "jemalloc-sys"
version = "0.5.2+5.3.0-patched"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "134163979b6eed9564c98637b710b40979939ba351f59952708234ea11b5f3f8"
dependencies = [
"cc",
"fs_extra",
"libc",
]
[[package]]
name = "jemallocator"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16c2514137880c52b0b4822b563fadd38257c1f380858addb74a400889696ea6"
dependencies = [
"jemalloc-sys",
"libc",
]
[[package]]
name = "libc"
version = "0.2.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
[[package]]
name = "linebuffer"
version = "0.1.1"
dependencies = [
"arrayvec",
]
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "miniz_oxide"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b275950c28b37e794e8c55d88aeb5e139d0ce23fdbbeda68f8d7174abdf9e8fa"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.30.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "239da7f290cfa979f43f85a8efeee9a8a76d0827c356d37f9d3d7254d6b537fb"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "pretty_env_logger"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
dependencies = [
"env_logger",
"log",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "regex"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

@ -1,29 +0,0 @@
[package]
name = "day14"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
strip=true
[profile.symbols]
inherits="release"
#incremental=true
strip=false
[features]
default= ["jemallocator"]
status = ["jemallocator/profiling", "pretty_env_logger"]
[dependencies]
color-eyre = { version = "0.6.2", default-features = false }
jemallocator = { version = "0.5.0", optional = true }
linebuffer = { path = "/home/avril/work/linebuffer" }
log = "0.4.17"
pretty_env_logger = { version = "0.4.0", optional = true }

@ -1,180 +0,0 @@
//! Grid of lines and sands
use super::*;
use std::{
collections::BTreeMap,
borrow::Cow,
fmt,
};
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Copy)]
#[repr(u8)]
pub enum Cell
{
#[default]
Air = b'.',
Wall = b'#',
Settled = b'O',
Falling = b'~',
Source = b'+',
}
impl From<Cell> for u8
{
#[inline(always)]
fn from(from: Cell) -> Self
{
from as u8
}
}
impl fmt::Display for Cell
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{}", u8::from(*self) as char)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Grid
{
map: BTreeMap<Point, Cell>,
}
impl Grid
{
#[inline]
pub const fn new() -> Self
{
Self {
map: BTreeMap::new(),
}
}
#[inline(always)]
pub fn insert_single(&mut self, point: Point, cell: Cell) -> Option<Cell>
{
self.map.insert(point, cell)
}
#[inline]
pub fn draw_line_with<F>(&mut self, line: Line, mut with: F)
where F: for<'a> FnMut(&'a Point) -> Cell
{
for point in line.unroll() {
let cell = with(&point);
self.map.insert(point, cell);
}
}
#[inline]
pub fn draw_line(&mut self, line: Line, cell: Cell)
{
self.draw_line_with(line, move |_| cell)
}
#[inline]
pub fn draw_lines_with<I,F>(&mut self, line: I, mut with: F)
where F: for<'a> FnMut(&'a Point) -> Cell,
I: IntoIterator<Item = Line>
{
for point in line.into_iter().map(|x| x.normalised().unroll()).flatten() {
let cell = with(&point);
self.map.insert(point, cell);
}
}
/// Draw these lines with this cell
#[inline]
pub fn draw_lines(&mut self, lines: impl IntoIterator<Item = Line>, cell: Cell)
{
self.draw_lines_with(lines, move |_| cell)
}
/// Add the source to the grid
#[inline(always)]
pub fn draw_source(&mut self, source: Point)
{
self.map.insert(source, Cell::Source);
}
}
/// Create a new sparse grid, returns the point.
///
/// A sparse grid does not draw air, only the specified `walls`.
#[inline]
pub fn sparse_grid(walls: Lines) -> (grid::Grid, Point)
{
let mut grid = grid::Grid::new();
// Add source
grid.draw_source(ORIGIN_POINT);
// Draw walls
grid.draw_lines(walls, Cell::Wall);
(grid, ORIGIN_POINT)
}
// Non-drawing grid functions
impl Grid
{
/// Number of points in the grid
#[inline]
pub fn len(&self) -> usize
{
self.map.len()
}
/// Find the highest point in the collection
#[inline(always)]
pub fn find_high_bound(&self) -> Point
{
let x = self.map.keys().max_by(|a, b| u64::cmp(&a.x, &b.x)).map(|x| x.x).unwrap_or(0);
let y = self.map.keys().max_by(|a, b| u64::cmp(&a.y, &b.y)).map(|y| y.y).unwrap_or(0);
Point{ x, y }
}
/// Find the lowest point in the collection
#[inline(always)]
pub fn find_low_bound(&self) -> Point
{
let x = self.map.keys().min_by(|a, b| u64::cmp(&a.x, &b.x)).map(|x| x.x).unwrap_or(0);
let y = self.map.keys().min_by(|a, b| u64::cmp(&a.y, &b.y)).map(|y| y.y).unwrap_or(0);
Point{ x, y }
}
/// Find the low and high bound as a flattened line
#[inline]
pub fn find_bound(&self) -> Line
{
let start = self.find_low_bound();
let end = self.find_high_bound();
Line{ start, end }
}
/// Iterate over a sparse grid, bounded by `bound`, any not set cells will be drawn with air.
#[inline]
pub fn iterate_sparse<'g>(&'g self, bound: Line) -> impl Iterator<Item = &'g Cell> + 'g
{
const AIR: Cell = Cell::Air;
//let mut grid = start.map.iter().fuse().peekable();
bound.normalised().unroll().into_iter()
.map(move |point| {
if let Some(cell) = self.map.get(&point) {
// Contains the point, return that reference
return cell;
}
/*
if let Some(&(next_wall, cell)) = grid.peek() {
if next_wall == &point {
// Take this point off the iterator if it matches
return grid.next().unwrap().1;
}
}*/
&AIR
})
}
}

@ -1,359 +0,0 @@
#![feature(repr_simd)]
#[macro_use] extern crate log;
use std::{
str,
io,
iter,
path::Path,
fmt,
};
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;
/// Base coordinate
pub type Coord = u64;
/// A point is a vector
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)]
#[repr(simd)]
pub struct Point {
pub x: Coord,
pub y: Coord,
}
impl fmt::Display for Point
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{},{}", self.x, self.y)
}
}
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)]
pub struct Line {
start: Point,
end: Point,
}
impl fmt::Display for Line
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!(f, "{} -> {}", self.start, self.end)
}
}
impl From<Point> for (Coord, Coord)
{
#[inline]
fn from(from: Point) -> Self
{
(from.x, from.y)
}
}
impl From<(Coord, Coord)> for Point
{
#[inline(always)]
fn from((x,y): (Coord, Coord)) -> Self
{
Self {x,y}
}
}
impl Line {
fn normalised(self) -> Self
{
let x = std::cmp::min(self.start.x, self.end.x);
let y = std::cmp::min(self.start.y, self.end.y);
let start = Point {x,y};
let x = std::cmp::max(self.start.x, self.end.x);
let y = std::cmp::max(self.start.y, self.end.y);
let end = Point {x,y};
Self {
start, end
}
}
#[inline]
fn normalise(&mut self)
{
*self = self.clone().normalised();
}
#[inline]
pub fn over_horizontal(&self) -> impl IntoIterator<Item = Point> + 'static
{
let x = self.start.x;
let y = self.start.y;
(self.start.x..=self.end.x).map(move |x| (x, y).into())
}
#[inline]
pub fn over_vertical(&self) -> impl IntoIterator<Item = Point> + 'static
{
let x = self.start.x;
(self.start.y..=self.end.y).map(move |y| (x, y).into())
}
#[inline]
pub fn unroll(&self) -> impl IntoIterator<Item = Point> + 'static
{
self.over_horizontal().into_iter()
.chain(self.over_vertical().into_iter())
}
}
/// A single parsed line into `Line`s.
#[derive(Debug, Clone, Default)]
pub 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 FromIterator<Lines> for Lines
{
#[inline]
fn from_iter<I: IntoIterator<Item=Lines>>(lines: I) -> Self
{
Self {
lines: Self::flattening(lines).into_iter().collect(),
}
}
}
impl FromIterator<Line> for Lines
{
#[inline]
fn from_iter<I: IntoIterator<Item=Line>>(lines: I) -> Self
{
Self {
lines: lines.into_iter().collect()
}
}
}
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, T: 'a>(lines: impl IntoIterator<Item = T> + 'a) -> impl IntoIterator<Item = Line> + 'a
where T: IntoIterator<Item = Line>
{
lines.into_iter().map(|x| x.into_iter()).flatten()
}
#[inline]
pub fn flatten(lines: impl IntoIterator<Item = Self>) -> Self
{
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 draw_grid<W: io::Write>(mut to: W, bound: Line, grid: &grid::Grid) -> io::Result<()>
{
for cells in grid.iterate_sparse(bound).chunk((bound.end.x - bound.start.x) as usize)
{
for cell in cells {
write!(&mut to, "{}", cell)?;
}
write!(&mut to, "\n")?;
}
Ok(())
}
/// Simulate the grid falling from `origin` in `grid`.
fn simulate(grid: &mut grid::Grid, origin: &Point) -> eyre::Result<()>
{
Ok(())
}
fn main() -> eyre::Result<()> {
init().wrap_err("Failed to install handlers and hooks")?;
// Parsed input
let input = {
// Input file
let input = load_input_from(INPUT_FILENAME)
.wrap_err("Failed to open input file")?;
// Parse 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:#?}```");
}
//Draw lines into grid before starting simulation of sand from `origin`.
let (mut grid, origin) = grid::sparse_grid(input.all);
debug!("Read {} unzipped lines", grid.len());
//TODO: Simulate falling, adding and counting each stopped particle until the sand drops below the bottom of the lowest vertical line.
simulate(&mut grid, &origin)
.wrap_err("Failed to simulate grid")?;
// Draw grid
if cfg!(feature="status")
{
let bound = grid.find_bound();
debug!("Current map (bound: {}):", bound);
draw_grid(io::stderr().lock(), bound, &grid).wrap_err("Failed to draw grid to stderr")?;
}
Ok(())
}

181
day3/Cargo.lock generated

@ -1,181 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "backtrace"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
dependencies = [
"addr2line",
"cc",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "cc"
version = "1.0.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "color-eyre"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204"
dependencies = [
"backtrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
]
[[package]]
name = "day3"
version = "0.1.0"
dependencies = [
"color-eyre",
"lazy_static",
"linebuffer",
"smallmap",
]
[[package]]
name = "eyre"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "gimli"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d"
[[package]]
name = "indenter"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.138"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8"
[[package]]
name = "linebuffer"
version = "0.1.0"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "miniz_oxide"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34"
dependencies = [
"adler",
]
[[package]]
name = "object"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]]
name = "owo-colors"
version = "3.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
[[package]]
name = "rustc-demangle"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
[[package]]
name = "rustc_version"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "smallmap"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2df0ae8eb5af9e6e00be6ba531cc6860ba93d5226d6d1d4f80ead510a5d6296e"
dependencies = [
"rustc_version",
]

@ -1,23 +0,0 @@
[package]
name = "day3"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
opt-level = 3
lto = "fat"
codegen-units = 1
strip = true
[profile.prof]
inherits="release"
strip = false
[dependencies]
color-eyre = { version = "0.6.2", default-features = false }
lazy_static = "1.4.0"
linebuffer = { path = "/home/avril/work/linebuffer" }
smallmap = "1.4.0"

@ -1,32 +0,0 @@
//! Extensions
use super::*;
use std::{
iter::{self, Fuse,}
};
pub trait TaekTwoExt: Iterator
{
fn take_two(self) -> TakeTwo<Self>;
}
#[derive(Debug, Clone)]
pub struct TakeTwo<I: ?Sized + Iterator>(I);
impl<T, I: Iterator<Item=T> + ?Sized> Iterator for TakeTwo<I>
{
type Item = (T, Option<T>);
#[inline]
fn next(&mut self) -> Option<Self::Item>
{
let first = self.0.next()?;
Some((first, self.0.next()))
}
}
impl<I: Iterator> TaekTwoExt for I
where I: Iterator + Sized
{
#[inline]
fn take_two(self) -> TakeTwo<Self> {
TakeTwo(self)
}
}

@ -1,117 +0,0 @@
#[macro_use] extern crate lazy_static;
use smallmap::{
Map,
smallmap,
};
use std::{
collections::BTreeSet,
};
use linebuffer::{
buf::forward,
TryFromBuf,
FromBuf,
ParsedLines,
};
use color_eyre::{
eyre::{
self, eyre,
WrapErr as _,
},
Help as _, SectionExt as _,
};
mod ext;
pub use ext::*;
fn init() -> eyre::Result<()>
{
color_eyre::install()
}
#[derive(Debug, Clone, PartialEq, Eq)] //TODO: implement PartialEq, Eq, PartialOrd, Ord via intersection between `full`s
struct Sack {
split_by: usize,
containers: Vec<BTreeSet<char>>,
full: smallmap::Set<char>, //TODO: When doing the above ^, change to BTreeSet for `intersection()`
}
impl Sack
{
/// Compute the intersection of each `N` part of the container
pub fn intersection(&self) -> impl Iterator<Item = char> + '_
{
self.containers.iter().take_two().flat_map(|(a, b)| b.map(move |b| (a, b)))
.map(|(a, b)| a.intersection(b)).flatten().copied()
}
}
#[inline]
fn read_input_from<R: std::io::Read>(input: R) -> ParsedLines<R, forward::FromStr<Sack>>
{
ParsedLines::new(input)
}
fn read_input() -> eyre::Result<ParsedLines<impl std::io::Read + 'static, forward::FromStr<Sack>>>
{
let reader = std::fs::OpenOptions::new()
.read(true)
.open("input")
.wrap_err("Failed to open input file for reading")?;
Ok(read_input_from(reader))
}
#[derive(Debug)]
pub struct SackParseError{
expected_div: usize,
size: usize,
}
impl std::str::FromStr for Sack
{
type Err = SackParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let (first, second) = s.split_at(s.len()/2);
Ok(Self {
split_by: 2,
containers: vec![
first.chars().collect(),
second.chars().collect(),
],
full: s.chars().map(|a| (a, ())).collect(),
})
}
}
#[inline]
fn prop_swivle<'a>(sigh: impl IntoIterator<Item = char>+ 'a) -> impl Iterator<Item = usize> + 'a
{
lazy_static! {
static ref PMAP: Map<char, usize> = ('a'..='z').enumerate().map(|(i, n)| (i+1, n))
.chain (('A'..='Z').enumerate().map(|(i, n)| (i + 27, n)))
.map(|(i, n)| (n, i))
.collect();
}
sigh.into_iter().map(|ref ch| PMAP.get(ch)).flat_map(std::convert::identity).copied()
}
fn main() -> eyre::Result<()> {
init().wrap_err("Panic hook failed to install")?;
let mut input = read_input()?;
let mut psum = 0;
while let Some(sack) = input.try_next()
{
let sack = sack.expect("Failed to parse input line");
psum += prop_swivle(sack.intersection())
.sum::<usize>();
// TODO: part2: find sack.full's intersection through *all other* lines, up to two where the intersection is exactly 1 between all three. This is boring, I give up.
}
println!("{psum}");
Ok(())
}
Loading…
Cancel
Save