aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-02-09 22:38:36 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-02-09 22:38:36 +0800
commit799bfdddc02739849c440f71a7de282856512c02 (patch)
tree476c451f4802a15d8515000bc98f53a884f5f27e /src
parent526228d5a94e74e600f38aa8055cb5d01e0e4eba (diff)
downloadadvent-of-code-799bfdddc02739849c440f71a7de282856512c02.tar.gz
advent-of-code-799bfdddc02739849c440f71a7de282856512c02.zip
2017 day17 part1
Diffstat (limited to 'src')
-rw-r--r--src/2017/day17/README.md12
-rw-r--r--src/2017/day17/aoc.cpp36
-rw-r--r--src/2017/day17/aoc.h7
3 files changed, 54 insertions, 1 deletions
diff --git a/src/2017/day17/README.md b/src/2017/day17/README.md
index f1ce675..33fc413 100644
--- a/src/2017/day17/README.md
+++ b/src/2017/day17/README.md
@@ -30,3 +30,15 @@ What is the value after 2017 in your completed circular buffer?
Your puzzle input is 356.
+--- Part Two ---
+
+The spinlock does not short-circuit. Instead, it gets more angry. At least, you assume that's what happened; it's spinning significantly faster than it was a moment ago.
+
+You have good news and bad news.
+
+The good news is that you have improved calculations for how to stop the spinlock. They indicate that you actually need to identify the value after 0 in the current state of the circular buffer.
+
+The bad news is that while you were determining this, the spinlock has just finished inserting its fifty millionth value (50000000).
+
+What is the value after 0 the moment 50000000 is inserted?
+
diff --git a/src/2017/day17/aoc.cpp b/src/2017/day17/aoc.cpp
index f2a9d5a..ba0d5bc 100644
--- a/src/2017/day17/aoc.cpp
+++ b/src/2017/day17/aoc.cpp
@@ -2,6 +2,40 @@
namespace aoc2017 {
-std::pair<int64_t, int64_t> day17(line_view file) { return {0, 0}; }
+static spinlock* next(spinlock* x, int step) {
+ while (step-- > 0) {
+ x = x->next;
+ }
+ return x;
+}
+
+static void insert(spinlock* s, spinlock** current, int v) {
+ spinlock* n = s->next;
+ spinlock* x = new spinlock;
+ x->val = v;
+ s->next = x;
+ x->prev = s;
+ x->next = n;
+ n->prev = x;
+ *current = x;
+}
+
+std::pair<int64_t, int64_t> day17(line_view file) {
+ spinlock* l = new spinlock;
+ l->val = 0;
+ l->next = l;
+ l->prev = l;
+ // spinlock* l0 = l;
+
+ int step{356};
+ for (int i = 1; i < 2018; i++) {
+ l = next(l, step);
+ insert(l, &l, i);
+ }
+ int t0 = l->next->val;
+ // printf("%d\n", l0->next->val);
+
+ return {t0, 0};
+}
} // namespace aoc2017
diff --git a/src/2017/day17/aoc.h b/src/2017/day17/aoc.h
index e8b7659..022f148 100644
--- a/src/2017/day17/aoc.h
+++ b/src/2017/day17/aoc.h
@@ -3,5 +3,12 @@
#include <vector>
namespace aoc2017 {
+
+struct spinlock {
+ int val;
+ spinlock* next = nullptr;
+ spinlock* prev = nullptr;
+};
+
std::pair<int64_t, int64_t> day17(line_view);
}