Compare commits
No commits in common. "ab86531dc591de07a15f51d735191e0503c74a09" and "c86e71ec09619915275b132d550070e25df52cc4" have entirely different histories.
ab86531dc5
...
c86e71ec09
97
2023/01.cpp
97
2023/01.cpp
@ -1,97 +0,0 @@
|
|||||||
#include <algorithm>
|
|
||||||
#include <cctype>
|
|
||||||
#include <cstdint>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string_view>
|
|
||||||
#include <string>
|
|
||||||
#include <tuple>
|
|
||||||
#include <tuple>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <aocpp/Startup.hpp>
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
using Input = std::vector<std::string>;
|
|
||||||
|
|
||||||
/// @brief Parse the input stream as a newline-delimited list of lists of integers
|
|
||||||
/// @param in input file parsed until EOF
|
|
||||||
/// @return outer list of elves, inner lists of calories
|
|
||||||
auto Parse(std::istream & in) -> Input
|
|
||||||
{
|
|
||||||
Input results {};
|
|
||||||
std::move(
|
|
||||||
std::istream_iterator<std::string>{in},
|
|
||||||
std::istream_iterator<std::string>{},
|
|
||||||
std::back_inserter(results)
|
|
||||||
);
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto Part1(Input const& entries) -> std::int64_t
|
|
||||||
{
|
|
||||||
std::int64_t acc = 0;
|
|
||||||
for (auto const& entry : entries)
|
|
||||||
{
|
|
||||||
auto lhs = std::find_if(entry.begin(), entry.end(), [](char c){ return std::isdigit(c); });
|
|
||||||
auto rhs = std::find_if(entry.rbegin(), entry.rend(), [](char c){ return std::isdigit(c); });
|
|
||||||
acc += 10 * (*lhs - '0') + (*rhs - '0');
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto Part2(Input const& entries) -> std::int64_t
|
|
||||||
{
|
|
||||||
static std::pair<char const*, std::int64_t> numbers[] = {
|
|
||||||
{"one", 1}, {"two", 2}, {"three", 3},
|
|
||||||
{"four", 4}, {"five", 5}, {"six", 6},
|
|
||||||
{"seven", 7}, {"eight", 8}, {"nine", 9},
|
|
||||||
{"0",0},{"1",1},{"2",2},{"3",3},{"4",4},
|
|
||||||
{"5",5},{"6",6},{"7",7},{"8",8},{"9",9}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::int64_t acc = 0;
|
|
||||||
for (auto const& entry : entries)
|
|
||||||
{
|
|
||||||
|
|
||||||
std::int64_t lhs = 0;
|
|
||||||
std::int64_t rhs = 0;
|
|
||||||
|
|
||||||
for (auto cursor = entry.begin(); cursor < entry.end(); cursor++)
|
|
||||||
{
|
|
||||||
for (auto const [k,v] : numbers)
|
|
||||||
{
|
|
||||||
if (std::string_view{cursor, entry.end()}.starts_with(k)) {
|
|
||||||
lhs = v;
|
|
||||||
goto do_rhs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
do_rhs:
|
|
||||||
for (auto cursor = entry.end(); cursor --> entry.begin();)
|
|
||||||
{
|
|
||||||
std::int64_t lhs = 0;
|
|
||||||
for (auto const [k,v] : numbers)
|
|
||||||
{
|
|
||||||
if (std::string_view{cursor, entry.end()}.starts_with(k)) {
|
|
||||||
rhs = v;
|
|
||||||
goto next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
next:
|
|
||||||
acc += 10 * lhs + rhs;
|
|
||||||
}
|
|
||||||
return acc;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
auto Main(std::istream & in, std::ostream & out) -> void
|
|
||||||
{
|
|
||||||
auto const entries = Parse(in);
|
|
||||||
out << "Part 1: " << Part1(entries) << std::endl;
|
|
||||||
out << "Part 2: " << Part2(entries) << std::endl;
|
|
||||||
}
|
|
@ -50,12 +50,8 @@ auto CountWins(std::vector<Card>& cards) -> std::vector<std::size_t>
|
|||||||
{
|
{
|
||||||
std::sort(card.mine.begin(), card.mine.end());
|
std::sort(card.mine.begin(), card.mine.end());
|
||||||
std::sort(card.winners.begin(), card.winners.end());
|
std::sort(card.winners.begin(), card.winners.end());
|
||||||
auto const counter =
|
auto const c = std::set_intersection(card.mine.begin(), card.mine.end(), card.winners.begin(), card.winners.end(), Counter<std::size_t>{});
|
||||||
std::set_intersection(
|
result.push_back(c);
|
||||||
card.mine .begin(), card.mine .end(),
|
|
||||||
card.winners.begin(), card.winners.end(),
|
|
||||||
Counter<std::size_t>{});
|
|
||||||
result.push_back(counter);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
add_executable(2023_01 01.cpp)
|
|
||||||
target_link_libraries(2023_01 aocpp Boost::headers)
|
|
||||||
|
|
||||||
add_executable(2023_02 02.cpp)
|
add_executable(2023_02 02.cpp)
|
||||||
target_link_libraries(2023_02 aocpp Boost::headers)
|
target_link_libraries(2023_02 aocpp Boost::headers)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user