parent
31efbe60ba
commit
112ee4df01
@ -0,0 +1,14 @@
|
||||
# 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"
|
@ -0,0 +1,9 @@
|
||||
[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" }
|
@ -1,117 +0,0 @@
|
||||
# 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}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,12 +0,0 @@
|
||||
#!/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 "};";
|
@ -1,62 +0,0 @@
|
||||
|
||||
#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;
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
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(())
|
||||
}
|
Loading…
Reference in new issue