From 37c1fb045f0f22348a7f920cff984f6d3aaf6083 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Tue, 20 Dec 2022 15:10:39 -0500 Subject: [PATCH] Part 1 complete. --- 2022/10/larger.txt | 146 +++++++++++++++++++++++++++++++++++++++++++++ 2022/10/main.cpp | 105 ++++++++++++++++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 2022/10/larger.txt diff --git a/2022/10/larger.txt b/2022/10/larger.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/2022/10/larger.txt @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop diff --git a/2022/10/main.cpp b/2022/10/main.cpp index a43dc5e..74fbd88 100644 --- a/2022/10/main.cpp +++ b/2022/10/main.cpp @@ -4,6 +4,29 @@ #include #include +enum Command { + NOOP = 1, + ADDX, +}; + +Command ParseCommand(const std::string &str) { + if(str.size() < 4) + return Command::NOOP; + + std::string head = str.substr(0, 4); + if(head == "noop") + return Command::NOOP; + else if(head == "addx") + return Command::ADDX; + + std::cerr << "Missing command type \"" << head << "\"." << std::endl; + exit(-1); +} + +void IncrementCycleCount(long &cycle, const Command &command); +long Report(const long &cycle, const long &x); +void ApplyAction(const std::pair &command, long &x); + int main() { std::ifstream ifs("data.txt"); @@ -16,14 +39,96 @@ int main() unsigned long total = 0; unsigned long total_pt2 = 0; + std::vector > commands; for(std::string line; std::getline(ifs, line); ) { if(line == "") continue; + + auto cmd = ParseCommand(line); + if(cmd == Command::NOOP) + commands.push_back({cmd, ""}); + else + commands.push_back({cmd, line.substr(5)}); } + long cycle = 1; + long x = 1; + for(const auto &command : commands) + { + // Step 1: Increase cycle count of command to see if we would have reported + IncrementCycleCount(cycle, command.first); + + // Step 2: Report, if needed: + total += Report(cycle, x); + + // Step 3: Perform command action: + ApplyAction(command, x); + } + + while(cycle <= 220) + { + IncrementCycleCount(cycle, Command::NOOP); + total += Report(cycle, x); + } + + std::cout << "Ended on cycle #" << cycle << ", value of x: " << x << std::endl; + std::cout << " Total: " << total << std::endl; std::cout << "PT2 Total: " << total_pt2 << std::endl; return 0; } + +void IncrementCycleCount(long &cycle, const Command &command) +{ + switch(command) + { + case Command::NOOP: + cycle++; + break; + case Command::ADDX: + cycle += 2; + break; + default: + std::cerr << "Missing ability to parse command type." << std::endl; + exit(-1); + break; + }; +} + +long Report(const long &cycle, const long &x) +{ + static long last_reported_cycle = -20; + auto next_cycle = last_reported_cycle + 40; + if(cycle > next_cycle) + { + std::cout << "Cycle #" << next_cycle << ": " + << next_cycle << " * " << x << " = " + << next_cycle * x << std::endl; + last_reported_cycle += 40; + + return next_cycle * x; + } + return 0; // no change +} + +void ApplyAction(const std::pair &command, long &x) +{ + switch(command.first) + { + case Command::NOOP: + // std::cout << "NOOP" << std::endl; + // noop + break; + case Command::ADDX: + // std::cout << "ADDX " << command.second << ": x goes from " << x << " to "; + x += std::atoi(command.second.c_str()); + // std::cout << x << std::endl; + break; + default: + std::cerr << "Missing ability to parse command type." << std::endl; + exit(-1); + break; + }; +}