-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.cpp
67 lines (59 loc) · 1.65 KB
/
day10.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Created by Johannes Loepelmann on 10.12.22.
//
#include "util.h"
enum Command {
NOOP, ADDX
};
struct Instruction {
Command com;
int arg;
explicit Instruction(const std::string& input) {
switch(input[0]) {
case 'n':
com = NOOP;
break;
case 'a':
com = ADDX;
arg = std::stoi(input.substr(4));
break;
}
}
};
int state_at(const std::vector<Instruction>& program, int cycle) {
int current_ins = 0;
int current_cycle = 0;
int x = 1;
while(current_cycle < cycle) {
const auto& ins = program[current_ins];
if(ins.com == NOOP) {
current_ins++;
current_cycle++;
} else if (ins.com == ADDX) {
if(current_cycle+2 >= cycle)
break;
current_ins++;
current_cycle+=2;
x += ins.arg;
}
}
return x;
}
int main() {
std::vector<Instruction> program;
for(const auto& line : read_strings_from_file("day10.input")){
program.emplace_back(line);
}
std::cout << "Result: " << 20 * state_at(program, 20) + 60 * state_at(program, 60) +100 * state_at(program, 100) +140 * state_at(program, 140) +180 * state_at(program, 180) + 220 * state_at(program, 220) << std::endl;
for(int row = 0; row < 6; ++row) {
for(int col = 0; col < 40; ++col) {
auto x = state_at(program, row*40+col+1);
if(abs(col-x) < 2){
std::cout << "██";
} else {
std::cout << "░░";
}
}
std::cout << std::endl;
}
}