aboutsummaryrefslogtreecommitdiff
path: root/2022/19-Not-Enough-Minerals/priority_queue.js
diff options
context:
space:
mode:
authorJulien Dessaux2022-12-28 20:03:29 +0100
committerJulien Dessaux2022-12-28 20:04:09 +0100
commit3430a7074b85b12bb16d9906c04e857448e85f44 (patch)
tree7c0a2db727eadfef707b4a0715866bb9f40d52a9 /2022/19-Not-Enough-Minerals/priority_queue.js
parent2022-18 in js (diff)
downloadadvent-of-code-3430a7074b85b12bb16d9906c04e857448e85f44.tar.gz
advent-of-code-3430a7074b85b12bb16d9906c04e857448e85f44.tar.bz2
advent-of-code-3430a7074b85b12bb16d9906c04e857448e85f44.zip
2022-19 in js
Diffstat (limited to '')
-rw-r--r--2022/19-Not-Enough-Minerals/priority_queue.js39
1 files changed, 39 insertions, 0 deletions
diff --git a/2022/19-Not-Enough-Minerals/priority_queue.js b/2022/19-Not-Enough-Minerals/priority_queue.js
new file mode 100644
index 0000000..76e513c
--- /dev/null
+++ b/2022/19-Not-Enough-Minerals/priority_queue.js
@@ -0,0 +1,39 @@
+export class QElement {
+ constructor(element, priority) {
+ this.element = element;
+ this.priority = priority;
+ }
+}
+
+export class PriorityQueue {
+ constructor(elt) {
+ this.items = [];
+ if (elt !== undefined) {
+ this.enqueue(elt, 0);
+ }
+ }
+
+ enqueue(element, priority) {
+ let qElement = new QElement(element, priority);
+
+ for (let i = 0; i < this.items.length; ++i) {
+ if (this.items[i].priority > qElement.priority) {
+ this.items.splice(i, 0, qElement);
+ return;
+ }
+ }
+ this.items.push(qElement);
+ }
+ dequeue() {
+ return this.items.pop(); // pop highest priority, use shift() for lower priority
+ }
+ front() {
+ return this.items[0];
+ }
+ rear() {
+ return this.items[this.items.length - 1];
+ }
+ isEmpty() {
+ return this.items.length === 0;
+ }
+}