diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-19 21:19:17 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-19 21:19:17 +0800 |
commit | 7da1f98aaad6260d68ca0a819f0f7c5388b47a4d (patch) | |
tree | 17ccb62223ed0359005086d1935c26a970835eed /src/2019/day7/aoc.cpp | |
parent | afdd58189c94f793d7bd892ec3f2f476b596b7fa (diff) | |
download | advent-of-code-7da1f98aaad6260d68ca0a819f0f7c5388b47a4d.tar.gz advent-of-code-7da1f98aaad6260d68ca0a819f0f7c5388b47a4d.zip |
2019 day7 part1
Diffstat (limited to 'src/2019/day7/aoc.cpp')
-rw-r--r-- | src/2019/day7/aoc.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/2019/day7/aoc.cpp b/src/2019/day7/aoc.cpp index 593848b..c1a8ba5 100644 --- a/src/2019/day7/aoc.cpp +++ b/src/2019/day7/aoc.cpp @@ -1,4 +1,64 @@ #include "aoc.h" +#include "../day5/aoc.h" +#include <set> namespace aoc2019 { + +void find_max(int* is, int i, std::set<int>& ns, int* max, const std::vector<int>& codes) { + if (i == 10) { + set_computer(&is[i - 2]); + int n = run_computer(codes); + if (n > *max) { + *max = n; + } + } else { + for (int x : {0, 1, 2, 3, 4}) { + if (ns.find(x) != ns.end()) { + continue; + } else { + is[i] = x; + if (i > 0) { + set_computer(&is[i - 2]); + is[i + 1] = run_computer(codes); + } + ns.insert(x); + find_max(is, i + 2, ns, max, codes); + ns.erase(x); + } + } + } +} + +static void get_number(const char** pp, int* d) { + const char* p = *pp; + int sign = 1; + if (*p == '-') { + sign = -1; + p++; + } + while (*p >= '0' && *p <= '9') { + *d = *d * 10 + *p - '0'; + p++; + } + *d *= sign; + *pp = p; +} + +int day7(line_view file) { + int max{INT32_MIN}; + int is[10] = {0}; + std::set<int> ns; + std::vector<int> codes; + const char* p = file.line; + while (p < file.line + file.length) { + if ((*p >= '0' && *p <= '9') || *p == '-') { + int d{0}; + get_number(&p, &d); + codes.push_back(d); + } + p++; + } + find_max(is, 0, ns, &max, codes); + return max; } +} // namespace aoc2019 |