Compare commits

...

8 commits

Author SHA1 Message Date
eriedaberrie a814a54c34 2023 day 5
I did not have fun

These problems were very unenjoyable imo (skill issue probably)
2023-12-04 22:21:01 -08:00
eriedaberrie 190b9c6ff9 2023 day 4: remove goto and replace vector with set
Due to popular demand
2023-12-04 11:46:32 -08:00
eriedaberrie c82a6bcffc 2023 day 4 2023-12-03 21:49:21 -08:00
eriedaberrie 3c9523ab79 2023 day 1-3: touchup C++ cin input 2023-12-03 16:15:40 -08:00
eriedaberrie ae27321e81 2023 day 1: add C++ solution 2023-12-03 14:34:24 -08:00
eriedaberrie 3f971f69ca Add readme 2023-12-03 13:39:12 -08:00
eriedaberrie 8a007ef0f3 2023 day 3: touchup 2023-12-02 23:39:35 -08:00
eriedaberrie be51e9fbf0 2023 day 3 2023-12-02 22:48:41 -08:00
10 changed files with 374 additions and 30 deletions

56
2023/1/main.cpp Normal file
View file

@ -0,0 +1,56 @@
#include <bits/stdc++.h>
using namespace std;
const string NUMBERS[] = {
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int ret = 0;
string line;
while (getline(cin, line) && !line.empty()) {
int pi = INT_MAX, pl = -1, vi = 0, vl = 0;
for (int i = 0; i < line.size(); i++) {
unsigned char c = line[i] - '0';
if (c <= 9) {
if (vi == 0) {
pi = i;
vi = c;
}
pl = i;
vl = c;
}
}
for (int n = 1; n <= 9; n++) {
const auto strn = NUMBERS[n - 1];
int i = 0;
int posi = line.find(strn);
if (posi == string::npos)
continue;
int posl = line.rfind(strn);
if (posi < pi) {
pi = posi;
vi = n;
}
if (posl > pl) {
pl = posl;
vl = n;
}
}
ret += vi * 10 + vl;
}
cout << ret << '\n';
}

View file

@ -5,28 +5,23 @@ int main() {
ios::sync_with_stdio(0); ios::sync_with_stdio(0);
cin.tie(0); cin.tie(0);
auto& cin = std::cin >> noskipws;
int ret = 0, id = 0; int ret = 0, id = 0;
bool isPossible = true; bool isPossible = true;
for (;;) { while (!cin.eof()) {
char c; switch (cin.get()) {
cin >> c;
switch (c) {
case 'G': case 'G':
cin >> c; cin >> c; cin >> c; cin >> c; cin.ignore(4);
cin >> id; cin >> id;
break; break;
case ':': case ',': case ';': case ':': case ',': case ';':
if (!isPossible) if (!isPossible)
break; break;
cin >> c; cin.ignore(1);
int n; int n;
cin >> n; cin >> n;
cin >> c; cin.ignore(1);
cin >> c; switch (cin.get()) {
switch (c) {
case 'r': case 'r':
if (n > 12) if (n > 12)
isPossible = false; isPossible = false;
@ -42,16 +37,16 @@ int main() {
} }
break; break;
case '\n': case '\n':
if (cin.eof()) case EOF:
goto end; if (isPossible) {
if (isPossible)
ret += id; ret += id;
else id = 0;
} else {
isPossible = true; isPossible = true;
}
break; break;
} }
} }
end:
cout << ret << '\n'; cout << ret << '\n';
} }

View file

@ -5,39 +5,32 @@ int main() {
ios::sync_with_stdio(0); ios::sync_with_stdio(0);
cin.tie(0); cin.tie(0);
auto& cin = std::cin >> noskipws;
int ret = 0, id = 0, r = 0, g = 0, b = 0; int ret = 0, id = 0, r = 0, g = 0, b = 0;
for (;;) { while (!cin.eof()) {
char c; switch (cin.get()) {
cin >> c;
switch (c) {
case 'G': case 'G':
cin >> c; cin >> c; cin >> c; cin >> c; cin.ignore(4);
cin >> id; cin >> id;
break; break;
case ':': case ',': case ';': case ':': case ',': case ';':
cin >> c; cin.ignore(1);
int n; int n;
cin >> n; cin >> n;
cin >> c; cin.ignore(1);
cin >> c; switch (cin.get()) {
switch (c) {
case 'r': r = max(n, r); break; case 'r': r = max(n, r); break;
case 'g': g = max(n, g); break; case 'g': g = max(n, g); break;
case 'b': b = max(n, b); break; case 'b': b = max(n, b); break;
} }
break; break;
case '\n': case '\n':
if (cin.eof()) case EOF:
goto end;
ret += r * g * b; ret += r * g * b;
r = 0; g = 0; b = 0; r = 0; g = 0; b = 0;
break; break;
} }
} }
end:
cout << ret << '\n'; cout << ret << '\n';
} }

50
2023/3/main-1.cpp Normal file
View file

@ -0,0 +1,50 @@
#include <bits/stdc++.h>
using namespace std;
inline bool isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<string> lines;
string line;
while (getline(cin, line) && !line.empty()) {
lines.push_back(line);
}
int ret = 0;
for (int r = 0; r < lines.size(); r++) {
auto& line = lines[r];
int cend = line.size();
for (int c = 0; c < cend; c++) {
char ch = line[c];
if (ch == '.' || isDigit(ch))
continue;
for (int rr = r - 1; rr <= r + 1; rr++) {
if (rr < 0 || rr == lines.size())
continue;
for (int cc = c - 1; cc <= c + 1; cc++) {
if (cc < 0 || cc == cend)
continue;
auto& lo = lines[rr];
if (isDigit(lo[cc])) {
int ci = cc, cf = cc;
while (ci > 0 && isDigit(lo[ci - 1]))
ci--;
while (cf < cend - 1 && isDigit(lo[cf + 1]))
cf++;
ret += stoi(lo.substr(ci, cf - ci + 1));
for (int i = ci; i <= cf; i++)
lo[i] = '.';
}
}
}
}
}
cout << ret << '\n';
}

58
2023/3/main-2.cpp Normal file
View file

@ -0,0 +1,58 @@
#include <bits/stdc++.h>
using namespace std;
inline bool isDigit(char ch) {
return ch >= '0' && ch <= '9';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<string> lines;
string line;
while (getline(cin, line) && !line.empty()) {
lines.push_back(line);
}
int ret = 0;
for (int r = 0; r < lines.size(); r++) {
auto& line = lines[r];
int cend = line.size();
for (int c = 0; c < cend; c++) {
char ch = line[c];
if (ch != '*')
continue;
vector<int> rs = {r}, cs = {c};
int p = 1;
int n = 0;
for (int rr = r - 1; rr <= r + 1; rr++) {
if (rr < 0 || rr == lines.size())
continue;
for (int cc = c - 1; cc <= c + 1; cc++) {
if (cc < 0 || cc == cend)
continue;
auto& lo = lines[rr];
if (isDigit(lo[cc])) {
int ci = cc, cf = cc;
while (ci > 0 && isDigit(lo[ci - 1]))
ci--;
while (cf < cend - 1 && isDigit(lo[cf + 1]))
cf++;
p *= stoi(lo.substr(ci, cf - ci + 1));
for (int i = ci; i <= cf; i++)
lo[i] = '.';
if (++n > 2)
goto rrccend;
}
}
}
rrccend:
if (n == 2)
ret += p;
}
}
cout << ret << '\n';
}

34
2023/4/main-1.cpp Normal file
View file

@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int ret = 0;
string line;
while (getline(cin, line) && !line.empty()) {
int count = 1;
int start = line.find(':');
int bar = line.find('|');
string left = line.substr(start + 2, bar - start - 3);
string right = line.substr(bar + 2);
istringstream lss(left), rss(right);
set<int> ls;
while (!lss.eof()) {
int n;
lss >> n;
ls.insert(n);
}
while (!rss.eof()) {
int n;
rss >> n;
if (ls.find(n) != ls.end()) {
count <<= 1;
}
}
ret += count >> 1;
}
cout << ret << '\n';
}

44
2023/4/main-2.cpp Normal file
View file

@ -0,0 +1,44 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<int> counts, copies;
string line;
while (getline(cin, line) && !line.empty()) {
int start = line.find(':');
int bar = line.find('|');
string left = line.substr(start + 2, bar - start - 3);
string right = line.substr(bar + 2);
istringstream lss(left), rss(right);
set<int> ls;
int count = 0;
while (!lss.eof()) {
int n;
lss >> n;
ls.insert(n);
}
while (!rss.eof()) {
int n;
rss >> n;
if (ls.find(n) != ls.end())
count++;
}
counts.push_back(count);
copies.push_back(1);
}
int ret = 0;
for (int cn = 0; cn < counts.size(); cn++) {
ret += copies[cn];
int n = cn;
for (int i = 0; i < counts[cn]; i++) {
if (++n == copies.size())
break;
copies[n] += copies[cn];
}
}
cout << ret << '\n';
}

39
2023/5/main-1.cpp Normal file
View file

@ -0,0 +1,39 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin.ignore(6);
string line;
getline(cin, line);
istringstream sls(line);
vector<long> seeds;
vector<bool> done;
while (!sls.eof()) {
long n;
sls >> n;
seeds.push_back(n);
done.push_back(false);
}
cin.ignore(1);
for (int mapn = 0; mapn < 7; mapn++) {
cin.ignore(500, '\n');
while (getline(cin, line) && !line.empty()) {
istringstream sls(line);
long d, s, r;
sls >> d >> s >> r;
for (int i = 0; i < seeds.size(); i++) {
if (!done[i] && seeds[i] >= s && seeds[i] < s + r) {
seeds[i] += d - s;
done[i] = true;
}
}
}
fill(done.begin(), done.end(), false);
}
cout << *min_element(seeds.begin(), seeds.end()) << '\n';
}

66
2023/5/main-2.cpp Normal file
View file

@ -0,0 +1,66 @@
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin.ignore(6);
string line;
getline(cin, line);
istringstream sls(line);
vector<pair<long, long>> seeds;
vector<bool> done;
while (!sls.eof()) {
long n, nn;
sls >> n >> nn;
seeds.push_back(make_pair(n, n + nn - 1));
done.push_back(false);
}
cin.ignore(1);
for (int mapn = 0; mapn < 7; mapn++) {
cin.ignore(500, '\n');
while (getline(cin, line) && !line.empty()) {
istringstream sls(line);
long d, s, r;
sls >> d >> s >> r;
int ssize = seeds.size();
for (int i = 0; i < ssize; i++) {
if (done[i])
continue;
auto& curr = seeds[i];
long *curs = &get<0>(curr), *cure = &get<1>(curr);
if (*curs >= s) {
if (*cure < s + r) {
*curs += d - s;
*cure += d - s;
done[i] = true;
} else if (*curs < s + r) {
seeds.push_back(make_pair(*curs + d - s, d + r - 1));
done.push_back(true);
*curs = s + r;
}
} else if (*cure > s + r) {
seeds.push_back(make_pair(d, d + r - 1));
done.push_back(true);
seeds.push_back(make_pair(s + r, *cure));
done.push_back(false);
*cure = s - 1;
} else if (*cure > s) {
seeds.push_back(make_pair(d, *cure + d - s));
done.push_back(true);
*cure = s - 1;
}
}
}
fill(done.begin(), done.end(), false);
}
int min = INT_MAX;
for (auto& r : seeds) {
if (get<0>(r) < min)
min = get<0>(r);
}
cout << min << '\n';
}

9
README.org Normal file
View file

@ -0,0 +1,9 @@
#+TITLE: Advent of Code
#+BEGIN_QUOTE
My [[https://adventofcode.com/][Advent of Code]] solutions.
#+END_QUOTE
Do not expect any form of consistency or maintainability from the files
contained within this repository, just that they managed to get the right
answers for me.