diff options
author | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-06 13:05:05 +0100 |
---|---|---|
committer | jacopograndi <jacopo.grandi@outlook.it> | 2022-01-06 13:08:35 +0100 |
commit | fb54beb2bef7fe6b0d4e524b03c61d3f94ffee09 (patch) | |
tree | 0410d0039473ea4c03e77e0be80e1328b9efba08 /day02/day02.cpp | |
parent | 515317921d63720d2d591b6c2c2700ef3a711ad5 (diff) |
init
Diffstat (limited to 'day02/day02.cpp')
-rw-r--r-- | day02/day02.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/day02/day02.cpp b/day02/day02.cpp new file mode 100644 index 0000000..dccb44a --- /dev/null +++ b/day02/day02.cpp @@ -0,0 +1,50 @@ +#include <iostream> + +int main (int argc, char *argv[]) { + if (argc != 2) return 1; + + int depth = 0; + int forward = 0; + + int aim = 0; + int aim_depth = 0; + + FILE *f = fopen(argv[1], "r"); + char c = fgetc(f); + std::string command; + while (c != EOF) { + if (c == '\n') { + auto token_space = command.find(" "); + if (token_space != std::string::npos) { + std::string op = command.substr(0, token_space); + int amt = std::stoi(command.substr(token_space+1)); + if (op == "forward") { + forward += amt; + aim_depth += aim * amt; + } + if (op == "down") { + depth += amt; + aim += amt; + } + if (op == "up") { + depth -= amt; + aim -= amt; + } + } + command = ""; + } + else command += c; + c = fgetc(f); + } + + std::cout << "depth " << depth << ", " + << "forward " << forward << ", " + << "product " << depth * forward << std::endl; + + std::cout << "using aim: depth " << aim_depth << ", " + << "forward " << forward << ", " + << "aim " << aim << ", " + << "product " << aim_depth * forward << std::endl; + + return 0; +} |