Add solution for 2025 Day 11
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
svr: aaa bbb
|
||||||
|
aaa: fft
|
||||||
|
fft: ccc
|
||||||
|
bbb: tty
|
||||||
|
tty: ccc
|
||||||
|
ccc: ddd eee
|
||||||
|
ddd: hub
|
||||||
|
hub: fff
|
||||||
|
eee: dac
|
||||||
|
dac: fff
|
||||||
|
fff: ggg hhh
|
||||||
|
ggg: out
|
||||||
|
hhh: out
|
||||||
+76
-11
@@ -2,35 +2,100 @@
|
|||||||
|
|
||||||
namespace y2025::day11
|
namespace y2025::day11
|
||||||
{
|
{
|
||||||
using InputType = std::vector<int>;
|
using InputType = std::map<std::string, std::vector<std::string>>;
|
||||||
REGISTER_DAY(2025, Day11, InputType, int);
|
REGISTER_DAY(2025, Day11, InputType, int64_t);
|
||||||
|
|
||||||
REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 1, 0);
|
REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 1, 8);
|
||||||
REGISTER_TEST(2025, Day11, Input, 1, 0);
|
REGISTER_TEST(2025, Day11, Input, 1, 613);
|
||||||
REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 2, 0);
|
REGISTER_TEST_EXAMPLE(2025, Day11, ExampleInput, 2, 2);
|
||||||
REGISTER_TEST(2025, Day11, Input, 2, 0);
|
REGISTER_TEST(2025, Day11, Input, 2, 372918445876116);
|
||||||
|
|
||||||
READ_INPUT(input)
|
READ_INPUT(input)
|
||||||
{
|
{
|
||||||
std::vector<int> vec;
|
std::map<std::string, std::vector<std::string>> vec;
|
||||||
std::string str;
|
std::string str;
|
||||||
while (std::getline(input, str))
|
while (std::getline(input, str))
|
||||||
{
|
{
|
||||||
std::stringstream ss{str};
|
std::stringstream ss{str};
|
||||||
int n;
|
int n;
|
||||||
ss >> n;
|
std::string str2;
|
||||||
vec.emplace_back(n);
|
ss >> str2;
|
||||||
|
str2.pop_back();
|
||||||
|
std::string str3;
|
||||||
|
|
||||||
|
std::vector<std::string> paths;
|
||||||
|
while (ss >> str3)
|
||||||
|
{
|
||||||
|
paths.emplace_back(str3);
|
||||||
|
}
|
||||||
|
vec[str2] = paths;
|
||||||
}
|
}
|
||||||
return vec;
|
return vec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct State
|
||||||
|
{
|
||||||
|
std::string current;
|
||||||
|
bool fft;
|
||||||
|
bool dac;
|
||||||
|
|
||||||
|
bool operator<(const State& other) const
|
||||||
|
{
|
||||||
|
if (fft != other.fft)
|
||||||
|
return fft < other.fft;
|
||||||
|
if (dac != other.dac)
|
||||||
|
return dac < other.dac;
|
||||||
|
return current < other.current;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int PathFind(const std::map<std::string, std::vector<std::string>>& paths, const std::string& current, int count)
|
||||||
|
{
|
||||||
|
if (current == "out")
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
int validPaths = 0;
|
||||||
|
for (auto& branch : paths.at(current))
|
||||||
|
{
|
||||||
|
validPaths += PathFind(paths, branch, count + 1);
|
||||||
|
}
|
||||||
|
return validPaths;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t PathFind2(const std::map<std::string, std::vector<std::string>>& paths,
|
||||||
|
const State& currentState,
|
||||||
|
std::map<State, int64_t>& memoization)
|
||||||
|
{
|
||||||
|
if (currentState.current == "out")
|
||||||
|
{
|
||||||
|
if (currentState.fft && currentState.dac)
|
||||||
|
return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto memIt = memoization.find(currentState);
|
||||||
|
if (memIt != memoization.end())
|
||||||
|
return memIt->second;
|
||||||
|
|
||||||
|
int64_t validPaths = 0;
|
||||||
|
for (auto& branch : paths.at(currentState.current))
|
||||||
|
{
|
||||||
|
bool fft = currentState.fft || branch == "fft";
|
||||||
|
bool dac = currentState.dac || branch == "dac";
|
||||||
|
validPaths += PathFind2(paths, State{branch, fft, dac}, memoization);
|
||||||
|
}
|
||||||
|
memoization[currentState] = validPaths;
|
||||||
|
return validPaths;
|
||||||
|
}
|
||||||
|
|
||||||
OUTPUT1(input)
|
OUTPUT1(input)
|
||||||
{
|
{
|
||||||
return 0;
|
return PathFind(input, isExample ? "svr" : "you", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
OUTPUT2(input)
|
OUTPUT2(input)
|
||||||
{
|
{
|
||||||
return 0;
|
std::map<State, int64_t> memoization{};
|
||||||
|
return PathFind2(input, State{"svr", false, false}, memoization);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user