|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <string> |
| 4 | +#include <vector> |
| 5 | +#include <cmath> |
| 6 | + |
| 7 | +using namespace std; |
| 8 | + |
| 9 | +void debugOutput(const string& message, bool debugFlag = false) { |
| 10 | + if (debugFlag) { |
| 11 | + cout << message << endl; |
| 12 | + } |
| 13 | + return; |
| 14 | +} |
| 15 | + |
| 16 | +int getData(vector<string> &data, string fileName = "data.txt") { |
| 17 | + ifstream file(fileName); // Open the file |
| 18 | + string line; |
| 19 | + data = {}; |
| 20 | + |
| 21 | + if (!file.is_open()) { // Check if the file opened successfully |
| 22 | + cerr << "Error opening the file!" << endl; |
| 23 | + return 1; |
| 24 | + } |
| 25 | + |
| 26 | + while (getline(file, line)) { // Read each line |
| 27 | + data.push_back(line); |
| 28 | + } |
| 29 | + |
| 30 | + file.close(); // Close the file |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +int solution1(vector<string> &data, bool debugFlag = false) { |
| 35 | + int dial = 50; |
| 36 | + int start = dial; |
| 37 | + int incr = 0; |
| 38 | + int result = 0; |
| 39 | + |
| 40 | + for (const auto& datum : data) { |
| 41 | + incr = stoi(datum.substr(1)); |
| 42 | + if (datum[0] == 'L') { |
| 43 | + dial -= incr; |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + dial += incr; |
| 48 | + } |
| 49 | + |
| 50 | + if (dial < 0) { |
| 51 | + dial = 100 + dial; |
| 52 | + } |
| 53 | + |
| 54 | + dial %= 100; |
| 55 | + if (dial == 0) { |
| 56 | + result++; |
| 57 | + } |
| 58 | + debugOutput(datum + " " + to_string(start) + " " + to_string(dial) + " " + to_string(result), debugFlag); |
| 59 | + start = dial; |
| 60 | + } |
| 61 | + return result; |
| 62 | +} |
| 63 | + |
| 64 | +int solution2(vector<string> &data, bool debugFlag = false) { |
| 65 | + int dial = 50; |
| 66 | + int start = dial; |
| 67 | + int incr = 0; |
| 68 | + int result = 0; |
| 69 | + |
| 70 | + for (const auto& datum : data) { |
| 71 | + incr = stoi(datum.substr(1)); |
| 72 | + for (int i = 0; i < abs(incr); i++) { |
| 73 | + if (datum[0] == 'L') { |
| 74 | + dial--; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + dial++; |
| 79 | + } |
| 80 | + if (dial > 99) { |
| 81 | + dial = 0; |
| 82 | + } |
| 83 | + if (dial < 0) { |
| 84 | + dial = 99; |
| 85 | + } |
| 86 | + if (dial == 0) { |
| 87 | + result++; |
| 88 | + } |
| 89 | + } |
| 90 | + debugOutput(datum + " " + to_string(start) + " " + to_string(dial) + " " + to_string(result), debugFlag); |
| 91 | + start = dial; |
| 92 | + } |
| 93 | + return result; |
| 94 | +} |
| 95 | + |
| 96 | +int main() |
| 97 | +{ |
| 98 | + int result; |
| 99 | + vector<string> data = {}; |
| 100 | + |
| 101 | + cout << "AdventOfCode25 Day 1\n\n"; |
| 102 | + |
| 103 | + if(getData(data) == 0) { |
| 104 | + result = solution1(data, false); |
| 105 | + cout << "The answer is " << result << endl; |
| 106 | + |
| 107 | + result = solution2(data, false); |
| 108 | + cout << "The answer is " << result << endl; |
| 109 | + } |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments