-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.