-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.cpp
100 lines (84 loc) · 2.91 KB
/
day5.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
#include <bits/stdc++.h>
using namespace std;
bool passesPartOne(const unordered_map<int, vector<int>> &rules, const vector<int> &update) {
for (int i=0;i<update.size();i++) {
int x = update[i];
if (rules.find(x) == rules.end()) {
continue; // if there's no rules for a character, we can skip it
}
vector<int> specificRules = rules.at(x);
for (int y : specificRules) {
auto it = find(update.begin(), update.end(), y);
if (it == update.end()) {
continue; // if y isn't in the update, then it can't have broken the rule
}
int index = distance(update.begin(), it);
if (index < i) { // then we have broke the rule
return false;
}
}
}
return true;
}
int partTwoMidPage(const unordered_map<int, vector<int>> &rules, vector<int> &update) {
bool swapped = true;
while (swapped) {
swapped = false;
for (int i=0;i<update.size();i++) {
int x = update[i];
if (rules.find(x) == rules.end()) {
continue; // if there's no rules for a character, we can skip it
}
vector<int> specificRules = rules.at(x);
for (int y : specificRules) {
auto it = find(update.begin(), update.end(), y);
if (it == update.end()) {
continue; // if y isn't in the update, then it can't have broken the rule
}
int index = distance(update.begin(), it);
if (index < i) { // then we have broke the rule, so swap the positions of x and y
swapped = true;
swap(update[i], update[index]);
}
}
}
}
return update[update.size() / 2];
}
int main() {
// Get input
unordered_map<int, vector<int>> rules;
vector<vector<int>> updates;
string line;
while(getline(cin, line) && !line.empty()) {
int x = stoi(line.substr(0, 2));
int y = stoi(line.substr(3, 2));
rules[x].push_back(y);
}
while(getline(cin, line) && !line.empty()) {
vector<int> update;
for (int i=0;i<line.length();i += 3) {
int x = stoi(line.substr(i, 2));
update.push_back(x);
}
updates.push_back(update);
}
// Part 1
int total = 0;
for(vector<int> update : updates) {
if (passesPartOne(rules, update)) {
total += update[update.size() / 2];
}
}
printf("Part 1: %d\n", total); // gonna start using printf now cause internet man said cout is ass (it is)
// Part 2
total = 0;
for (vector<int> update : updates) {
if (!passesPartOne(rules, update)) {
int mid = partTwoMidPage(rules, update);
total += partTwoMidPage(rules, update);
}
}
printf("Part 2: %d\n", total);
return 0;
}