Advertisement
Guest User

AOC2024 day 13

a guest
Dec 14th, 2024
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.33 KB | Source Code | 0 0
  1. #include <cmath>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. vector<string> split (const string &s, char delim) {
  12.     vector<string> result;
  13.     stringstream ss (s);
  14.     string item;
  15.  
  16.     while (getline (ss, item, delim)) {
  17.         result.push_back (item);
  18.     }
  19.  
  20.     return result;
  21. }
  22.  
  23. vector<string> readFile(string path) {
  24.     ifstream file(path);
  25.     vector<string> lines;
  26.     string line;
  27.  
  28.     if (file.is_open()) {
  29.         while (getline(file, line)) {
  30.             lines.push_back(line);
  31.         }
  32.         file.close();
  33.     } else {
  34.         cout << "Unable to open file" << endl;
  35.     }
  36.  
  37.     return lines;
  38. }
  39.  
  40. vector<vector<vector<double>>> parseInput(vector<string> input) {
  41.     vector<vector<vector<double>>> systems;
  42.     for(int i = 0; i < input.size(); i += 4) {
  43.         vector<double> equations;
  44.         vector<string> buttonA = split(input[i], ' ');
  45.         vector<string> buttonB = split(input[i + 1], ' ');
  46.         vector<string> price = split(input[i + 2], ' ');
  47.  
  48.         buttonA[2].pop_back();
  49.         double ax = stod(split(buttonA[2], '+')[1]);
  50.         double ay = stod(split(buttonA[3], '+')[1]);
  51.  
  52.         buttonB[2].pop_back();
  53.         double bx = stod(split(buttonB[2], '+')[1]);
  54.         double by = stod(split(buttonB[3], '+')[1]);
  55.  
  56.         price[1].pop_back();
  57.         double px = stod(split(price[1], '=')[1]);
  58.         double py = stod(split(price[2], '=')[1]);
  59.  
  60.         vector<vector<double>> system = {{ax, bx, px}, {ay, by, py}};
  61.         systems.push_back(system);
  62.     }
  63.     return systems;
  64. }  
  65.  
  66. vector<double> solveSystemOfEquations(vector<vector<double>> system) {
  67.     double D = (system[0][0] * system[1][1]) - (system[1][0] * system[0][1]);
  68.     double Dx = (system[0][2] * system[1][1]) - (system[1][2] * system[0][1]);
  69.     double Dy = (system[0][0] * system[1][2]) - (system[1][0] * system[0][2]);
  70.  
  71.     double x = Dx / D;
  72.     double y = Dy / D;
  73.  
  74.     return {x, y};
  75. }
  76.  
  77.  
  78. int part1(vector<vector<vector<double>>> systems) {
  79.     int totalTokens = 0;
  80.     for (int i = 0; i < systems.size(); i++) {
  81.         vector<double> result = solveSystemOfEquations(systems[i]);
  82.         double a = result[0];
  83.         double b = result[1];
  84.        
  85.         if ((a < 100 && b < 100) && ((int) a == a && (int) b == b)) {
  86.             totalTokens += (a * 3) + (b * 1);
  87.         }
  88.     }
  89.    
  90.     cout << "Part 1 result: " << totalTokens << endl;
  91.  
  92.     return 0;
  93. }
  94.  
  95. int part2(vector<vector<vector<double>>> systems) {
  96.     unsigned long long totalTokens = 0;
  97.     for (int i = 0; i < systems.size(); i++) {
  98.         vector<vector<double>> system = systems[i];
  99.         system[0][2] = system[0][2]+10000000000000;
  100.         system[1][2] = system[1][2]+10000000000000;
  101.         vector<double> result = solveSystemOfEquations(system);
  102.         double a = result[0];
  103.         double b = result[1];
  104.        
  105.         if (((unsigned long long) a == a && (unsigned long long) b == b)) {
  106.             totalTokens += (a * 3) + (b * 1);
  107.         }
  108.     }
  109.    
  110.     cout << "Part 2 result: " << totalTokens << endl;
  111.  
  112.     return 0;
  113. }
  114.  
  115. int main() {
  116.     vector<string> input = readFile("day13.txt");
  117.     vector<vector<vector<double>>> systems = parseInput(input);
  118.     part1(systems);
  119.     part2(systems);
  120.  
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement