1
0

Finished Day 2 Part 2

This commit is contained in:
2021-12-03 23:08:16 -05:00
parent e6dafb4daa
commit 2162f8a646
4 changed files with 1045 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
#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;
int aim = 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(" ")));
vertical += aim * stoi(line.substr(line.find(" ")));
}
else if (movement == "up")
{
aim -= stoi(line.substr(line.find(" ")));
}
else
{
aim += stoi(line.substr(line.find(" ")));
}
}
std::cout << horizontal * vertical << std::endl;
}