Guest User

aoc 2025 d5

a guest
Dec 4th, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <unordered_set>
  7. using namespace std;
  8.  
  9. #define ll unsigned long long
  10.  
  11. std::vector<string> split(std::string& s, std::string&& delim) {
  12. std::vector<string> tokens;
  13. int start = 0;
  14. int end;
  15. while( (end = s.find(delim, start)) != string::npos) {
  16. tokens.push_back(s.substr(start, end - start));
  17. start = end + delim.length();
  18. }
  19. if(start < s.length()) {
  20. tokens.push_back(s.substr(start));
  21. }
  22. return tokens;
  23. }
  24.  
  25.  
  26. ll solve(std::vector<ll> nums, std::vector<std::vector<ll>> ranges) {
  27. ll ans = 0;
  28. for(const auto& num : nums) {
  29. for(const auto& range : ranges) {
  30. if(num >= range[0] && num <= range[1]) {
  31. ans += 1;
  32. break;
  33. }
  34. }
  35. }
  36. return ans;
  37. }
  38.  
  39. struct Marker {
  40. ll val;
  41. int type;
  42. bool operator<(const Marker& other) const {
  43. return val == other.val ? (type < other.type) : (val < other.val);
  44. }
  45. };
  46.  
  47. ll solve2(std::vector<std::vector<ll>> ranges) {
  48. ll ans = 0;
  49. std::vector<Marker> markers;
  50. for(const auto& range : ranges) {
  51. markers.push_back({range[0],0});
  52. markers.push_back({range[1],1});
  53. }
  54. std::sort(markers.begin(), markers.end());
  55.  
  56. int counter = 0;
  57. int start = 0;
  58. for(int i=0; i<markers.size(); i++) {
  59. if(counter == 0) {
  60. start = i;
  61. }
  62. counter = (markers[i].type == 0) ? counter + 1 : counter - 1;
  63. if(counter == 0) {
  64. ans += (markers[i].val - markers[start].val + 1);
  65. }
  66. }
  67. return ans;
  68. }
  69.  
  70.  
  71. int main() {
  72. ifstream inputFile("../data/day5.txt");
  73. std::string line;
  74. std::vector<std::vector<ll>> ranges;
  75. std::vector<ll> queries;
  76. bool readRanges = true;
  77. while(std::getline(inputFile, line)) {
  78. if(line == "") {
  79. readRanges = false;
  80. continue;
  81. }
  82. if(readRanges) {
  83. vector<string> strNums = split(line, "-");
  84. ranges.push_back({std::stoull(strNums[0]), std::stoull(strNums[1])});
  85. }
  86. else {
  87. queries.push_back(std::stoull(line));
  88. }
  89. }
  90. std::cout << solve2(ranges) << std::endl;
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment