1
0
Fork 0
Advent2021/Day 2/Pujan/Part 1/main.cpp

40 lines
832 B
C++

#include<iostream>
#include <stdlib.h>
#include <fstream>
int main()
{
std::string filename("data.txt");
std::ifstream input{filename};
std::string line;
int horizontal = 0;
int vertical = 0;
if(!input.is_open())
{
std::cerr << "Couldn't read file: " << filename << "\n";
return 1;
}
while(std::getline(input,line))
{
std::string movement = line.substr(0, line.find(" "));
if (movement == "forward")
{
horizontal += stoi(line.substr(line.find(" ")));
}
else if (movement == "up")
{
vertical -= stoi(line.substr(line.find(" ")));
}
else
{
vertical += stoi(line.substr(line.find(" ")));
}
}
std::cout << horizontal * vertical << std::endl;
}