diff options
Diffstat (limited to 'src/day24.cpp')
| -rw-r--r-- | src/day24.cpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/day24.cpp b/src/day24.cpp index 296d38a..76f459b 100644 --- a/src/day24.cpp +++ b/src/day24.cpp | |||
| @@ -2,9 +2,12 @@ | |||
| 2 | #include <fstream> | 2 | #include <fstream> |
| 3 | #include <iostream> | 3 | #include <iostream> |
| 4 | #include <limits> | 4 | #include <limits> |
| 5 | #include <map> | ||
| 5 | #include <string> | 6 | #include <string> |
| 6 | #include <tuple> | 7 | #include <tuple> |
| 7 | #include <vector> | 8 | #include <vector> |
| 9 | #include <z3++.h> | ||
| 10 | |||
| 8 | using namespace std; | 11 | using namespace std; |
| 9 | 12 | ||
| 10 | using line_type = tuple<double, double, double, double, double, double>; | 13 | using line_type = tuple<double, double, double, double, double, double>; |
| @@ -122,10 +125,49 @@ part1(const puzzle_type& input) | |||
| 122 | cout << sum << endl; | 125 | cout << sum << endl; |
| 123 | } | 126 | } |
| 124 | 127 | ||
| 128 | void | ||
| 129 | part2(const puzzle_type& input) | ||
| 130 | { | ||
| 131 | auto config = z3::config(); | ||
| 132 | auto context = z3::context(config); | ||
| 133 | auto solver = z3::solver(context); | ||
| 134 | |||
| 135 | const auto px = context.int_const("px"); | ||
| 136 | const auto py = context.int_const("py"); | ||
| 137 | const auto pz = context.int_const("pz"); | ||
| 138 | const auto vx = context.int_const("vx"); | ||
| 139 | const auto vy = context.int_const("vy"); | ||
| 140 | const auto vz = context.int_const("vz"); | ||
| 141 | |||
| 142 | for ( size_t i = 0; i != input.size(); ++i ) { | ||
| 143 | const auto& stone = input[i]; | ||
| 144 | |||
| 145 | const auto pxn = context.int_val(int64_t(get<0>(stone))); | ||
| 146 | const auto pyn = context.int_val(int64_t(get<1>(stone))); | ||
| 147 | const auto pzn = context.int_val(int64_t(get<2>(stone))); | ||
| 148 | const auto vxn = context.int_val(int64_t(get<3>(stone))); | ||
| 149 | const auto vyn = context.int_val(int64_t(get<4>(stone))); | ||
| 150 | const auto vzn = context.int_val(int64_t(get<5>(stone))); | ||
| 151 | const auto tn = context.int_const(to_string(i+1).c_str()); | ||
| 152 | |||
| 153 | solver.add(tn >= 0); | ||
| 154 | solver.add(pxn + vxn * tn == px + vx * tn); | ||
| 155 | solver.add(pyn + vyn * tn == py + vy * tn); | ||
| 156 | solver.add(pzn + vzn * tn == pz + vz * tn); | ||
| 157 | } | ||
| 158 | |||
| 159 | solver.check(); | ||
| 160 | |||
| 161 | auto model = solver.get_model(); | ||
| 162 | |||
| 163 | cout << model.eval(px).as_int64() + model.eval(py).as_int64() + model.eval(pz).as_int64() << endl; | ||
| 164 | } | ||
| 165 | |||
| 125 | int | 166 | int |
| 126 | main() | 167 | main() |
| 127 | { | 168 | { |
| 128 | const auto input = read_file("data/day24.txt"); | 169 | const auto input = read_file("data/day24.txt"); |
| 129 | 170 | ||
| 130 | part1(input); | 171 | part1(input); |
| 172 | part2(input); | ||
| 131 | } | 173 | } |
