mid-kid
3 years ago
5 changed files with 1089 additions and 5 deletions
@ -1 +1 @@ |
|||||
CXXFLAGS := -std=c++17 -O2 -Wall -Wextra |
CXXFLAGS := -std=c++20 -O2 -Wall -Wextra |
||||
|
@ -0,0 +1,79 @@ |
|||||
|
#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; |
||||
|
} |
File diff suppressed because it is too large
@ -0,0 +1,2 @@ |
|||||
|
1524750 |
||||
|
1592426537 |
Loading…
Reference in new issue