Day 3 WIP
This commit is contained in:
43
day3/part2/main.cpp
Normal file
43
day3/part2/main.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#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 2" << endl;
|
||||
|
||||
int x, y, aim = 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;
|
||||
y += cmd.distance * aim;
|
||||
}
|
||||
else if (cmd.direction == "down") {
|
||||
aim += cmd.distance;
|
||||
}
|
||||
else if(cmd.direction == "up") {
|
||||
aim -= cmd.distance;
|
||||
}
|
||||
else {
|
||||
cout << "Uh I shouldn't get here in the for cmd loop" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << x * y << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user