-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
120 lines (112 loc) · 4.33 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <vector>
#include <map>
namespace aoc2017_day08 {
int part_1(std::string_view path) {
std::vector<std::tuple<std::string, std::string, int, std::string, std::string, int>> input;
std::vector<std::string> lines = file::readFileAsArrayString(path);
for (const auto& line : lines) {
std::vector<std::string> tokens = utils::splitString(line, " ");
input.push_back({tokens[0], tokens[1], std::stoi(tokens[2]), tokens[4], tokens[5], std::stoi(tokens[6])});
}
std::map<std::string, int> registers;
for (const auto& line : input) {
const auto& [value1, op, nr1, value2, comp, nr2] = line;
int value = 0;
if (registers.find(value2) != registers.end()) {
value = registers[value2];
}
bool conditionTrue = false;
if (comp.compare("<") == 0 && value < nr2) {
conditionTrue = true;
}
else if (comp.compare("<=") == 0 && value <= nr2) {
conditionTrue = true;
}
else if (comp.compare(">") == 0 && value > nr2) {
conditionTrue = true;
}
else if (comp.compare(">=") == 0 && value >= nr2) {
conditionTrue = true;
}
else if (comp.compare("==") == 0 && value == nr2) {
conditionTrue = true;
}
else if (comp.compare("!=") == 0 && value != nr2) {
conditionTrue = true;
}
if (conditionTrue) {
if (registers.find(value1) == registers.end()) {
registers.insert({value1, 0});
}
if (op.compare("inc") == 0) {
registers[value1] += nr1;
}
else {
registers[value1] -= nr1;
}
}
}
return std::max_element(registers.begin(), registers.end(), [](const auto& a, const auto& b) {
return a.second < b.second;
})->second;
}
int part_2(std::string_view path) {
std::vector<std::tuple<std::string, std::string, int, std::string, std::string, int>> input;
std::vector<std::string> lines = file::readFileAsArrayString(path);
for (const auto& line : lines) {
std::vector<std::string> tokens = utils::splitString(line, " ");
input.push_back({tokens[0], tokens[1], std::stoi(tokens[2]), tokens[4], tokens[5], std::stoi(tokens[6])});
}
std::map<std::string, int> registers;
int max = std::numeric_limits<int>::min();
for (const auto& line : input) {
const auto& [value1, op, nr1, value2, comp, nr2] = line;
int value = 0;
if (registers.find(value2) != registers.end()) {
value = registers[value2];
}
bool conditionTrue = false;
if (comp.compare("<") == 0 && value < nr2) {
conditionTrue = true;
}
else if (comp.compare("<=") == 0 && value <= nr2) {
conditionTrue = true;
}
else if (comp.compare(">") == 0 && value > nr2) {
conditionTrue = true;
}
else if (comp.compare(">=") == 0 && value >= nr2) {
conditionTrue = true;
}
else if (comp.compare("==") == 0 && value == nr2) {
conditionTrue = true;
}
else if (comp.compare("!=") == 0 && value != nr2) {
conditionTrue = true;
}
if (conditionTrue) {
if (registers.find(value1) == registers.end()) {
registers.insert({value1, 0});
}
if (op.compare("inc") == 0) {
registers[value1] += nr1;
}
else {
registers[value1] -= nr1;
}
max = std::max(max, registers[value1]);
}
}
return max;
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2017_day08::part_1("../2017/day08/input.in") << std::endl;
std::cout << "Part 2: " << aoc2017_day08::part_2("../2017/day08/input.in") << std::endl;
return 0;
}
#endif