Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <unordered_map>
- #include <unordered_set>
- #include <queue>
- #include <cmath>
- using namespace std;
- #define ll long long
- std::vector<string> split(std::string s, std::string&& delim) {
- std::vector<string> tokens;
- int start = 0;
- int end;
- while( (end = s.find(delim, start)) != string::npos) {
- tokens.push_back(s.substr(start, end - start));
- start = end + delim.length();
- }
- if(start < s.length()) {
- tokens.push_back(s.substr(start));
- }
- return tokens;
- }
- ll solveSingle(int curr, std::vector<std::vector<int>>& buttons, std::vector<ll>& dp, int target) {
- if(dp[curr] >= 0) {
- return dp[curr];
- }
- if(dp[curr] == -1) {
- return -1;
- }
- dp[curr] = -1;
- if(curr == target) {
- dp[curr] = 0;
- return dp[curr];
- }
- ll total = LLONG_MAX;
- for(const auto& button : buttons) {
- int nextState = curr;
- vector<int> boof = {0,1};
- for(const auto & b : button) {
- nextState ^= (1 << b);
- }
- ll val = solveSingle(nextState, buttons, dp, target);
- if(val >= 0 && val != LLONG_MAX) {
- total = std::min(total, 1 + val);
- }
- }
- dp[curr] = total;
- return dp[curr];
- }
- ll solve(std::vector<std::vector<std::vector<int>>>& buttons, std::vector<int>& targets, std::vector<int>& maxes) {
- ll ans = 0;
- for(int i=0;i<buttons.size(); i++) {
- std::vector<ll> dp(maxes[i], -2);
- ans += solveSingle(0, buttons[i], dp, targets[i]);
- }
- return ans;
- }
- ll solve2(std::vector<std::vector<std::vector<int>>>& buttons, std::vector<int>& targets, std::vector<std::vector<int>>& joltages) {
- ll ans = 0;
- // Imma use python cause I'm lazy
- return ans;
- }
- int main() {
- ifstream inputFile("../data/day10.txt");
- std::string line;
- std::vector<std::vector<std::vector<int>>> buttons;
- std::vector<int> targets;
- std::vector<int> maxes;
- std::vector<std::vector<int>> joltages;
- while(std::getline(inputFile, line)) {
- vector<string> inputs = split(line, " ");
- int target = 0;
- int max = 0;
- std::string strTarget = inputs[0].substr(1,inputs[0].length()-2);
- for(int i=0; i<strTarget.length(); i++) {
- if(strTarget[i] == '#') {
- target += (1 << i);
- }
- max += (1<<i);
- }
- targets.push_back(target);
- maxes.push_back(max+1);
- vector<vector<int>> buttonList;
- for(int i=1; i<inputs.size()-1;i++) {
- auto buttonStr = split(inputs[i].substr(1,inputs[i].length()-2), ",");
- vector<int> button;
- for(const auto&s : buttonStr) {
- button.push_back(std::stoi(s));
- }
- buttonList.push_back(button);
- }
- buttons.push_back(buttonList);
- vector<int> joltage;
- auto joltageStr = split(inputs.back().substr(1,inputs.back().length()-2), ",");
- for(const auto&s : joltageStr) {
- joltage.push_back(std::stoi(s));
- }
- joltages.push_back(joltage);
- }
- std::cout << solve(buttons, targets, maxes) << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment