Skip to content

Commit

Permalink
2024 day 19: solve part 2
Browse files Browse the repository at this point in the history
Can't say I didn't see this coming.
  • Loading branch information
yut23 committed Dec 19, 2024
1 parent b9fd2df commit 6365996
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions 2024/answer_tests/day19/example1.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Day 19:
6
16
6 changes: 4 additions & 2 deletions 2024/src/day19.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ int main(int argc, char **argv) {
auto [checker, designs] = aoc::day19::read_input(infile);

int part_1 = 0;
long part_2 = 0;
for (std::size_t i = 0; i < designs.size(); ++i) {
if (checker.check(designs[i])) {
if (long count = checker.check(designs[i])) {
++part_1;
part_2 += count;
}
}
std::cout << part_1 << "\n";
std::cout << part_1 << "\n" << part_2 << "\n";

return 0;
}
26 changes: 12 additions & 14 deletions 2024/src/day19.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ class DesignChecker {
Node *root_ptr() { return &nodes.front(); }
const Node *root_ptr() const { return &nodes.front(); }

bool check_helper(const ColorString &design,
std::vector<std::optional<bool>> &memo,
long check_helper(const ColorString &design,
std::vector<std::optional<long>> &memo,
std::size_t start) const;

public:
DesignChecker() : nodes(1) {}

void add_pattern(const ColorString &pattern);
bool check(const ColorString &design) const;
long check(const ColorString &design) const;
};

DesignChecker::Node &DesignChecker::get_child(DesignChecker::Node &parent,
Expand All @@ -107,15 +107,15 @@ void DesignChecker::add_pattern(const ColorString &pattern) {
node->is_terminal = true;
}

bool DesignChecker::check(const ColorString &design) const {
std::vector<std::optional<bool>> memo(design.size());
long DesignChecker::check(const ColorString &design) const {
std::vector<std::optional<long>> memo(design.size());
return check_helper(design, memo, 0);
}

// check a design against all possible towel combinations by recursing when
// reaching a terminal node
bool DesignChecker::check_helper(const ColorString &design,
std::vector<std::optional<bool>> &memo,
long DesignChecker::check_helper(const ColorString &design,
std::vector<std::optional<long>> &memo,
std::size_t start) const {
// base case
if (start == design.size()) {
Expand All @@ -125,20 +125,18 @@ bool DesignChecker::check_helper(const ColorString &design,
return *cached;
}
const Node *node = root_ptr();
bool valid = false;
long count = 0;
for (std::size_t i = start; i < design.size(); ++i) {
node = node->at(design[i]);
if (node == nullptr) {
valid = false;
break;
}
if (node->is_terminal && check_helper(design, memo, i + 1)) {
valid = true;
break;
if (node->is_terminal) {
count += check_helper(design, memo, i + 1);
}
}
memo[start] = valid;
return valid;
memo[start] = count;
return count;
}

std::pair<DesignChecker, std::vector<ColorString>>
Expand Down

0 comments on commit 6365996

Please sign in to comment.