From cb6cb86b57d8eee9e9892aff5bfa5e56529a0ead Mon Sep 17 00:00:00 2001 From: Alexander Vogt Date: Wed, 3 Apr 2024 23:39:57 +0200 Subject: [PATCH] Start of 2015 --- 2015/01/runner.ts | 29 +++++++++++ 2015/01/task.ts | 26 ++++++++++ 2015/02/runner.ts | 29 +++++++++++ 2015/02/task.ts | 26 ++++++++++ 2015/03/runner.ts | 29 +++++++++++ 2015/03/task.ts | 59 +++++++++++++++++++++ 2015/05/runner.ts | 29 +++++++++++ 2015/05/task.ts | 48 +++++++++++++++++ 2015/06/runner.ts | 29 +++++++++++ 2015/06/task.ts | 75 +++++++++++++++++++++++++++ 2015/07/runner.ts | 29 +++++++++++ 2015/07/task.ts | 112 ++++++++++++++++++++++++++++++++++++++++ 2015/08/runner.ts | 29 +++++++++++ 2015/08/task.ts | 46 +++++++++++++++++ 2020/01/runner.ts | 29 +++++++++++ 2020/01/task.ts | 7 +++ 2021/23/task.ts | 90 +++++++++++++++++--------------- base/simpleStructure.ts | 4 ++ 18 files changed, 683 insertions(+), 42 deletions(-) create mode 100644 2015/01/runner.ts create mode 100644 2015/01/task.ts create mode 100644 2015/02/runner.ts create mode 100644 2015/02/task.ts create mode 100644 2015/03/runner.ts create mode 100644 2015/03/task.ts create mode 100644 2015/05/runner.ts create mode 100644 2015/05/task.ts create mode 100644 2015/06/runner.ts create mode 100644 2015/06/task.ts create mode 100644 2015/07/runner.ts create mode 100644 2015/07/task.ts create mode 100644 2015/08/runner.ts create mode 100644 2015/08/task.ts create mode 100644 2020/01/runner.ts create mode 100644 2020/01/task.ts diff --git a/2015/01/runner.ts b/2015/01/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/01/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/01/task.ts b/2015/01/task.ts new file mode 100644 index 0000000..5eea5fb --- /dev/null +++ b/2015/01/task.ts @@ -0,0 +1,26 @@ +export async function taskOne(input: string[]): Promise { + let count = 0; + for (let i = 0; i < input[0].length; i++) { + if (input[0][i] == '(') { + count++; + } else { + count-- + } + } + console.log(count); +} + +export async function taskTwo(input: string[]): Promise { + let count = 0 + for (let i = 0; i < input[0].length; i++) { + if (input[0][i] == '(') { + count++ + } else { + count-- + } + if (count == -1) { + console.log(i+1); + return; + } + } +} \ No newline at end of file diff --git a/2015/02/runner.ts b/2015/02/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/02/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/02/task.ts b/2015/02/task.ts new file mode 100644 index 0000000..130488d --- /dev/null +++ b/2015/02/task.ts @@ -0,0 +1,26 @@ +import { parseNumberList } from '../../base/parse' + +export async function taskOne(input: string[]): Promise { + console.log( + input.map(getPresentArea).reduce((a,b) => a+b, 0) + ) +} + +export async function taskTwo(input: string[]): Promise { + console.log( + input.map(getRibbonLength).reduce((a,b) => a+b, 0) + ) +} + +function getPresentArea(line: string) { + const l = parseNumberList(line, 'x'); + const sides = [l[0]*l[1], l[1]*l[2], l[0]*l[2]] + return 2*(sides[0]+sides[1]+sides[2]) + Math.min(...sides); +} + +function getRibbonLength(line: string) { + const l = parseNumberList(line, 'x'); + l.sort((a,b) => a-b) + + return 2*(l[0]+l[1]) + l[0]*l[1]*l[2]; +} \ No newline at end of file diff --git a/2015/03/runner.ts b/2015/03/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/03/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/03/task.ts b/2015/03/task.ts new file mode 100644 index 0000000..b1e91cb --- /dev/null +++ b/2015/03/task.ts @@ -0,0 +1,59 @@ +import { JsonSet } from '../../base/simpleStructure' + +export async function taskOne(_input: string[]): Promise { + const input = _input[0].split(''); + const set = new JsonSet(); + + const c = [0, 0] + set.add(c) + for(const i of input) { + if (i == '>') { + c[0]++ + } else if (i == '<') { + c[0]-- + } else if(i == '^') { + c[1]++ + } else { + c[1]-- + } + set.add(c) + } + console.log(set.get().size) +} + +export async function taskTwo(_input: string[]): Promise { + const input = _input[0].split(''); + const set = new JsonSet(); + + const s = [0, 0] + const r = [0, 0] + set.add(s) + let santaMoves = true; + for(const i of input) { + if (santaMoves) { + if (i == '>') { + s[0]++ + } else if (i == '<') { + s[0]-- + } else if(i == '^') { + s[1]++ + } else { + s[1]-- + } + set.add(s) + } else { + if (i == '>') { + r[0]++ + } else if (i == '<') { + r[0]-- + } else if(i == '^') { + r[1]++ + } else { + r[1]-- + } + set.add(r) + } + santaMoves = !santaMoves + } + console.log(set.get().size) +} \ No newline at end of file diff --git a/2015/05/runner.ts b/2015/05/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/05/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/05/task.ts b/2015/05/task.ts new file mode 100644 index 0000000..35ae04f --- /dev/null +++ b/2015/05/task.ts @@ -0,0 +1,48 @@ +export async function taskOne(input: string[]): Promise { + console.log(input.filter(isNice).length) + + function isNice(text: string) { + const le = text.split('') + if (le.filter(i => ['a','e','i','o','u'].includes(i)).length < 3) { + return false + } + let last = '' + let double = false + for (const l of le) { + if (last == l) double = true + if (last == 'a' && l == 'b') return false + if (last == 'c' && l == 'd') return false + if (last == 'p' && l == 'q') return false + if (last == 'x' && l == 'y') return false + last = l + } + return double + } +} + +export async function taskTwo(input: string[]): Promise { + console.log(input.filter(isNice).length) + + function isNice(text: string) { + const le = text.split('') + + let last = '' + let beforeLast = '' + const pairs: [number, string][] = [] + let withGap = false + for (const [i,l] of le.entries()) { + if (l == beforeLast) { + withGap = true + } + if (last != '') { + pairs.push([i-1, last + l]) + } + beforeLast = last + last = l + } + if (!withGap) return false + + return pairs.filter(([i, w]) => pairs.findIndex(([i2, w2]) => w == w2 && Math.abs(i-i2) > 1) >= 0).length > 0 + } +} + diff --git a/2015/06/runner.ts b/2015/06/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/06/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/06/task.ts b/2015/06/task.ts new file mode 100644 index 0000000..d12492d --- /dev/null +++ b/2015/06/task.ts @@ -0,0 +1,75 @@ +let grid: number[][] = [] + +export async function taskOne(input: string[]): Promise { + grid = Array.from({length: 1000}, () => Array.from({length: 1000}, () => 0)) + input.forEach(doInstrcution) + console.log(count()) + + function doInstrcution(ins: string) { + const r = /([^0-9]) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)/.exec(ins) + if (r == null) throw ins + const x1 = parseInt(r[2]) + const y1 = parseInt(r[3]) + const x2 = parseInt(r[4]) + const y2 = parseInt(r[5]) + let task = 0 + if (r[1] == 'n') task = 1 + if (r[1] == 'e') task = 2 + for(let x = x1; x <= x2; x++) { + for (let y = y1; y <= y2; y++) { + if (task == 0) grid[x][y] = 0 + if (task == 1) grid[x][y] = 1 + if (task == 2) grid[x][y] = grid[x][y] == 1 ? 0:1 + } + } + } + + function count() { + let i = 0; + for (let x = 0; x < 1000; x++) { + for (let y = 0; y < 1000; y++) { + if (grid[x][y] > 0) i++ + } + } + return i + } +} + +export async function taskTwo(input: string[]): Promise { + grid = Array.from({length: 1000}, () => Array.from({length: 1000}, () => 0)) + input.forEach(doInstrcution) + console.log(count()) + + function doInstrcution(ins: string) { + const r = /([^0-9]) ([0-9]+),([0-9]+) through ([0-9]+),([0-9]+)/.exec(ins) + if (r == null) throw ins + const x1 = parseInt(r[2]) + const y1 = parseInt(r[3]) + const x2 = parseInt(r[4]) + const y2 = parseInt(r[5]) + let task = 0 + if (r[1] == 'n') task = 1 + if (r[1] == 'e') task = 2 + for(let x = x1; x <= x2; x++) { + for (let y = y1; y <= y2; y++) { + if (task == 0) { + grid[x][y]-- + if (grid[x][y] < 0) grid[x][y] = 0 + } + if (task == 1) grid[x][y]++ + if (task == 2) grid[x][y] += 2 + } + } + } + + function count() { + let i = 0; + for (let x = 0; x < 1000; x++) { + for (let y = 0; y < 1000; y++) { + i += grid[x][y] + } + } + return i + } +} + diff --git a/2015/07/runner.ts b/2015/07/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/07/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/07/task.ts b/2015/07/task.ts new file mode 100644 index 0000000..2c632b9 --- /dev/null +++ b/2015/07/task.ts @@ -0,0 +1,112 @@ +const MASK = 0xFFFF +let n: Record = {} + +export async function taskOne(input: string[]): Promise { + const tasks: [string, boolean][] = input.map(i => [i, false]) + let done = 0; + let i = 0 + while (done < input.length) { + if (tasks[i][1]) { + i++ + i %= input.length + continue; + } + if (process(tasks[i][0])) { + tasks[i][1] = true + done++ + } + + i++ + i %= input.length + } + console.log(n['a']) +} + +export async function taskTwo(input: string[]): Promise { + let tasks: [string, number][] = input.map(i => [i, 0]) + let done = 0; + let i = 0 + while (done < input.length) { + if (tasks[i][1] > 0) { + i++ + i %= input.length + continue; + } + if (process(tasks[i][0])) { + tasks[i][1] = 1 + done++ + } + + i++ + i %= input.length + } + tasks = tasks.filter(([i,_]) => !i.endsWith('-> b')) + const b = n['a'] + n = {} + n['b'] = b + done = 0; + i = 0 + while (done < tasks.length) { + if (tasks[i][1] > 1) { + i++ + i %= tasks.length + continue; + } + if (process(tasks[i][0])) { + tasks[i][1] = 2 + done++ + } + + i++ + i %= tasks.length + } + console.log(n['a']) +} + +function process(ins: string) { + if (ins.includes('NOT')) { + let r = m(/NOT ([a-z0-9]+) -> ([a-z]+)/,ins) + if (!valid(r[1])) return false + n[r[2]] = (~get(r[1])) & MASK + } else if (ins.includes('AND')) { + const r = m(/([a-z0-9]+) AND ([a-z0-9]+) -> ([a-z]+)/, ins) + if (!valid(r[1]) || !valid(r[2])) return false + n[r[3]] = get(r[1]) & get(r[2]) + } else if (ins.includes('OR')) { + const r = m(/([^A]+) OR ([^-]+) -> ([a-z]+)/, ins) + if (!valid(r[1]) || !valid(r[2])) return false + n[r[3]] = get(r[1]) | get(r[2]) + } else if (ins.includes('LSHIFT')) { + const r = m(/([a-z0-9]+) LSHIFT ([0-9]+) -> ([a-z]+)/, ins) + if (!valid(r[1])) return false + n[r[3]] = (get(r[1]) << parseInt(r[2])) & MASK + } else if (ins.includes('RSHIFT')) { + const r = m(/([a-z0-9]+) RSHIFT ([0-9]+) -> ([a-z]+)/, ins) + if (!valid(r[1])) return false + n[r[3]] = get(r[1]) >> parseInt(r[2]) + } else { + const r = m(/([a-z0-9]+) -> ([a-z]+)/, ins) + if(!valid(r[1])) return false + n[r[2]] = get(r[1]) + } + return true +} + +function valid(s: string) { + return !isOp(s) || (n[s] != undefined) +} + +function get(s: string) { + if (isOp(s)) return n[s] + return parseInt(s) +} + +function m(r: RegExp, t: string) { + const re = r.exec(t) + if (!re) throw t + return re +} + +function isOp(s: string) { + return /[a-z]+/.test(s) +} \ No newline at end of file diff --git a/2015/08/runner.ts b/2015/08/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2015/08/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2015/08/task.ts b/2015/08/task.ts new file mode 100644 index 0000000..d482786 --- /dev/null +++ b/2015/08/task.ts @@ -0,0 +1,46 @@ +export async function taskOne(input: string[]): Promise { + const c = input.map(i => i.length).reduce((a,b)=>a+b,0) + const d = input.map(getMemory).reduce((a,b)=>a+b,0) + console.log(c-d) +} + +export async function taskTwo(input: string[]): Promise { + const c = input.map(i => i.length).reduce((a,b)=>a+b,0) + const d = input.map(encodedLength).reduce((a,b)=>a+b,0) + console.log(c, d) + console.log(d-c) +} + +function getMemory(t: string) { + let c = -2 + t.length + for(let i = 0; i < t.length; i++) { + if (t[i] == '\\') { + if (t[i+1] == '\\') { + i++ + c-- + } else if (t[i+1] == '"') { + c-- + } else if (t[i+1] == 'x') { + c -= 3 + } + } + } + return c +} + +function encodedLength(t: string) { + let c = t.length + 4 + for(let i = 0; i < t.length; i++) { + if (t[i] == '\\') { + if (t[i+1] == '\\') { + i++ + c+=2 + } else if (t[i+1] == '"') { + c+=2 + } else if (t[i+1] == 'x') { + c++ + } + } + } + return c +} \ No newline at end of file diff --git a/2020/01/runner.ts b/2020/01/runner.ts new file mode 100644 index 0000000..b72c86b --- /dev/null +++ b/2020/01/runner.ts @@ -0,0 +1,29 @@ +import { readFile } from "fs/promises"; +import { taskOne, taskTwo } from "./task"; + +async function main() { + let lastArgument = process.argv.pop() as string; + let taskNumber = 1; + let isTest = false; + + if (lastArgument === "test") { + isTest = true; + taskNumber = parseInt(process.argv.pop() as string); + } else { + taskNumber = parseInt(lastArgument) + } + + const fileToLoad = isTest ? "test.in" : "solve.in"; + const fileContents = await readFile(fileToLoad, "utf-8") + + const lines = fileContents.split("\n"); + + if (taskNumber === 1) { + await taskOne(lines); + } + if (taskNumber === 2) { + await taskTwo(lines); + } +} + +void main(); \ No newline at end of file diff --git a/2020/01/task.ts b/2020/01/task.ts new file mode 100644 index 0000000..3d8caba --- /dev/null +++ b/2020/01/task.ts @@ -0,0 +1,7 @@ +export async function taskOne(input: string[]): Promise { + console.log("Unimplemented"); +} + +export async function taskTwo(input: string[]): Promise { + console.log("Unimplemented"); +} \ No newline at end of file diff --git a/2021/23/task.ts b/2021/23/task.ts index ee2a6d7..e031de1 100644 --- a/2021/23/task.ts +++ b/2021/23/task.ts @@ -21,6 +21,8 @@ export async function taskOne(input: string[]): Promise { movedOut: false }] } + console.log(u) + print(u) make(u) } @@ -57,7 +59,8 @@ function make(_u: Universe) { } while(queue.length > 0) { - const q = queue.shift() as Universe + //for (let _c = 0; _c < 5; _c++) { + const q = queue.pop() as Universe console.log(queue.length, best) print(q) if (isFinished(q)) { @@ -65,49 +68,12 @@ function make(_u: Universe) { } if (q.cost >= best) continue - for (let i = 0; i < q.rooms.length; i++) { - const c = copy(q) - if (c.rooms[i][0] != undefined) { - if ((c.rooms[i][0] as Amp).movedOut) continue - (c.rooms[i][0] as Amp).movedOut = true - c.way[roomIndex.indexOf(i)] = c.rooms[i][0] - c.cost += c.rooms[i][0]?.cost??0 - c.rooms[i][0] = undefined - } else if (c.rooms[i][1] != undefined) { - if ((c.rooms[i][1] as Amp).movedOut) continue - (c.rooms[i][1] as Amp).movedOut = true - c.way[roomIndex.indexOf(i)] = c.rooms[i][1] - c.cost += 2*(c.rooms[i][1]?.cost??0) - c.rooms[i][1] = undefined - } else { - continue - } - add(c) - } - // Move all for (let i = 0; i < q.way.length; i++) { if (q.way[i] == undefined) continue - function insert(index: number, wayIndex: number): Universe|undefined { - if (q.rooms[index][0] != undefined) return undefined - if (q.rooms[index][1] != undefined || q.rooms[index][1]?.name != q.way[wayIndex]?.name) return undefined - - const c = copy(q) - c.cost += (Math.abs(roomIndex.indexOf(index)-wayIndex) + 1) * (c.way[wayIndex]?.cost??0) - if (c.rooms[index][1] == undefined) { - c.cost += (c.way[wayIndex]?.cost??0) - c.rooms[index][1] = c.way[wayIndex] - } else { - c.rooms[index][0] = c.way[wayIndex] - } - c.way[wayIndex] = undefined - add(c) - } - for (let j = i-1; j >= 0; j--) { if (q.way[j] != undefined) break - if (roomIndex[j] >= 0) insert(roomIndex[j], i) if (!validPlaces[j]) continue const c = copy(q) c.way[j] = c.way[i] @@ -117,7 +83,6 @@ function make(_u: Universe) { } for (let j = i+1; j < q.way.length; j++) { if (q.way[j] != undefined) break - if (roomIndex[j] >= 0) insert(roomIndex[j], i) if (!validPlaces[j]) continue const c = copy(q) c.way[j] = c.way[i] @@ -125,10 +90,46 @@ function make(_u: Universe) { c.cost += (c.way[j]?.cost??0) * (j-i) add(c) } + } - + for (let i = 0; i < q.way.length; i++) { + if (roomIndex[i] < 0) continue + if (q.way[i] == undefined) continue + const j = roomIndex[i] + const c = copy(q) + if (q.rooms[j][1] == undefined) { + c.rooms[j][1] = c.way[i] + c.cost += (c.way[i]?.cost??0)*2 + c.way[i] = undefined + } else if (q.rooms[j][0] == undefined) { + if (q.rooms[j][1]?.name != q.way[i]?.name) continue + c.rooms[j][0] = c.way[i] + c.cost += (c.way[i]?.cost??0) + c.way[i] = undefined + } else continue + add(c) } + for (let i = 0; i < q.way.length; i++) { + if (roomIndex[i] < 0) continue + if (q.way[i] != undefined) continue + const j = roomIndex[i] + const c = copy(q) + if (q.rooms[j][0] != undefined) { + if (q.rooms[j][0]?.movedOut) continue + c.way[i] = q.rooms[j][0]; + (c.way[i] as Amp).movedOut = true + c.cost += c.way[i]?.cost??0 + c.rooms[j][0] = undefined + } else if(q.rooms[j][1] != undefined) { + if (q.rooms[j][1]?.movedOut) continue + c.way[i] = q.rooms[j][1]; + (c.way[i] as Amp).movedOut = true + c.cost += (c.way[i]?.cost??0)*2 + c.rooms[j][1] = undefined + } else continue + add(c) + } } console.log(best) @@ -137,7 +138,12 @@ function make(_u: Universe) { function isFinished(u: Universe) { if (u.way.map(i => i != undefined).reduce((a,b)=> a||b, false)) return false - return u.rooms.map(i => i[0] != undefined && i[0].name == i[1]?.name).reduce((a,b) => a&&b, true) + if(u.rooms.map(i => i[0] != undefined && i[0].name == i[1]?.name).reduce((a,b) => a&&b, true)) { + print(u) + throw u.cost + } + return false + } function copy(u: Universe): Universe { @@ -159,7 +165,7 @@ function print(u: Universe) { console.log('#############') console.log('#' + u.way.map(i => i==undefined?'.':i.name).join('') + ' #') console.log('###' + u.rooms.map(i => i[0]==undefined?'.':i[0].name).join('#') + '###') - console.log(' #' + u.rooms.map(i => i[0]==undefined?'.':i[0].name).join('#') + '#') + console.log(' #' + u.rooms.map(i => i[1]==undefined?'.':i[1].name).join('#') + '#') console.log(' #########') console.log(' ') } \ No newline at end of file diff --git a/base/simpleStructure.ts b/base/simpleStructure.ts index c373a6a..5387783 100644 --- a/base/simpleStructure.ts +++ b/base/simpleStructure.ts @@ -96,6 +96,10 @@ class JsonSet { public remove(val: S) { this.set.delete(JSON.stringify(val)) } + + public get() { + return this.set + } } class FunctionSet {