This commit is contained in:
zv0n 2022-12-10 08:54:27 +01:00
parent 9161b5ca8d
commit 0303b2e9cd
3 changed files with 315 additions and 0 deletions

16
2022/10/Makefile Normal file
View File

@ -0,0 +1,16 @@
CXX ?= c++
CXXFLAGS ?= -std=c++11 -Wall -Wextra -pedantic -O2
PROJECT = CRT
all: ${PROJECT}
${PROJECT}: main.cpp
${CXX} ${CXXFLAGS} -o $@ $^
test: ${PROJECT}
./${PROJECT}
clean:
${RM} *.o ${PROJECT}
.PHONY: all clean test

140
2022/10/input Normal file
View File

@ -0,0 +1,140 @@
addx 1
addx 4
addx 21
addx -20
addx 4
noop
noop
addx 5
addx 3
noop
addx 2
addx 1
noop
noop
addx 4
noop
noop
noop
addx 3
addx 5
addx 2
addx 1
noop
addx -37
addx 22
addx -4
addx -14
addx 2
addx 5
addx 3
addx -2
addx 2
addx 5
addx 2
addx -15
addx 32
addx -14
addx 5
addx 2
addx 3
noop
addx -13
addx -2
addx 18
addx -36
noop
addx 11
addx -7
noop
noop
addx 6
addx 22
addx -21
addx 3
addx 2
addx 4
noop
noop
noop
addx 5
addx -16
addx 17
addx 2
addx 5
addx -11
addx 15
addx -15
addx -24
noop
noop
addx 7
addx 2
addx -6
addx 9
noop
addx 5
noop
addx -3
addx 4
addx 2
noop
noop
addx 7
noop
noop
noop
addx 5
addx -28
addx 29
noop
addx 3
addx -7
addx -29
noop
addx 7
addx -2
addx 2
addx 5
addx 2
addx -3
addx 4
addx 5
addx 2
addx 8
addx -30
addx 25
addx 7
noop
noop
addx 3
addx -2
addx 2
addx -10
addx -24
addx 2
noop
noop
addx 2
noop
addx 3
addx 2
noop
addx 3
addx 2
addx 5
addx 2
noop
addx 1
noop
addx 2
addx 8
noop
noop
addx -1
addx -9
addx 14
noop
addx 1
noop
noop

159
2022/10/main.cpp Normal file
View File

@ -0,0 +1,159 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <chrono>
#include <thread>
enum InstructionType {
NOOP,
ADDX,
};
struct Instruction {
InstructionType instruction;
int parameter;
};
class CPU {
public:
CPU(const std::vector<Instruction> &instructions): instructions(instructions) {}
void reset() {
working_instructions = instructions;
remaining_program = getProgramCycles();
remaining_cycles = 0;
reg_x = 1;
current_instruction = {NOOP, 0};
}
int getRemainingInstructions() {
return remaining_program;
}
void performCycle() {
if(remaining_cycles == 0) {
if(current_instruction.instruction == ADDX) {
reg_x += current_instruction.parameter;
}
current_instruction = working_instructions.back();
working_instructions.pop_back();
remaining_cycles = getInstructionCycles(current_instruction);
}
remaining_cycles--;
remaining_program--;
}
int getX() {
return reg_x;
}
private:
int getProgramCycles() {
int result = 0;
for(auto &instruction : working_instructions) {
result += getInstructionCycles(instruction);
}
return result;
}
int getInstructionCycles(const Instruction &instruction) {
switch(instruction.instruction) {
case NOOP:
return 1;
case ADDX:
return 2;
}
}
int reg_x;
Instruction current_instruction;
int remaining_cycles;
int remaining_program;
std::vector<Instruction> working_instructions;
std::vector<Instruction> instructions;
};
CPU getCPU( std::ifstream &file ) {
std::vector<Instruction> instructions{};
std::string str;
int parameter;
while ( std::getline( file, str ) ) {
if(str[0] == 'n') {
instructions.push_back({NOOP, 0});
} else {
std::stringstream ss( str.substr(4) );
ss >> parameter;
instructions.push_back( {ADDX, parameter} );
}
}
std::reverse(instructions.begin(), instructions.end());
return CPU(instructions);
}
int part1(CPU &cpu) {
int result = 0;
cpu.reset();
for(int i = 0; i < 20; i++) {
cpu.performCycle();
}
result += 20 * cpu.getX();
int cycles = 0;
while(true) {
cycles++;
cpu.performCycle();
if(cycles % 40 == 0) {
result += (20+cycles) * cpu.getX();
if(cpu.getRemainingInstructions() < 40) {
break;
}
}
}
return result;
}
void part2(CPU &cpu) {
cpu.reset();
int x = 0;
for(int i = 0; i < 240; i++) {
cpu.performCycle();
if(cpu.getX() >= x - 1 && cpu.getX() <= x + 1) {
std::cout << '#';
} else {
std::cout << ' ';
}
x++;
if(x == 40) {
x = 0;
std::cout << std::endl;
}
}
}
void visualize(CPU &cpu) {
cpu.reset();
int x = 0;
std::cout << "\033[?25l";
std::cout << "" << std::flush;
for(int i = 0; i < 240; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(25));
cpu.performCycle();
if(cpu.getX() >= x - 1 && cpu.getX() <= x + 1) {
std::cout << "\010";
} else {
std::cout << "\010 ";
}
x++;
if(x == 40) {
x = 0;
std::cout << std::endl;
}
std::cout << "" << std::flush;
}
std::cout << "\010\033[?25h" << std::flush;
}
int main() {
std::ifstream input_file( "input" );
auto cpu = getCPU( input_file );
std::cout << "Part 1 result is \033[91;1m" << part1(cpu)
<< "\033[0m." << std::endl;
std::cout << "The CRT provides the following image:" << std::endl;
std::cout << "\033[91;1m";
part2(cpu);
std::cout << "\033[0m";
// visualize(cpu);
}