Compare commits
2 Commits
c86e71ec09
...
ab86531dc5
Author | SHA1 | Date | |
---|---|---|---|
ab86531dc5 | |||
8c3b0ad0cf |
97
2023/01.cpp
Normal file
97
2023/01.cpp
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
#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;
|
||||||
|
}
|
10
2023/04.cpp
10
2023/04.cpp
@ -48,10 +48,14 @@ auto CountWins(std::vector<Card>& cards) -> std::vector<std::size_t>
|
|||||||
|
|
||||||
for (auto& card : cards)
|
for (auto& card : cards)
|
||||||
{
|
{
|
||||||
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 c = std::set_intersection(card.mine.begin(), card.mine.end(), card.winners.begin(), card.winners.end(), Counter<std::size_t>{});
|
auto const counter =
|
||||||
result.push_back(c);
|
std::set_intersection(
|
||||||
|
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,3 +1,6 @@
|
|||||||
|
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