Part 1 complete.

This commit is contained in:
David Vereb 2022-12-20 15:10:39 -05:00
parent 03b991a582
commit 37c1fb045f
2 changed files with 251 additions and 0 deletions

146
2022/10/larger.txt Normal file
View File

@ -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

View File

@ -4,6 +4,29 @@
#include <string>
#include <vector>
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, std::string> &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<std::pair<Command, std::string> > 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, std::string> &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;
};
}