Skip to content

Commit

Permalink
Add 2017 day 23 cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
vss2sn committed Nov 4, 2023
1 parent 027357f commit 0c90d4d
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 2017/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This folder contains solutions to each of the problems in Advent of Code 2017 in
#### Notes ####
* There are multiple sample puzzle inputs for some days. Modify the sample input file appropriately before using it.
* There are places where the sample input and input have different parameters (for e.g. day 16, where n = 5 and 16 respectively and day 18, where iterations = 2 and 5(18) respectively). Modify the .cpp file appropriately before using it.
* Note that some days (like 23) do not have a sample input

|Puzzle|C++ Solutions|Input|Sample Input|Puzzle page with solutions|
|:---:|:---:|:---:|:---:|:---:|
Expand All @@ -30,12 +31,12 @@ This folder contains solutions to each of the problems in Advent of Code 2017 in
| <nobr> [Day 20: Particle Swarm](https://adventofcode.com/2017/day/20) </nobr> | <nobr> [Part 1](/2017/cpp/day_20a.cpp) [Part 2](/2017/cpp/day_20b.cpp) </nobr> |[Link](/2017/input/day_20_input)|[Link](/2017/sample_input/day_20_sample_input)|[Link](/2017/puzzles/day_20_puzzle)|
| <nobr> [Day 21: Fractal Art](https://adventofcode.com/2017/day/21) </nobr> | <nobr> [Part 1](/2017/cpp/day_21a.cpp) [Part 2](/2017/cpp/day_21b.cpp) </nobr> |[Link](/2017/input/day_21_input)|[Link](/2017/sample_input/day_21_sample_input)|[Link](/2017/puzzles/day_21_puzzle)|
| <nobr> [Day 22: Sporifica Virus](https://adventofcode.com/2017/day/22) </nobr> | <nobr> [Part 1](/2017/cpp/day_22a.cpp) [Part 2](/2017/cpp/day_22b.cpp) </nobr> |[Link](/2017/input/day_22_input)|[Link](/2017/sample_input/day_22_sample_input)|[Link](/2017/puzzles/day_22_puzzle)|
| <nobr> [Day 23: Coprocessor Conflagration](https://adventofcode.com/2017/day/23) </nobr> | <nobr> [Part 1](/2017/cpp/day_23a.cpp) [Part 2](/2017/cpp/day_23b.cpp) </nobr> |[Link](/2017/input/day_23_input)|[Link](/2017/sample_input/day_23_sample_input)|[Link](/2017/puzzles/day_23_puzzle)|

#### TODO ####

|Puzzle|C++ Solutions|Input|Sample Input|Puzzle page with solutions|
|:---:|:---:|:---:|:---:|:---:|
| <nobr> [Day 23: ](https://adventofcode.com/2017/day/23) </nobr> | <nobr> [Part 1](/2017/cpp/day_23a.cpp) [Part 2](/2017/cpp/day_23b.cpp) </nobr> |[Link](/2017/input/day_23_input)|[Link](/2017/sample_input/day_23_sample_input)|[Link](/2017/puzzles/day_23_puzzle)|
| <nobr> [Day 24: ](https://adventofcode.com/2017/day/24) </nobr> | <nobr> [Part 1](/2017/cpp/day_24a.cpp) [Part 2](/2017/cpp/day_24b.cpp) </nobr> |[Link](/2017/input/day_24_input)|[Link](/2017/sample_input/day_24_sample_input)|[Link](/2017/puzzles/day_24_puzzle)|

| <nobr> [Day 25: ](https://adventofcode.com/2017/day/25) </nobr> | <nobr> [Part 1](/2017/cpp/day_25a.cpp) </nobr> | </nobr> |[Link](/2017/input/day_25_input)|[Link](/2017/sample_input/day_25_sample_input)|[Link](/2017/puzzles/day_25_puzzle)|
124 changes: 124 additions & 0 deletions 2017/cpp/day_23a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <variant>
#include <queue>

// HINT: When the puzzle says X or Y, unless it specifies register, X or Y can be a number
// JNZ 1 3 is a possibility
// add p b is a possibility

enum class InstructionType{
UNKNOWN, SND, SUB, SET, ADD, MUL, MOD, RCV, JGZ
};

struct Instruction {
std::string line;
InstructionType type;
std::array<std::variant<long long, char>,2> args;

long long reg() const {
return std::get<char>(args[0]);
}

long long get_value (const int idx, std::unordered_map<char, long long>& regs) const {
return args[idx].index() == 0 ? std::get<long long>(args[idx]) : regs[std::get<char>(args[idx])];
}
};

InstructionType parse_type(const std::string& instr) {
if (instr == "set") {
return InstructionType::SET;
} else if (instr == "sub") {
return InstructionType::SUB;
} else if (instr == "mul") {
return InstructionType::MUL;
} else if (instr == "jnz") {
return InstructionType::JGZ;
}
std::cout << "UNKNOWN" << '\n';
exit(0);
return InstructionType::UNKNOWN;
}

std::vector<Instruction> parse_instructions (std::ifstream& file) {
std::vector<Instruction> instructions;
std::string line;
while(std::getline(file, line)) {
// std::cout << line << '\n';
Instruction i;
i.line = line;
i.type = parse_type(line.substr(0, 3));
int next = 0;
if (line[4] >= 'a' && line[4] <='z') {
std::cout << "Args 1 is a register: " << char(line[4]) <<'\n';
i.args[0] = char(line[4]);
next = 6;
} else {
next = line.find(' ', 4);
if (next == std::string::npos) {
// std::cout << "npos" << '\n';
next == line.size();
}
std::cout << "Args 1 is a number: " << std::stoi(line.substr(4, next - 4)) << '\n';
i.args[0] = std::stoi(line.substr(4, next - 4));
if (next != std::string::npos) {
next++;
}
}
// std::cout << line.size() << ' ' << next << '\n';
if (line.size() >= next) {
// std::cout << "Second args exists; size == " << line.size() << " > " << next << '\n';
if (line[next] >= 'a' && line[next] <='z') {
i.args[1] = char(line[next]);
std::cout << "Args 2 is a register: " << char(line[next]) <<'\n';
} else {
std::cout << "Args 2 is a number: " << std::stoi(line.substr(next, line.size() - next)) << '\n';
i.args[1] = std::stoi(line.substr(next, line.size() - next));
}
}

instructions.push_back(i);
}
// exit(0);
return instructions;
}

int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_18_input" ;
std::ifstream file(input);
const std::vector<Instruction> instructions = parse_instructions(file);
std::unordered_map<char, long long> regs;
for (char c = 'a'; c <= 'h'; c++) {
regs[c] = 0;
}
std::size_t ans = 0;
std::size_t instruction_ptr = 0;
while (instruction_ptr < instructions.size()) {
const auto& instruction = instructions[instruction_ptr];
if (instruction.type == InstructionType::SET) {
regs[instruction.reg()] = instruction.get_value(1, regs);
instruction_ptr++;
} else if (instruction.type == InstructionType::SUB) {
regs[instruction.reg()] -= instruction.get_value(1, regs);
instruction_ptr++;
} else if (instruction.type == InstructionType::MUL) {
regs[instruction.reg()] *= instruction.get_value(1, regs);
instruction_ptr++;
ans++;
} else if (instruction.type == InstructionType::JGZ) {
if(instruction.get_value(0, regs) != 0) {
instruction_ptr += instruction.get_value(1, regs);
} else {
instruction_ptr++;
}
} else {
std::cout << "UNKNOWN" << '\n';
exit(0);
}
}
std::cout << ans << '\n';
return 0;
}
76 changes: 76 additions & 0 deletions 2017/cpp/day_23b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <vector>

int main(int argc, char * argv[]) {
constexpr int b = 108100;
constexpr int c = 125100;
std::vector<bool> primes(c, true);
for (int i = 2; i * i <= c; i++) {
if (primes[i]) {
for (int j = i * i; j <= c; j+= i) {
primes[j] = false;
}
}
}

int ans = 0;
for (int i = b; i <= c; i+=17) {
if (!primes[i]) ans++;
}
std::cout << ans << '\n';
return 0;
}


// Instructions converted to code:
// int main(int argc, char * argv[]) {
// int count = 0;
// int a = 1;
// int b = 0;
// int c = 0;
// int d = 0;
// int e = 0;
// int f = 0;
// int g = 0;
// int h = 0;
// b = 108100;
// c = 125100;
//
// while(true) {
// f=1;
// d=2;
// do {
// e = 2;
// do {
// g = d;
// g = g * e;
// g = g - b;
// if (g == 0) {
// f = 0;
// }
// e -= - 1;
// g = e;
// g = g - b;
//
// } while( g != 0);
// d -= -1;
// g = d;
// g -= b;
// } while (g != 0);
// if (f == 0) {
// h -= -1;
// }
// g = b;
// g -= c;
// if(g == 0) {
// std::cout << h << '\n';
// exit(0);
// }
// b -= -17;
// }
// std::cout << h << '\n';
// }

// Note that e and d both start of as 2 and the minimum value of b is 108100
// The while loops become for loops from 2 to b
// The outermost loop is indexed by b and runs from the initial value of b to c.
Empty file.

0 comments on commit 0c90d4d

Please sign in to comment.