Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ __pycache__/
# C extensions
*.so

*.o
_codeql_*
89 changes: 44 additions & 45 deletions aoc_cpp/2016/day1/main.cpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,67 @@
#include <iostream>
#include <fstream>
#include <iostream>

int direction = 0; //Start facing north
int current_position[2] = {0, 0}; //At position x,y=0,0
int direction = 0; // Start facing north
int current_position[2] = {0, 0}; // At position x,y=0,0

void walk(int distance) {

switch (direction) {
case 0:
current_position[1] += distance; //North
break;
case 1:
current_position[0] += distance; //East
break;
case 2:
current_position[1] -= distance; //South
break;
case 3:
current_position[0] -= distance; // West
break;
}
//std::cout << "Walking " << distance << " in direction " << direction << std::endl;

switch (direction) {
case 0:
current_position[1] += distance; // North
break;
case 1:
current_position[0] += distance; // East
break;
case 2:
current_position[1] -= distance; // South
break;
case 3:
current_position[0] -= distance; // West
break;
}
// std::cout << "Walking " << distance << " in direction " << direction <<
// std::endl;
}

void turn(std::string inDirection) {

if (inDirection == "L") {
direction--;
} else if (inDirection == "R") {
direction++;
}
if (inDirection == "L") {
direction--;
} else if (inDirection == "R") {
direction++;
}

if (direction < 0) {
direction = 3;
}

direction %= 4;
//std::cout << inDirection << " -> "<< direction << std::endl;
if (direction < 0) {
direction = 3;
}

direction %= 4;
// std::cout << inDirection << " -> "<< direction << std::endl;
}

int main() {

std::ifstream file("2016/day1/input.txt");
std::string instruction;

while (getline(file, instruction, ',')) {

instruction.erase(std::remove(instruction.begin(), instruction.end(), ' '), instruction.end());
std::ifstream file("2016/day1/input.txt");
std::string instruction;

std::string direction = instruction.substr(0, 1);
turn(direction);
while (getline(file, instruction, ',')) {

int distance = std::stoi(instruction.substr(1, instruction.length()));
walk(distance);
instruction.erase(std::remove(instruction.begin(), instruction.end(), ' '),
instruction.end());

}
std::string direction = instruction.substr(0, 1);
turn(direction);

std::cout << "Current position: (x,y)=(" << current_position[0] << "," << current_position[1] << ")" << std::endl;
int distance = std::stoi(instruction.substr(1, instruction.length()));
walk(distance);
}

int solution = std::abs(current_position[0]) + std::abs(current_position[1]);
std::cout << "Solution: " << solution << std::endl;
std::cout << "Current position: (x,y)=(" << current_position[0] << ","
<< current_position[1] << ")" << std::endl;

return 0;
int solution = std::abs(current_position[0]) + std::abs(current_position[1]);
std::cout << "Solution: " << solution << std::endl;

return 0;
}
130 changes: 60 additions & 70 deletions aoc_cpp/2016/day12/main.cpp
Original file line number Diff line number Diff line change
@@ -1,112 +1,102 @@
#include <iostream>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <map>

/*
* https://stackoverflow.com/questions/2844817/how-do-i-check-if-a-c-string-is-an-int
*/
inline bool isInteger(const std::string &s) {
if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+')))
return false;

char *p;
std::strtol(s.c_str(), &p, 10);
char *p;
std::strtol(s.c_str(), &p, 10);

return (*p == 0);
return (*p == 0);
}

class Registers {

private:
std::map<std::string, int> registers;
std::map<std::string, int> registers;

public:

Registers() {
registers["a"] = 0;
registers["b"] = 0;
registers["c"] = 0;
registers["d"] = 0;
Registers() {
registers["a"] = 0;
registers["b"] = 0;
registers["c"] = 0;
registers["d"] = 0;
}

void cpy(std::string input, std::string toRegister) {
if (isInteger(input)) {
registers[toRegister] = std::stoi(input);
} else {
registers[toRegister] = registers[input];
}
}

void cpy(std::string input, std::string toRegister) {
if (isInteger(input)) {
registers[toRegister] = std::stoi(input);
} else {
registers[toRegister] = registers[input];
}
}
void inc(std::string reg) { registers[reg] = registers[reg] + 1; }

void inc(std::string reg) {
registers[reg] = registers[reg] + 1;
}
void dec(std::string reg) { registers[reg] = registers[reg] - 1; }

void dec(std::string reg) {
registers[reg] = registers[reg] - 1;
void show() {
std::cout << "Registers" << std::endl;
for (auto elem : registers) {
std::cout << elem.first << " " << elem.second << "\n";
}
}

void show() {
std::cout << "Registers" << std::endl;
for (auto elem : registers) {
std::cout << elem.first << " " << elem.second << "\n";
}
}

int getValue(std::string reg) {
return registers[reg];
}
int getValue(std::string reg) { return registers[reg]; }
};


int main() {

std::vector<std::vector<std::string>> rows;

std::ifstream file("2016/day12/input.txt");
for (std::string line; getline(file, line);) {
std::vector<std::vector<std::string>> rows;

std::vector<std::string> instructionParts;
std::ifstream file("2016/day12/input.txt");
for (std::string line; getline(file, line);) {

std::istringstream f(line);
std::string column;
std::vector<std::string> instructionParts;

while (std::getline(f, column, ' ')) {
instructionParts.push_back(column);
}
std::istringstream f(line);
std::string column;

rows.push_back(instructionParts);
while (std::getline(f, column, ' ')) {
instructionParts.push_back(column);
}

Registers registers;
for (int i = 0; i < rows.size(); ++i) {
rows.push_back(instructionParts);
}

std::string instruction = rows[i][0];
Registers registers;
for (int i = 0; i < rows.size(); ++i) {

if (instruction == "cpy") {
registers.cpy(rows[i][1], rows[i][2]);
} else if (instruction == "inc") {
registers.inc(rows[i][1]);
} else if (instruction == "dec") {
registers.dec(rows[i][1]);
} else if (instruction == "jnz") {
std::string instruction = rows[i][0];

std::string reg = rows[i][1];
std::string relativeJump = rows[i][2];
if (instruction == "cpy") {
registers.cpy(rows[i][1], rows[i][2]);
} else if (instruction == "inc") {
registers.inc(rows[i][1]);
} else if (instruction == "dec") {
registers.dec(rows[i][1]);
} else if (instruction == "jnz") {

//The reg cant be "0" if its numeric
//And if its a register the content cant be 0
if ((isInteger(reg) && reg != "0") ||
registers.getValue(reg) != 0) {
std::string reg = rows[i][1];
std::string relativeJump = rows[i][2];

//Move the position forward, relative to current iteration
i += std::stoi(relativeJump) - 1;
}
// The reg cant be "0" if its numeric
// And if its a register the content cant be 0
if ((isInteger(reg) && reg != "0") || registers.getValue(reg) != 0) {

}
// Move the position forward, relative to current iteration
i += std::stoi(relativeJump) - 1;
}
}
registers.show();

return 0;
}
registers.show();

return 0;
}
Loading