Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void day1()
- {
- // Open input file (Day 1.txt)
- std::ifstream inputFile("Input/Day1.txt");
- if (!inputFile.is_open())
- {
- std::cerr << "Error opening file!" << std::endl;
- return;
- }
- int pos = 50; // Assuming starting position is 50 on a dial of 100 digits (0-99)
- int totalZeros = 0;
- std::string line = "Temp";
- Part part = Part::TWO;
- while (std::getline(inputFile, line) && !line.empty())
- {
- std::cout << "Processing line: " << line << std::endl;
- // Each line is L or R followed by a number so L10, R5, etc. number can be multiple digits
- // Separate the direction and the number
- char direction = line[0];
- int number = std::stoi(line.substr(1));
- if (part == Part::ONE)
- {
- number = number % 100; // As its a dial of 100 digits, wrap around using modulo 100
- if (direction == 'L')
- {
- pos = (pos - number + 100) % 100; // Move left and wrap around
- }
- else if (direction == 'R')
- {
- pos = (pos + number) % 100; // Move right and wrap around
- }
- if (pos == 0)
- {
- totalZeros++;
- std::cout << totalZeros << std::endl;
- }
- }
- else if (part == Part::TWO)
- {
- bool atZero = false;
- if (pos == 0)
- {
- // Any rotation less than 100 is NOT a totalZeros++
- atZero = true;
- }
- if (direction == 'L')
- {
- pos -= number;
- if (pos == 0 && number == 0)
- {
- // No movement so no totalZero I think? Undefined in question or maybe L0 / R0 is not an expected output
- }
- else if (pos == 0) // number != 0
- {
- // One case so totalZeros++
- totalZeros++;
- }
- else if (pos < 0)
- {
- if (!atZero)
- totalZeros++; // Since it passed 0
- int numRots = pos / (-100); // If it passed 0 more than once
- totalZeros += numRots;
- pos = 100 + (pos + (numRots * 100)); // -725 -> 75
- }
- }
- else if (direction == 'R')
- {
- pos += number;
- if (pos >= 100)
- {
- int numRots = pos / 100;
- totalZeros += numRots;
- pos = pos - (100 * numRots);
- }
- }
- }
- std::cout << "Current position: " << pos << std::endl;
- std::cout << "Current totalZeros: " << totalZeros << std::endl << std::endl;
- }
- std::cout << "Ending position: " << pos << std::endl;
- std::cout << "totalZeros: " << totalZeros << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment