adventofcode2021/day2/part1/main.cpp
2021-12-02 14:43:30 +00:00

42 lines
919 B
C++

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
struct Command {
string direction;
int distance;
};
int main() {
cout << "Advent of Code 2021 - Day 2 Part 1" << endl;
int x, y = 0;
ifstream filein("input");
vector<Command> inputCommands;
for(string line; getline(filein, line);) {
int space = line.find(" ");
inputCommands.push_back(Command {line.substr(0, space), stoi(line.substr(space+1))});
}
for(Command cmd : inputCommands) {
if(cmd.direction == "forward") {
x += cmd.distance;
}
else if (cmd.direction == "down") {
y += cmd.distance;
}
else if(cmd.direction == "up") {
y -= cmd.distance;
}
else {
cout << "Uh I shouldn't get here in the for cmd loop" << endl;
}
}
cout << x * y << endl;
return 0;
}