aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-04-05 13:15:31 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-04-05 13:15:31 +0800
commite23d6cd0d7fc195453dfbef876a893360f49178a (patch)
treefcac3ec942317f0147ca34f02bd2f80845e2b613
parent0742b596f9da460736d64a261312099b3e6483b3 (diff)
downloadadvent-of-code-e23d6cd0d7fc195453dfbef876a893360f49178a.tar.gz
advent-of-code-e23d6cd0d7fc195453dfbef876a893360f49178a.zip
2019 day2
-rw-r--r--src/2019/day2/aoc.cpp25
-rw-r--r--src/2019/day2/aoc.h2
-rw-r--r--test/test_2019.cpp4
3 files changed, 24 insertions, 7 deletions
diff --git a/src/2019/day2/aoc.cpp b/src/2019/day2/aoc.cpp
index 7707191..7f9eb0a 100644
--- a/src/2019/day2/aoc.cpp
+++ b/src/2019/day2/aoc.cpp
@@ -33,7 +33,25 @@ void run(size_t i, std::vector<int>& codes) {
}
}
-int day2(line_view file) {
+int test(int n, int v, std::vector<int> codes) {
+ codes[1] = n;
+ codes[2] = v;
+ run(0, codes);
+ return codes[0];
+}
+
+int test(int target, const std::vector<int>& codes) {
+ for (int n = 0; n <= 99; n++) {
+ for (int v = 0; v <= 99; v++) {
+ if (target == test(n, v, codes)) {
+ return n * 100 + v;
+ }
+ }
+ }
+ return 0;
+}
+
+std::pair<int, int> day2(line_view file) {
std::vector<int> optcodes;
const char* p = file.line;
while (p < file.line + file.length) {
@@ -42,10 +60,7 @@ int day2(line_view file) {
}
p++;
}
- optcodes[1] = 12;
- optcodes[2] = 2;
- run(0, optcodes);
- return optcodes[0];
+ return {test(12, 2, optcodes), test(19690720, optcodes)};
}
} // namespace aoc2019
diff --git a/src/2019/day2/aoc.h b/src/2019/day2/aoc.h
index 1304999..6cfaa34 100644
--- a/src/2019/day2/aoc.h
+++ b/src/2019/day2/aoc.h
@@ -4,5 +4,5 @@
namespace aoc2019 {
-int day2(line_view);
+std::pair<int, int> day2(line_view);
}
diff --git a/test/test_2019.cpp b/test/test_2019.cpp
index 1505395..20cbd24 100644
--- a/test/test_2019.cpp
+++ b/test/test_2019.cpp
@@ -12,5 +12,7 @@ TEST_CASE("The Tyranny of the Rocket Equation", "[2019]") {
TEST_CASE("1202 Program Alarm", "[2019]") {
line_view lv = load_file("../src/2019/day2/input");
- REQUIRE(11590668 == aoc2019::day2(lv));
+ auto p = aoc2019::day2(lv);
+ REQUIRE(11590668 == p.first);
+ REQUIRE(2254 == p.second);
}