1
0
Fork 0

Finished Day 2 Part 2

This commit is contained in:
Pujan 2021-12-03 23:08:16 -05:00
parent e6dafb4daa
commit 2162f8a646
4 changed files with 1045 additions and 0 deletions

View File

@ -10,6 +10,7 @@ int main()
int horizontal = 0; int horizontal = 0;
int vertical = 0; int vertical = 0;
int aim = 0;
if(!input.is_open()) if(!input.is_open())
{ {
@ -23,14 +24,17 @@ int main()
if (movement == "forward") if (movement == "forward")
{ {
horizontal += stoi(line.substr(line.find(" "))); horizontal += stoi(line.substr(line.find(" ")));
vertical += aim * stoi(line.substr(line.find(" ")));
} }
else if (movement == "up") else if (movement == "up")
{ {
vertical -= stoi(line.substr(line.find(" "))); vertical -= stoi(line.substr(line.find(" ")));
aim -= stoi(line.substr(line.find(" ")));
} }
else else
{ {
vertical += stoi(line.substr(line.find(" "))); vertical += stoi(line.substr(line.find(" ")));
aim += stoi(line.substr(line.find(" ")));
} }
} }

1000
Day 2/Pujan/Part 2/data.txt Normal file

File diff suppressed because it is too large Load Diff

BIN
Day 2/Pujan/Part 2/main Executable file

Binary file not shown.

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