Finished Day 2 Part 2
This commit is contained in:
parent
e6dafb4daa
commit
2162f8a646
|
@ -10,6 +10,7 @@ int main()
|
|||
|
||||
int horizontal = 0;
|
||||
int vertical = 0;
|
||||
int aim = 0;
|
||||
|
||||
if(!input.is_open())
|
||||
{
|
||||
|
@ -23,14 +24,17 @@ int main()
|
|||
if (movement == "forward")
|
||||
{
|
||||
horizontal += stoi(line.substr(line.find(" ")));
|
||||
vertical += aim * stoi(line.substr(line.find(" ")));
|
||||
}
|
||||
else if (movement == "up")
|
||||
{
|
||||
vertical -= stoi(line.substr(line.find(" ")));
|
||||
aim -= stoi(line.substr(line.find(" ")));
|
||||
}
|
||||
else
|
||||
{
|
||||
vertical += stoi(line.substr(line.find(" ")));
|
||||
aim += stoi(line.substr(line.find(" ")));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
@ -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;
|
||||
|
||||
}
|
Loading…
Reference in New Issue