Day 2
This commit is contained in:
parent
92d177e678
commit
274e026447
11
CMakeLists.txt
Normal file
11
CMakeLists.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# set the project name
|
||||
project(AdventofCode2021)
|
||||
|
||||
# specify the C++ standard
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
add_subdirectory(day1)
|
||||
add_subdirectory(day2)
|
||||
9
day2/CMakeLists.txt
Normal file
9
day2/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# set the project name
|
||||
project(d2p1)
|
||||
|
||||
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input
|
||||
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
# add the executable
|
||||
add_executable(d2p1 part1/main.cpp)
|
||||
add_executable(d2p2 part2/main.cpp)
|
||||
1000
day2/input
Normal file
1000
day2/input
Normal file
File diff suppressed because it is too large
Load Diff
42
day2/part1/main.cpp
Normal file
42
day2/part1/main.cpp
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Command {
|
||||
string direction;
|
||||
int distance;
|
||||
};
|
||||
|
||||
int main() {
|
||||
cout << "Advent of Code 2021 - Day 2 Part 1" << endl;
|
||||
|
||||
int x, y = 0;
|
||||
|
||||
ifstream filein("input");
|
||||
vector<Command> inputCommands;
|
||||
for(string line; getline(filein, line);) {
|
||||
int space = line.find(" ");
|
||||
inputCommands.push_back(Command {line.substr(0, space), stoi(line.substr(space+1))});
|
||||
}
|
||||
|
||||
for(Command cmd : inputCommands) {
|
||||
if(cmd.direction == "forward") {
|
||||
x += cmd.distance;
|
||||
}
|
||||
else if (cmd.direction == "down") {
|
||||
y += cmd.distance;
|
||||
}
|
||||
else if(cmd.direction == "up") {
|
||||
y -= cmd.distance;
|
||||
}
|
||||
else {
|
||||
cout << "Uh I shouldn't get here in the for cmd loop" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << x * y << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
43
day2/part2/main.cpp
Normal file
43
day2/part2/main.cpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Command {
|
||||
string direction;
|
||||
int distance;
|
||||
};
|
||||
|
||||
int main() {
|
||||
cout << "Advent of Code 2021 - Day 2 Part 2" << endl;
|
||||
|
||||
int x, y, aim = 0;
|
||||
|
||||
ifstream filein("input");
|
||||
vector<Command> inputCommands;
|
||||
for(string line; getline(filein, line);) {
|
||||
int space = line.find(" ");
|
||||
inputCommands.push_back(Command {line.substr(0, space), stoi(line.substr(space+1))});
|
||||
}
|
||||
|
||||
for(Command cmd : inputCommands) {
|
||||
if(cmd.direction == "forward") {
|
||||
x += cmd.distance;
|
||||
y += cmd.distance * aim;
|
||||
}
|
||||
else if (cmd.direction == "down") {
|
||||
aim += cmd.distance;
|
||||
}
|
||||
else if(cmd.direction == "up") {
|
||||
aim -= cmd.distance;
|
||||
}
|
||||
else {
|
||||
cout << "Uh I shouldn't get here in the for cmd loop" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << x * y << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user