3 changed files with 269 additions and 0 deletions
			
			
		| @ -0,0 +1,118 @@ | |||||
|  | #include <iostream> | ||||
|  | #include <vector> | ||||
|  | 
 | ||||
|  | using namespace std; | ||||
|  | 
 | ||||
|  | enum Opcode { | ||||
|  |     NOOP, | ||||
|  |     ADDX | ||||
|  | }; | ||||
|  | 
 | ||||
|  | struct Instr { | ||||
|  |     Opcode op; | ||||
|  |     int arg; | ||||
|  | }; | ||||
|  | 
 | ||||
|  | unsigned instr_cycles[] = { | ||||
|  |     1,  // NOOP
 | ||||
|  |     2   // ADDX
 | ||||
|  | }; | ||||
|  | 
 | ||||
|  | typedef vector<Instr> Input; | ||||
|  | 
 | ||||
|  | Input parse() | ||||
|  | { | ||||
|  |     Input data; | ||||
|  |     for (string line; getline(cin, line);) { | ||||
|  |         Instr instr; | ||||
|  |         string op = line.substr(0, 5); | ||||
|  |         if (op == "noop") { | ||||
|  |             instr.op = NOOP; | ||||
|  |         } else if (op == "addx ") { | ||||
|  |             instr.op = ADDX; | ||||
|  |             instr.arg = stoi(line.substr(5)); | ||||
|  |         } | ||||
|  |         data.push_back(instr); | ||||
|  |     } | ||||
|  |     return data; | ||||
|  | } | ||||
|  | 
 | ||||
|  | class Cpu { | ||||
|  |     const vector<Instr> *prog; | ||||
|  |     vector<Instr>::const_iterator cur; | ||||
|  |     unsigned cur_cycles = 0; | ||||
|  |     bool run = true; | ||||
|  | 
 | ||||
|  | public: | ||||
|  |     unsigned cycle = 1; | ||||
|  |     int X = 1; | ||||
|  | 
 | ||||
|  |     Cpu(const vector<Instr> *prog) { | ||||
|  |         this->prog = prog; | ||||
|  |         this->cur = prog->begin(); | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     void tick(); | ||||
|  |     bool running() { return run; } | ||||
|  | }; | ||||
|  | 
 | ||||
|  | void Cpu::tick() | ||||
|  | { | ||||
|  |     if (!run) return; | ||||
|  | 
 | ||||
|  |     // Fetch next instruction
 | ||||
|  |     if (!cur_cycles) { | ||||
|  |         cur_cycles = instr_cycles[cur->op]; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     // Execute instruction when done
 | ||||
|  |     if (!--cur_cycles) { | ||||
|  |         switch (cur->op) { | ||||
|  |         case NOOP: | ||||
|  |             break; | ||||
|  | 
 | ||||
|  |         case ADDX: | ||||
|  |             this->X += cur->arg; | ||||
|  |             break; | ||||
|  |         } | ||||
|  |         cur++; | ||||
|  |         if (cur >= prog->end()) run = false; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     cycle++; | ||||
|  | } | ||||
|  | 
 | ||||
|  | unsigned p1(const Input &input) | ||||
|  | { | ||||
|  |     unsigned sum = 0; | ||||
|  |     Cpu cpu(&input); | ||||
|  |     while (cpu.running()) { | ||||
|  |         if ((cpu.cycle + 20) % 40 == 0) sum += cpu.cycle * cpu.X; | ||||
|  |         cpu.tick(); | ||||
|  |     } | ||||
|  |     return sum; | ||||
|  | } | ||||
|  | 
 | ||||
|  | void p2(const Input &input) | ||||
|  | { | ||||
|  |     Cpu cpu(&input); | ||||
|  |     while (cpu.running()) { | ||||
|  |         // Calculate current CRT line
 | ||||
|  |         int X = cpu.cycle % 40; | ||||
|  |         if (X == 0) X += 40; | ||||
|  | 
 | ||||
|  |         // Draw pixel
 | ||||
|  |         if (cpu.X <= X && X <= cpu.X + 2) cout << '#'; | ||||
|  |         else cout << '.'; | ||||
|  |         if (X == 40) cout << endl; | ||||
|  | 
 | ||||
|  |         cpu.tick(); | ||||
|  |     } | ||||
|  | } | ||||
|  | 
 | ||||
|  | int main() | ||||
|  | { | ||||
|  |     auto input = parse(); | ||||
|  |     cout << p1(input) << endl; | ||||
|  |     p2(input); | ||||
|  | } | ||||
| @ -0,0 +1,144 @@ | |||||
|  | noop | ||||
|  | addx 33 | ||||
|  | addx -30 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | addx 1 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 3 | ||||
|  | addx 3 | ||||
|  | addx 3 | ||||
|  | addx -4 | ||||
|  | addx 5 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | noop | ||||
|  | addx 1 | ||||
|  | addx 4 | ||||
|  | noop | ||||
|  | addx 1 | ||||
|  | addx -38 | ||||
|  | noop | ||||
|  | addx 16 | ||||
|  | addx -13 | ||||
|  | addx 2 | ||||
|  | addx 7 | ||||
|  | noop | ||||
|  | addx -2 | ||||
|  | addx -10 | ||||
|  | addx 17 | ||||
|  | addx -5 | ||||
|  | addx 10 | ||||
|  | noop | ||||
|  | addx -15 | ||||
|  | addx 16 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | addx 3 | ||||
|  | addx -2 | ||||
|  | addx 2 | ||||
|  | addx 5 | ||||
|  | addx -38 | ||||
|  | addx 7 | ||||
|  | addx -6 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | noop | ||||
|  | addx 1 | ||||
|  | addx 4 | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 3 | ||||
|  | noop | ||||
|  | addx 3 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | noop | ||||
|  | addx -20 | ||||
|  | addx 21 | ||||
|  | addx 3 | ||||
|  | addx 1 | ||||
|  | addx -35 | ||||
|  | addx 1 | ||||
|  | addx 4 | ||||
|  | noop | ||||
|  | addx 31 | ||||
|  | noop | ||||
|  | addx -26 | ||||
|  | addx 5 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx -2 | ||||
|  | addx 25 | ||||
|  | addx -18 | ||||
|  | addx -13 | ||||
|  | addx 14 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 6 | ||||
|  | addx 1 | ||||
|  | addx 5 | ||||
|  | addx 3 | ||||
|  | addx -2 | ||||
|  | addx -38 | ||||
|  | addx 24 | ||||
|  | addx -17 | ||||
|  | addx 5 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx -2 | ||||
|  | addx 31 | ||||
|  | addx -24 | ||||
|  | addx 7 | ||||
|  | addx -10 | ||||
|  | addx 6 | ||||
|  | noop | ||||
|  | addx 3 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | addx -2 | ||||
|  | addx -26 | ||||
|  | addx 31 | ||||
|  | addx 5 | ||||
|  | addx -40 | ||||
|  | addx 5 | ||||
|  | addx 33 | ||||
|  | addx -31 | ||||
|  | noop | ||||
|  | addx 1 | ||||
|  | addx 4 | ||||
|  | addx 1 | ||||
|  | addx 4 | ||||
|  | addx 20 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx -14 | ||||
|  | addx -1 | ||||
|  | addx 5 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 1 | ||||
|  | addx 2 | ||||
|  | noop | ||||
|  | noop | ||||
|  | addx 7 | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
|  | noop | ||||
| @ -0,0 +1,7 @@ | |||||
|  | 16480 | ||||
|  | ###..#....####.####.#..#.#....###..###.. | ||||
|  | #..#.#....#....#....#..#.#....#..#.#..#. | ||||
|  | #..#.#....###..###..#..#.#....#..#.###.. | ||||
|  | ###..#....#....#....#..#.#....###..#..#. | ||||
|  | #....#....#....#....#..#.#....#....#..#. | ||||
|  | #....####.####.#.....##..####.#....###.. | ||||
					Loading…
					
					
				
		Reference in new issue