From b9cc49459bafdff848f45bc953b8e39454dd9b1e Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Sun, 6 Oct 2024 21:15:16 +0000 Subject: [PATCH] Piss poor attempt at AoC 2021 and I'm 3 years late publishing it :/ --- 2021/Makefile | 17 +++++++++ 2021/days/day1.cpp | 54 +++++++++++++++++++++++++++++ 2021/misc/day.cpp.template | 23 +++++++++++++ 2021/misc/main.cpp | 36 ++++++++++++++++++++ 2021/misc/main_test.cpp | 66 +++++++++++++++++++++++++++++++++++ 2021/misc/print.hpp | 31 +++++++++++++++++ 2021/misc/types.cpp | 47 +++++++++++++++++++++++++ 2021/misc/types.hpp | 70 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 344 insertions(+) create mode 100644 2021/Makefile create mode 100644 2021/days/day1.cpp create mode 100644 2021/misc/day.cpp.template create mode 100644 2021/misc/main.cpp create mode 100644 2021/misc/main_test.cpp create mode 100644 2021/misc/print.hpp create mode 100644 2021/misc/types.cpp create mode 100644 2021/misc/types.hpp diff --git a/2021/Makefile b/2021/Makefile new file mode 100644 index 0000000..2978a55 --- /dev/null +++ b/2021/Makefile @@ -0,0 +1,17 @@ +day%.out: out/types.o misc/main.cpp days/day%.cpp + g++ -O3 -std=c++2a $(flags) -Dcurrent_day=$(@:.out=) days/$(@:out=cpp) misc/main.cpp out/types.o -o $@ + +day%_test.out: out/types.o misc/main_test.cpp days/day%.cpp + g++ -O3 -std=c++2a $(flags) -DTEST_BUILD -Dcurrent_day=$(@:_test.out=) days/$(@:_test.out=.cpp) misc/main_test.cpp out/types.o -o $@ + +days/day%.cpp: + cp 'misc/day.cpp.template' $@ + sed -i -e "s/current_day/$(shell basename $@ | cut -f 1 -d '.')/g" $@ + +out/types.o: misc/types.hpp misc/types.cpp + mkdir -p out/ + g++ -std=c++2a $(flags) -c misc/types.cpp -o $@ + +.PHONY: clean +clean: + rm -f out/* day*.out day*.output diff --git a/2021/days/day1.cpp b/2021/days/day1.cpp new file mode 100644 index 0000000..4f8c65c --- /dev/null +++ b/2021/days/day1.cpp @@ -0,0 +1,54 @@ +/***************************** + * + * Copyright (c) 2021 Gnarwhal + * + *****************************/ + +#include +#include +#include +#include + +#include "../misc/types.hpp" +#include "../misc/print.hpp" + +auto current_day() -> void { + { + auto line = std::string(); + auto file = std::ifstream("inputs/day1.input"); + getline(file, line); + auto value = stoi(line); + auto increases = 0; + while (getline(file, line)) { + auto new_value = stoi(line); + increases += new_value > value; + value = new_value; + } + + std::cout << increases << std::endl; + } + + { + auto line = std::string(); + auto file = std::ifstream("inputs/day1.input"); + getline(file, line); + auto index = 2; + auto values = std::vector{ 0, 0 }; + while (getline(file, line)) { + auto new_value = stoi(line); + values.push_back(0); + values[index - 2] += new_value; + values[index - 1] += new_value; + values[index ] += new_value; + ++index; + } + + auto increases = 0; + for (auto i = 3; i < values.size(); ++i) { + increases += values[i - 1] < values[i]; + } + + std::cout << increases << std::endl; + } +} + diff --git a/2021/misc/day.cpp.template b/2021/misc/day.cpp.template new file mode 100644 index 0000000..c2f20b4 --- /dev/null +++ b/2021/misc/day.cpp.template @@ -0,0 +1,23 @@ +/***************************** + * + * Copyright (c) 2020 Gnarwhal + * + *****************************/ + +#include +#include +#include +#include + +#include "../misc/types.hpp" +#include "../misc/print.hpp" + +auto current_day() -> void { + { + auto line = std::string(); + auto file = std::ifstream("inputs/current_day.input"); + while (getline(file, line)) { + + } + } +} diff --git a/2021/misc/main.cpp b/2021/misc/main.cpp new file mode 100644 index 0000000..c4752db --- /dev/null +++ b/2021/misc/main.cpp @@ -0,0 +1,36 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#include "types.hpp" + +extern auto current_day() -> void; + +auto main(i32 argc, char * argv[]) -> i32 { + current_day(); + + return 0; +} + diff --git a/2021/misc/main_test.cpp b/2021/misc/main_test.cpp new file mode 100644 index 0000000..0d8954d --- /dev/null +++ b/2021/misc/main_test.cpp @@ -0,0 +1,66 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#include +#include +#include + +#include "types.hpp" + +extern auto current_day() -> void; + +auto main(i32 argc, char * argv[]) -> i32 { + auto TEST_CYCLES = 1; + if (argc >= 2) { + TEST_CYCLES = std::stoi(argv[1]); + } + + std::cout << "Starting test with " << TEST_CYCLES << " iterations..." << std::endl; + auto begin1 = std::chrono::high_resolution_clock::now(); + + for (auto i = usize(0); i < TEST_CYCLES; ++i) { + current_day(); + } + + auto end1 = std::chrono::high_resolution_clock::now(); + + if (argc >= 3) { + if (argv[2] == std::string("millis")) { + std::cout << "Tests completed in " << std::chrono::duration_cast(end1 - begin1).count() << " milliseconds"<< std::endl; + } else if (argv[2] == std::string("nanos")) { + std::cout << "Tests completed in " << std::chrono::duration_cast(end1 - begin1).count() << " nanoseconds"<< std::endl; + } else if (argv[2] == std::string("micros")) { + std::cout << "Tests completed in " << std::chrono::duration_cast(end1 - begin1).count() << " microsenconds"<< std::endl; + } else { + std::cout << "Unkown time scale '" << argv[2] << "'" << std::endl; + } + } else { + std::cout << "Tests completed in " << std::chrono::duration_cast(end1 - begin1).count() << " microseconds"<< std::endl; + } + + return 0; +} + diff --git a/2021/misc/print.hpp b/2021/misc/print.hpp new file mode 100644 index 0000000..5960cd8 --- /dev/null +++ b/2021/misc/print.hpp @@ -0,0 +1,31 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#ifndef TEST_BUILD +#define print(x) std::cout << (x) << std::endl +#else +#define print(x) +#endif diff --git a/2021/misc/types.cpp b/2021/misc/types.cpp new file mode 100644 index 0000000..cc4d3da --- /dev/null +++ b/2021/misc/types.cpp @@ -0,0 +1,47 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#include "types.hpp" + +constexpr auto operator "" _u8 (unsigned long long int num) noexcept -> u8 { return u8(num); } +constexpr auto operator "" _i8 (unsigned long long int num) noexcept -> i8 { return u8(num); } +constexpr auto operator "" _u16 (unsigned long long int num) noexcept -> u16 { return u16(num); } +constexpr auto operator "" _i16 (unsigned long long int num) noexcept -> i16 { return i16(num); } +constexpr auto operator "" _u32 (unsigned long long int num) noexcept -> u32 { return u32(num); } +constexpr auto operator "" _i32 (unsigned long long int num) noexcept -> i32 { return i32(num); } +constexpr auto operator "" _u64 (unsigned long long int num) noexcept -> u64 { return u64(num); } +constexpr auto operator "" _i64 (unsigned long long int num) noexcept -> i64 { return i64(num); } +constexpr auto operator "" _usize(unsigned long long int num) noexcept -> usize { return usize(num); } + +constexpr auto operator "" _c8 (char c) noexcept -> c8 { return c8(c); } +constexpr auto operator "" _c16(char c) noexcept -> c16 { return c16(c); } +constexpr auto operator "" _c32(char c) noexcept -> c32 { return c32(c); } +constexpr auto operator "" _c8 (unsigned long long int c) noexcept -> c8 { return c8(c); } +constexpr auto operator "" _c16(unsigned long long int c) noexcept -> c16 { return c16(c); } +constexpr auto operator "" _c32(unsigned long long int c) noexcept -> c32 { return c32(c); } + +constexpr auto operator "" _f32(long double num) noexcept -> f32 { return (f32) num; } +constexpr auto operator "" _f64(long double num) noexcept -> f64 { return (f64) num; } diff --git a/2021/misc/types.hpp b/2021/misc/types.hpp new file mode 100644 index 0000000..bc94dab --- /dev/null +++ b/2021/misc/types.hpp @@ -0,0 +1,70 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#ifndef GNARWHAL_REDWOOD_TYPES +#define GNARWHAL_REDWOOD_TYPES + +#include +#include + +using u8 = std::uint8_t; +using i8 = std::int8_t; +using u16 = std::uint16_t; +using i16 = std::int16_t; +using u32 = std::uint32_t; +using i32 = std::int32_t; +using u64 = std::uint64_t; +using i64 = std::int64_t; +using usize = std::size_t; + +constexpr auto operator "" _u8 (unsigned long long int num) noexcept -> u8; +constexpr auto operator "" _i8 (unsigned long long int num) noexcept -> i8; +constexpr auto operator "" _u16 (unsigned long long int num) noexcept -> u16; +constexpr auto operator "" _i16 (unsigned long long int num) noexcept -> i16; +constexpr auto operator "" _u32 (unsigned long long int num) noexcept -> u32; +constexpr auto operator "" _i32 (unsigned long long int num) noexcept -> i32; +constexpr auto operator "" _u64 (unsigned long long int num) noexcept -> u64; +constexpr auto operator "" _i64 (unsigned long long int num) noexcept -> i64; +constexpr auto operator "" _usize(unsigned long long int num) noexcept -> usize; + +using c8 = u8; +using c16 = u16; +using c32 = u32; + +constexpr auto operator "" _c8 (char c) noexcept -> c8; +constexpr auto operator "" _c16(char c) noexcept -> c16; +constexpr auto operator "" _c32(char c) noexcept -> c32; +constexpr auto operator "" _c8 (unsigned long long int c) noexcept -> c8; +constexpr auto operator "" _c16(unsigned long long int c) noexcept -> c16; +constexpr auto operator "" _c32(unsigned long long int c) noexcept -> c32; + +using f32 = float; +using f64 = double; + +constexpr auto operator "" _f32(long double num) noexcept -> f32; +constexpr auto operator "" _f64(long double num) noexcept -> f64; + +#endif // GNARWHAL_REDWOOD_TYPES