add 2015 01 and 02

This commit is contained in:
Eric Mertens 2024-06-10 11:08:30 -07:00
parent 4073ac22fc
commit c1dca9065f
6 changed files with 116 additions and 4 deletions

35
2015/01.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <aocpp/Startup.hpp>
#include <cstdint>
#include <stdexcept>
auto Main(std::istream & in, std::ostream & out) -> void
{
std::string line;
std::getline(in, line);
std::int64_t part1 = 0;
std::size_t part2 = 0;
for (std::size_t i = 0; i < line.size(); ++i)
{
switch (line[i]) {
case '(': part1++; break;
case ')': part1--; break;
default: throw std::runtime_error{"Invalid input character"};
}
if (part2 == 0 && part1 == -1)
{
part2 = i + 1;
}
}
if (part2 == 0)
{
throw std::runtime_error{"No part 2 solution"};
}
out << "Part 1: " << part1 << std::endl;
out << "Part 2: " << part2 << std::endl;
}

71
2015/02.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <aocpp/Startup.hpp>
#include <boost/spirit/include/qi.hpp>
#include <doctest.h>
#include <algorithm>
#include <array>
#include <functional>
#include <numeric>
struct Box
{
std::int64_t x;
std::int64_t y;
std::int64_t z;
};
auto Part1(Box const& box) -> std::int64_t
{
std::array<std::int64_t, 3> sides {box.x * box.y, box.x * box.z, box.y * box.z};
std::partial_sort(begin(sides), begin(sides)+1, end(sides));
return 2 * std::reduce(begin(sides), end(sides)) + sides[0];
}
auto Part2(Box const& box) -> std::int64_t
{
std::array<std::int64_t, 3> dims {box.x, box.y, box.z};
std::sort(begin(dims), end(dims));
return 2 * (dims[0] + dims[1]) + std::reduce(begin(dims), end(dims), 1, std::multiplies<>());
}
TEST_SUITE("documented examples") {
TEST_CASE("part 1") {
REQUIRE(Part1({2,3,4}) == 58);
REQUIRE(Part1({1,1,10}) == 43);
}
TEST_CASE("part 2") {
REQUIRE(Part2({2,3,4}) == 34);
REQUIRE(Part2({1,1,10}) == 14);
}
}
auto Main(std::istream & in, std::ostream & out) -> void
{
std::int64_t part1 = 0;
std::int64_t part2 = 0;
std::string line;
while (std::getline(in, line)) {
namespace qi = boost::spirit::qi;
using p = qi::int_parser<std::int64_t>;
auto b = begin(line); // updated on successful parse
auto const e = end(line);
Box box;
if (not qi::parse(b, e, p{} >> 'x' >> p{} >> 'x' >> p{}, box.x, box.y, box.z) || b != e)
{
throw std::runtime_error{"Parse error"};
}
part1 += Part1(box);
part2 += Part2(box);
}
std::cout << "Part1: " << part1 << std::endl;
std::cout << "Part2: " << part2 << std::endl;
}

5
2015/CMakeLists.txt Normal file
View File

@ -0,0 +1,5 @@
add_executable(2015_01 01.cpp)
target_link_libraries(2015_01 aocpp)
add_executable(2015_02 02.cpp)
target_link_libraries(2015_02 aocpp)

View File

@ -26,6 +26,7 @@ add_subdirectory(dlx)
add_subdirectory(knothash)
add_subdirectory(zmod)
add_subdirectory(intcode)
add_subdirectory(2015)
add_subdirectory(2016)
add_subdirectory(2017)
add_subdirectory(2018)

View File

@ -8,7 +8,7 @@ struct Counter
{
using difference_type = std::ptrdiff_t;
struct EmptyRef {
struct NoOpReference {
template <typename T>
auto operator=(T&&) const -> void {}
};
@ -31,14 +31,14 @@ struct Counter
return n++;
}
auto operator*() const -> EmptyRef
auto operator*() const -> NoOpReference
{
return {};
}
auto operator-(Counter rhs) const -> difference_type
{
return static_cast<difference_type>(n) - static_cast<difference_type>(rhs);
return static_cast<difference_type>(n) - static_cast<difference_type>(rhs.n);
}
operator std::size_t() const

View File

@ -48,4 +48,4 @@ auto ParseGrammar_(G const& grammar, std::istream & in) -> typename G::start_typ
}
#endif
#endif