summaryrefslogtreecommitdiff
path: root/utils/jsonds.ts
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-12-02 14:16:56 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-12-02 14:16:56 -0700
commitcfd970e21663c3278f6e01d356690789225b6b56 (patch)
treea868659cc8425cc47ba1cd02509b3fe80632039c /utils/jsonds.ts
parent118fc144884f0d716cc03e877aa85f83d289cec8 (diff)
downloadaoc-cfd970e21663c3278f6e01d356690789225b6b56.tar.gz
aoc-cfd970e21663c3278f6e01d356690789225b6b56.zip
2022 day 9 and add utils
Diffstat (limited to 'utils/jsonds.ts')
-rw-r--r--utils/jsonds.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/utils/jsonds.ts b/utils/jsonds.ts
new file mode 100644
index 0000000..c717232
--- /dev/null
+++ b/utils/jsonds.ts
@@ -0,0 +1,66 @@
+export class JSONSet<T extends Object> {
+ private items: Set<string>;
+
+ constructor() {
+ this.items = new Set<string>();
+ }
+
+ add(item: T): void {
+ const itemJson = JSON.stringify(item, Object.keys(item).sort());
+ this.items.add(itemJson);
+ }
+
+ has(item: T): boolean {
+ const itemJson = JSON.stringify(item, Object.keys(item).sort());
+ return this.items.has(itemJson);
+ }
+
+ delete(item: T): boolean {
+ const itemJson = JSON.stringify(item, Object.keys(item).sort());
+ return this.items.delete(itemJson);
+ }
+
+ clear(): void {
+ this.items.clear();
+ }
+
+ get size(): number {
+ return this.items.size;
+ }
+}
+
+export class JSONHashMap<T extends Object> {
+ private map: Map<string, T>;
+
+ constructor() {
+ this.map = new Map<string, T>();
+ }
+
+ set(key: T, value: T): void {
+ const keyJson = JSON.stringify(key, Object.keys(key).sort());
+ this.map.set(keyJson, value);
+ }
+
+ get(key: T): T | undefined {
+ const keyJson = JSON.stringify(key, Object.keys(key).sort());
+ return this.map.get(keyJson);
+ }
+
+ has(key: T): boolean {
+ const keyJson = JSON.stringify(key, Object.keys(key).sort());
+ return this.map.has(keyJson);
+ }
+
+ delete(key: T): boolean {
+ const keyJson = JSON.stringify(key, Object.keys(key).sort());
+ return this.map.delete(keyJson);
+ }
+
+ clear(): void {
+ this.map.clear();
+ }
+
+ get size(): number {
+ return this.map.size;
+ }
+}