aboutsummaryrefslogtreecommitdiff
path: root/src/2022/day13/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-14 13:40:33 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-14 13:40:33 +0800
commit0be5a28f1373448194c1266ec33a7e5860e806d9 (patch)
treee909378019f78a72a53ca391835ab1b503f9be3e /src/2022/day13/aoc.cpp
parentf46ace5333e19e56e0024fef94c732b8129ef9f3 (diff)
downloadadvent-of-code-0be5a28f1373448194c1266ec33a7e5860e806d9.tar.gz
advent-of-code-0be5a28f1373448194c1266ec33a7e5860e806d9.zip
2022 day14 setup
Diffstat (limited to 'src/2022/day13/aoc.cpp')
-rw-r--r--src/2022/day13/aoc.cpp40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/2022/day13/aoc.cpp b/src/2022/day13/aoc.cpp
index fb71da4..f844116 100644
--- a/src/2022/day13/aoc.cpp
+++ b/src/2022/day13/aoc.cpp
@@ -1,17 +1,44 @@
#include "aoc.h"
#include <iostream>
+#include <algorithm>
+
namespace aoc2022 {
+struct pkt {
+ packet* p;
+ size_t i;
+};
+
+std::pair<int, int> find(const std::vector<pkt>& ps) {
+ size_t s1 = ps.size() - 1;
+ size_t s2 = ps.size() - 2;
+ size_t a{0}, b{0};
+ for(size_t i = 0; i < ps.size(); i++) {
+ if (ps[i].i == s1) a = i;
+ if (ps[i].i == s2) b = i;
+ printf("%zu :", i);
+ ps[i].p->print(0);
+ printf("\n");
+ }
+ printf("%zu %zu\n", a, b);
+ return {a + 1, b + 1};
+}
+
std::pair<int, int> day13(line_view file) {
int count{0};
int pair{0};
+
+
+ std::vector<pkt> all;
packet* ps[2] = {nullptr, nullptr};
- per_line(file, [&pair, &count, &ps](line_view lv){
+ per_line(file, [&pair, &count, &ps, &all](line_view lv){
if (lv.length > 1) {
int i = pair % 2;
- ps[i] = new packet;
+ packet* pkt = new packet;
+ ps[i] = pkt;
const char* p = lv.line;
packet::load(&p, &ps[i]);
+ all.push_back({pkt, (size_t) pair});
if (i == 1 && *(ps[0]) < *(ps[1])) {
printf("group[%d] is in right order\n", pair/2 + 1);
count += pair / 2 + 1;
@@ -20,7 +47,14 @@ std::pair<int, int> day13(line_view file) {
}
return true;
});
- return {count, 0};
+
+ std::sort(all.begin(), all.end(), [](const pkt& p1, const pkt& p2){
+ return *(p1.p) < *(p2.p);
+ });
+
+ auto p = find(all);
+
+ return {count, p.first * p.second};
}
}