Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Apr 3, 2024
2 parents 3f0a08c + eebcc22 commit dc9f131
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 9 deletions.
39 changes: 39 additions & 0 deletions base/complex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ComplexNumber {
constructor(public real: number, public imag: number) {}
add(that: ComplexNumber) {
return new ComplexNumber(this.real + that.real, this.imag + that.imag);
}
sub(that: ComplexNumber|number) {
if (typeof that === 'number') {
return new ComplexNumber(this.real - that, this.imag);
}
return new ComplexNumber(this.real - that.real, this.imag - that.imag);
}
mul(that: ComplexNumber|number) {
if (typeof that === 'number') {
return new ComplexNumber(this.real * that, this.imag * that);
}
return new ComplexNumber(
this.real * that.real - this.imag * that.imag,
this.real * that.imag + this.imag * that.real
);
}
div(that: ComplexNumber|number) {
if (typeof that === 'number') {
return new ComplexNumber(this.real / that, this.imag / that);
}
const r = that.real * that.real + that.imag * that.imag;
return new ComplexNumber(
(this.real * that.real + this.imag * that.imag) / r,
(this.imag * that.real - this.real * that.imag) / r
);
}
abs() {
return Math.sqrt(this.real * this.real + this.imag * this.imag);
}
toString() {
return `${this.real} ${this.imag >= 0 ? '+':'-'} ${Math.abs(this.imag)}i`;
}
}

export { ComplexNumber };
89 changes: 89 additions & 0 deletions base/grid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { ComplexNumber } from "./complex";
import { parseNumberList } from "./parse";

class Grid<T> {
public grid : T[][];
public rotation = new ComplexNumber(1,0);

constructor(public width: number, public height: number, def: T) {
this.grid = Array.from({length: width}, () => Array.from({length: height}, () => def))
}

public get(x: number, y: number) {
const idx = this.getIdx(x, y)
return this.grid[idx.x][idx.y]
}

public set(x: number, y: number, val: T) {
const idx = this.getIdx(x, y)
this.grid[idx.x][idx.y] = val
}

public setAll(newGrid : T[][]) {
this.grid = newGrid
this.width = newGrid.length
this.height = newGrid[0].length
}

public rotate(amount: -3|-2|-1|0|1|2|3|number) {
if (amount == -3 || amount == 1) {
this.rotation = this.rotation.mul(new ComplexNumber(0,1));
}
else if (amount == -1 || amount == 3) {
this.rotation = this.rotation.mul(new ComplexNumber(0,-1));
}
else if (amount == -2 || amount == 2) {
this.rotation = this.rotation.mul(new ComplexNumber(-1,0));
}
else if (amount != 0) {
console.warn("Can not handle rotation value!")
}
}

public getHeight() {
if (this.rotation.imag != 0) {
return this.width
}
return this.height
}

public getWidth() {
if (this.rotation.imag != 0) {
return this.height
}
return this.width
}

protected getIdx(x: number, y: number) {
const idx = new ComplexNumber(x, y).mul(this.rotation)
if (idx.real < 0) {
idx.real = this.height - idx.real
}
if (idx.imag < 0) {
idx.imag = this.width - idx.imag
}
return {x: idx.real, y: idx.imag}
}
}

class NumGrid extends Grid<number> {
constructor(width: number, height: number, def: number = 0) {
super(width, height, def);
}

public setByLines(lines: string[], separator = '') {
this.setAll(lines.map(i => parseNumberList(i, separator)))
}
}

class CharGrid extends Grid<string> {
constructor(width: number, height: number, def: string = ' ') {
super(width, height, def);
}

public setByLines(lines: string[], separator = '') {
this.setAll(lines.map(i => i.split(separator)))
}
}

export { Grid, NumGrid, CharGrid }
122 changes: 122 additions & 0 deletions base/simpleStructure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
class StructureNode<T> {
constructor(public val: T, public next?: StructureNode<T>) {}
}

class Stack<T> {
private _top: StructureNode<T>|undefined

public push(v: T) {
const next = new StructureNode(v, this._top)
this._top = next
}

public pop(): T {
if (!this._top) throw "Empty Stack"
const r = this._top.val
this._top = this._top.next
return r
}

public isEmpty() {
return !this._top
}

public top() {
if (!this._top) throw "Empty Stack"
return this._top.val
}

public asArray() {
let e = this._top
if (!e) return []
const arr: T[] = []
while(e) {
arr.push(e.val)
e = e.next
}
return arr
}
}

class Queue<T> {
private _head: StructureNode<T>|undefined
private _tail: StructureNode<T>|undefined

public push(v: T) {
if (!this._tail) {
const n = new StructureNode(v)
this._head = n
this._tail = n
return
}
const n = new StructureNode(v)
this._tail.next = n
this._tail = n
}

public pop(): T {
if (!this._head) throw "Empty Queue"
const r = this._head.val
this._head = this._head.next
return r
}

public head(): T {
if (!this._head) throw "Empty Queue"
return this._head.val
}

public isEmpty() {
return !this._head
}

public asArray() {
let e = this._head
if (!e) return []
const arr: T[] = []
while(e) {
arr.push(e.val)
e = e.next
}
return arr
}
}

class JsonSet<S> {
private set = new Set()

public add(val: S) {
this.set.add(JSON.stringify(val))
}

public has(val: S) {
return this.set.has(JSON.stringify(val))
}

public remove(val: S) {
this.set.delete(JSON.stringify(val))
}
}

class FunctionSet<S> {

private set: S[] = []

constructor(public equals: (s1: S, s2:S) => boolean) {}

public add(val: S) {
if (!this.has(val))
this.set.push(val)
}

public has(val: S) {
return this.set.some(s => this.equals(s, val))
}

public remove(val: S) {
const idx = this.set.findIndex(s => this.equals(s, val))
if (idx >= 0) this.set = this.set.filter((_, ind) => ind != idx)
}
}

export { Stack, Queue, JsonSet, FunctionSet }
44 changes: 44 additions & 0 deletions base/stateProblem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { JsonSet, Queue, Stack } from "./simpleStructure";

class StateProblem<S> {

constructor(public next: (arg1: S) => S[], public explore?: (arg1: S) => boolean) {}

public bfs(root: S) {
const Q = new Queue<S>();
const V = new JsonSet<S>();
Q.push(root)
while(!Q.isEmpty) {
const q = Q.pop()
if (this.explore != undefined) {
const r = this.explore(q)
if (!r) break
}
const next = this.next(q)
for (const n of next) {
if (!V.has(n)) {
Q.push(n)
}
}
}
}

public dfs(root: S) {
const Q = new Stack<S>();
const V = new JsonSet<S>();
Q.push(root)
while(!Q.isEmpty) {
const q = Q.pop()
if (this.explore != undefined) {
const r = this.explore(q)
if (!r) break
}
const next = this.next(q)
for (const n of next) {
if (!V.has(n)) {
Q.push(n)
}
}
}
}
}
8 changes: 4 additions & 4 deletions visuals/github-metrics-2023.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions visuals/github-metrics.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dc9f131

Please sign in to comment.