From 0303b2e9cd5244117595f90f118fb9d134579834 Mon Sep 17 00:00:00 2001 From: zv0n Date: Sat, 10 Dec 2022 08:54:27 +0100 Subject: [PATCH] 10 --- 2022/10/Makefile | 16 +++++ 2022/10/input | 140 +++++++++++++++++++++++++++++++++++++++++ 2022/10/main.cpp | 159 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 315 insertions(+) create mode 100644 2022/10/Makefile create mode 100644 2022/10/input create mode 100644 2022/10/main.cpp diff --git a/2022/10/Makefile b/2022/10/Makefile new file mode 100644 index 0000000..deec922 --- /dev/null +++ b/2022/10/Makefile @@ -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 diff --git a/2022/10/input b/2022/10/input new file mode 100644 index 0000000..bbc6bcd --- /dev/null +++ b/2022/10/input @@ -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 diff --git a/2022/10/main.cpp b/2022/10/main.cpp new file mode 100644 index 0000000..0db09b5 --- /dev/null +++ b/2022/10/main.cpp @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include + +enum InstructionType { + NOOP, + ADDX, +}; + +struct Instruction { + InstructionType instruction; + int parameter; +}; + +class CPU { +public: + CPU(const std::vector &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 working_instructions; + std::vector instructions; +}; + +CPU getCPU( std::ifstream &file ) { + std::vector 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); +}