Browse Source

Add day 2

master
mid-kid 3 years ago
parent
commit
b0a39f2a03
  1. 2
      Makefile
  2. 11
      d01.cc
  3. 79
      d02.cc
  4. 1000
      d02_input.txt
  5. 2
      d02_output.txt

2
Makefile

@ -1 +1 @@
CXXFLAGS := -std=c++17 -O2 -Wall -Wextra
CXXFLAGS := -std=c++20 -O2 -Wall -Wextra

11
d01.cc

@ -5,12 +5,15 @@ using namespace std;
int main(int argc, char *argv[]) {
if (argc <= 1) return 1;
ifstream input(argv[1]);
vector<unsigned> numbers;
unsigned num;
while (input >> num) {
numbers.push_back(num);
{
ifstream input(argv[1]);
if (!input.is_open()) return 1;
unsigned num;
while (input >> num) {
numbers.push_back(num);
}
}
unsigned part1 = 0;

79
d02.cc

@ -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;
}

1000
d02_input.txt

File diff suppressed because it is too large

2
d02_output.txt

@ -0,0 +1,2 @@
1524750
1592426537
Loading…
Cancel
Save