Skip to content

Commit

Permalink
Clean up day 17 solution slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
bg2b committed Dec 18, 2024
1 parent 43a1ffb commit fa10b8e
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions 17/doit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <optional>
#include <cassert>

Expand Down Expand Up @@ -42,7 +41,8 @@ struct cpu {
vector<int> run(optional<num> A = nullopt);

// Search for the minimum self-reproducing value
optional<num> search(num A);
optional<num> search(num A, size_t level);
num search() { return *search(0, code.size() - 1); }
};

cpu::cpu() {
Expand Down Expand Up @@ -118,31 +118,29 @@ vector<int> cpu::run(optional<num> A) {
return out;
}

optional<num> cpu::search(num A) {
optional<num> cpu::search(num A, size_t level) {
// The idea is that the program is vaguely counter-like, and you can
// work backwards from the high digits
for (int i = 0; i < 8; ++i) {
// Try next digit
auto out = run(A + i);
auto guess = 8 * A + i;
auto out = run(guess);
assert(out.size() <= code.size());
if (!equal(out.begin(), out.end(), code.begin() + (code.size() - out.size())))
// High digits won't match, no good
continue;
if (out.size() == code.size())
// Found!
return A + i;
if (A + i == 0)
// Avoid infinite recursion
if (out[level - (code.size() - out.size())] != code[level])
// Didn't match, try next digit
continue;
if (level == 0)
// Solution!
return guess;
// This digit is OK, look for the rest
if (auto a = search(8 * (A + i)); a.has_value())
if (auto a = search(guess, level - 1); a.has_value())
return *a;
}
return nullopt;
}

void part1() { print(cpu().run()); }
void part2() { cout << *cpu().search(0) << '\n'; }
void part2() { cout << cpu().search() << '\n'; }

int main(int argc, char **argv) {
if (argc != 2) {
Expand Down

0 comments on commit fa10b8e

Please sign in to comment.