Guest User

Untitled

a guest
Dec 21st, 2025
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. void day1()
  2. {
  3. // Open input file (Day 1.txt)
  4. std::ifstream inputFile("Input/Day1.txt");
  5. if (!inputFile.is_open())
  6. {
  7. std::cerr << "Error opening file!" << std::endl;
  8. return;
  9. }
  10.  
  11. int pos = 50; // Assuming starting position is 50 on a dial of 100 digits (0-99)
  12. int totalZeros = 0;
  13. std::string line = "Temp";
  14. Part part = Part::TWO;
  15.  
  16. while (std::getline(inputFile, line) && !line.empty())
  17. {
  18. std::cout << "Processing line: " << line << std::endl;
  19. // Each line is L or R followed by a number so L10, R5, etc. number can be multiple digits
  20. // Separate the direction and the number
  21. char direction = line[0];
  22. int number = std::stoi(line.substr(1));
  23.  
  24. if (part == Part::ONE)
  25. {
  26. number = number % 100; // As its a dial of 100 digits, wrap around using modulo 100
  27. if (direction == 'L')
  28. {
  29. pos = (pos - number + 100) % 100; // Move left and wrap around
  30. }
  31. else if (direction == 'R')
  32. {
  33. pos = (pos + number) % 100; // Move right and wrap around
  34. }
  35.  
  36. if (pos == 0)
  37. {
  38. totalZeros++;
  39. std::cout << totalZeros << std::endl;
  40. }
  41. }
  42.  
  43. else if (part == Part::TWO)
  44. {
  45. bool atZero = false;
  46. if (pos == 0)
  47. {
  48. // Any rotation less than 100 is NOT a totalZeros++
  49. atZero = true;
  50. }
  51.  
  52. if (direction == 'L')
  53. {
  54. pos -= number;
  55. if (pos == 0 && number == 0)
  56. {
  57. // No movement so no totalZero I think? Undefined in question or maybe L0 / R0 is not an expected output
  58. }
  59. else if (pos == 0) // number != 0
  60. {
  61. // One case so totalZeros++
  62. totalZeros++;
  63. }
  64. else if (pos < 0)
  65. {
  66. if (!atZero)
  67. totalZeros++; // Since it passed 0
  68. int numRots = pos / (-100); // If it passed 0 more than once
  69. totalZeros += numRots;
  70. pos = 100 + (pos + (numRots * 100)); // -725 -> 75
  71. }
  72. }
  73. else if (direction == 'R')
  74. {
  75. pos += number;
  76. if (pos >= 100)
  77. {
  78. int numRots = pos / 100;
  79. totalZeros += numRots;
  80. pos = pos - (100 * numRots);
  81. }
  82. }
  83. }
  84. std::cout << "Current position: " << pos << std::endl;
  85. std::cout << "Current totalZeros: " << totalZeros << std::endl << std::endl;
  86. }
  87. std::cout << "Ending position: " << pos << std::endl;
  88. std::cout << "totalZeros: " << totalZeros << std::endl;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment