Day 3 WIP

This commit is contained in:
limb 2021-12-03 16:19:48 +00:00
parent 274e026447
commit 2c7e21b098
7 changed files with 1114 additions and 4 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.12)
# set the project name # set the project name
project(AdventofCode2021) project(AdventofCode2021)
@ -9,3 +9,4 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
add_subdirectory(day1) add_subdirectory(day1)
add_subdirectory(day2) add_subdirectory(day2)
add_subdirectory(day3)

View File

@ -1,5 +1,5 @@
# set the project name # set the project name
project(d1p1) project(day2)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

View File

@ -1,5 +1,5 @@
# set the project name # set the project name
project(d2p1) project(day1)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

9
day3/CMakeLists.txt Normal file
View File

@ -0,0 +1,9 @@
# set the project name
project(day3)
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/input
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
# add the executable
add_executable(d3p1 part1/main.cpp)
add_executable(d3p2 part2/main.cpp)

1000
day3/input Normal file

File diff suppressed because it is too large Load Diff

57
day3/part1/main.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
cout << "Advent of Code 2021 - Day 3 Part 1" << endl;
unsigned char gamma[12] = {0};
unsigned char epsilon[12] = {0};
vector<vector<char>> inputBits;
ifstream filein("input");
for(string line; getline(filein, line);) {
vector<char> bits;
for(int i = 0; i < 12; i++) {
if (line[i] == '1') {
bits.push_back(1);
}
else {
bits.push_back(0);
}
}
inputBits.push_back(bits);
}
for(int x = 0; x < 12; x++) {
int greater = 0;
for(int i = 0; i < inputBits.size(); i++) {
if(inputBits[i][x] == 1) {
greater++;
}
}
if(greater > (inputBits.size() / 2)) {
gamma[x] = 1;
epsilon[x] = 0;
} else {
gamma[x] = 0;
epsilon[x] = 1;
}
}
int asd = 0;
for(int x = 0; x < 12; x++) {
asd = asd << (int)gamma[x];
}
cout << asd << endl;
cout << epsilon << endl;
return 0;
}

43
day3/part2/main.cpp Normal file
View 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;
}