aboutsummaryrefslogtreecommitdiff
path: root/2022/16-proboscidea-volcanium
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/16-proboscidea-volcanium
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/16-proboscidea-volcanium/first.js3
-rw-r--r--2022/16-proboscidea-volcanium/priority_queue.js36
-rw-r--r--2022/16-proboscidea-volcanium/second.js3
3 files changed, 12 insertions, 30 deletions
diff --git a/2022/16-proboscidea-volcanium/first.js b/2022/16-proboscidea-volcanium/first.js
index a7274aa..504ad54 100644
--- a/2022/16-proboscidea-volcanium/first.js
+++ b/2022/16-proboscidea-volcanium/first.js
@@ -18,8 +18,7 @@ fs.readFileSync("input", "utf8")
rate: a[2],
computePathCostTo: function (target) {
Object.values(valves).forEach(v => v.cost = 0);
- let nq = new PriorityQueue();
- nq.enqueue(this, 0);
+ let nq = new PriorityQueue(this);
while (true) {
let n = nq.dequeue();
if (n.element.label === target) {
diff --git a/2022/16-proboscidea-volcanium/priority_queue.js b/2022/16-proboscidea-volcanium/priority_queue.js
index c1a5b65..b9be08b 100644
--- a/2022/16-proboscidea-volcanium/priority_queue.js
+++ b/2022/16-proboscidea-volcanium/priority_queue.js
@@ -6,50 +6,34 @@ export class QElement {
}
export class PriorityQueue {
- constructor() {
+ constructor(elt) {
this.items = [];
+ if (elt !== undefined) {
+ this.enqueue(elt, 0);
+ }
}
enqueue(element, priority) {
- var qElement = new QElement(element, priority);
- var contain = false;
+ let qElement = new QElement(element, priority);
- for (var i = 0; i < this.items.length; i++) {
+ for (let i = 0; i < this.items.length; ++i) {
if (this.items[i].priority > qElement.priority) {
this.items.splice(i, 0, qElement);
- contain = true;
- break;
+ return;
}
}
- if (!contain) {
- this.items.push(qElement);
- }
+ this.items.push(qElement);
}
dequeue() {
- if (this.isEmpty()){
- throw "Attempting to dequeue an empty queue";
- }
- return this.items.shift();
+ return this.items.shift(); // pop highest priority, use shift() for lower priority
}
front() {
- if (this.isEmpty()){
- throw "Attempting to front an empty queue";
- }
return this.items[0];
}
rear() {
- if (this.isEmpty()){
- throw "Attempting to rear an empty queue";
- }
return this.items[this.items.length - 1];
}
isEmpty() {
- return this.items.length == 0;
- }
- printPQueue() {
- var str = "";
- for (var i = 0; i < this.items.length; i++)
- str += this.items[i].element + " ";
- return str;
+ return this.items.length === 0;
}
}
diff --git a/2022/16-proboscidea-volcanium/second.js b/2022/16-proboscidea-volcanium/second.js
index 18c6f84..3b41645 100644
--- a/2022/16-proboscidea-volcanium/second.js
+++ b/2022/16-proboscidea-volcanium/second.js
@@ -18,8 +18,7 @@ fs.readFileSync("input", "utf8")
rate: a[2],
computePathCostTo: function (target) {
Object.values(valves).forEach(v => v.cost = 0);
- let nq = new PriorityQueue();
- nq.enqueue(this, 0);
+ let nq = new PriorityQueue(this);
while (true) {
let n = nq.dequeue();
if (n.element.label === target) {