You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.7 KiB
80 lines
1.7 KiB
3 years ago
|
#include <iostream>
|
||
|
#include <fstream>
|
||
|
#include <vector>
|
||
|
using namespace std;
|
||
|
|
||
|
enum cmd {
|
||
|
CMD_FORWARD,
|
||
|
CMD_DOWN,
|
||
|
CMD_UP,
|
||
|
};
|
||
|
|
||
|
struct commands {
|
||
|
enum cmd cmd;
|
||
|
int pos;
|
||
|
};
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
if (argc <= 1) return 1;
|
||
|
|
||
|
vector<struct commands> commands;
|
||
|
{
|
||
|
ifstream input(argv[1]);
|
||
|
if (!input.is_open()) return 1;
|
||
|
for (string line; getline(input, line);) {
|
||
|
struct commands command;
|
||
|
|
||
|
unsigned split = line.find(' ');
|
||
|
string cmd = line.substr(0, split);
|
||
|
line.erase(0, split + 1);
|
||
|
if (cmd == "forward") {
|
||
|
command.cmd = CMD_FORWARD;
|
||
|
} else if (cmd == "down") {
|
||
|
command.cmd = CMD_DOWN;
|
||
|
} else if (cmd == "up") {
|
||
|
command.cmd = CMD_UP;
|
||
|
}
|
||
|
command.pos = stoi(line);
|
||
|
commands.push_back(command);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int pos_h = 0;
|
||
|
int pos_d = 0;
|
||
|
for (struct commands command : commands) {
|
||
|
switch (command.cmd) {
|
||
|
case CMD_FORWARD:
|
||
|
pos_h += command.pos;
|
||
|
break;
|
||
|
case CMD_DOWN:
|
||
|
pos_d += command.pos;
|
||
|
break;
|
||
|
case CMD_UP:
|
||
|
pos_d -= command.pos;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cout << pos_h * pos_d << endl;
|
||
|
|
||
|
int aim = 0;
|
||
|
pos_h = 0;
|
||
|
pos_d = 0;
|
||
|
for (struct commands command : commands) {
|
||
|
switch (command.cmd) {
|
||
|
case CMD_FORWARD:
|
||
|
pos_h += command.pos;
|
||
|
pos_d += aim * command.pos;
|
||
|
break;
|
||
|
case CMD_DOWN:
|
||
|
aim += command.pos;
|
||
|
break;
|
||
|
case CMD_UP:
|
||
|
aim -= command.pos;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cout << pos_h * pos_d << endl;
|
||
|
}
|