day 7 done

This commit is contained in:
Tanishq Dubey 2022-12-09 00:08:30 -05:00
parent a3524c0f70
commit 99db573cb2
5 changed files with 1343 additions and 0 deletions

86
day7/.gitignore vendored Normal file
View File

@ -0,0 +1,86 @@
node_modules/
.node_modules/
built/*
tests/cases/rwc/*
tests/cases/test262/*
tests/cases/perf/*
!tests/cases/webharness/compilerToString.js
test-args.txt
~*.docx
\#*\#
.\#*
tests/baselines/local/*
tests/baselines/local.old/*
tests/services/baselines/local/*
tests/baselines/prototyping/local/*
tests/baselines/rwc/*
tests/baselines/test262/*
tests/baselines/reference/projectOutput/*
tests/baselines/local/projectOutput/*
tests/baselines/reference/testresults.tap
tests/services/baselines/prototyping/local/*
tests/services/browser/typescriptServices.js
src/harness/*.js
src/compiler/diagnosticInformationMap.generated.ts
src/compiler/diagnosticMessages.generated.json
src/parser/diagnosticInformationMap.generated.ts
src/parser/diagnosticMessages.generated.json
rwc-report.html
*.swp
build.json
*.actual
tests/webTestServer.js
tests/webTestServer.js.map
tests/webhost/*.d.ts
tests/webhost/webtsc.js
tests/cases/**/*.js
!tests/cases/docker/*.js/
tests/cases/**/*.js.map
*.config
scripts/eslint/built/
scripts/debug.bat
scripts/run.bat
scripts/**/*.js
scripts/**/*.js.map
coverage/
internal/
**/.DS_Store
.settings
**/.vs
**/.vscode/*
!**/.vscode/tasks.json
!**/.vscode/settings.template.json
!**/.vscode/launch.template.json
!**/.vscode/extensions.json
!tests/cases/projects/projectOption/**/node_modules
!tests/cases/projects/NodeModulesSearch/**/*
!tests/baselines/reference/project/nodeModules*/**/*
.idea
yarn.lock
yarn-error.log
.parallelperf.*
tests/cases/user/*/package-lock.json
tests/cases/user/*/node_modules/
tests/cases/user/*/**/*.js
tests/cases/user/*/**/*.js.map
tests/cases/user/*/**/*.d.ts
!tests/cases/user/zone.js/
!tests/cases/user/bignumber.js/
!tests/cases/user/discord.js/
tests/baselines/reference/dt
.failed-tests
TEST-results.xml
package-lock.json
tests/cases/user/npm/npm
tests/cases/user/TypeScript-React-Starter/TypeScript-React-Starter
tests/cases/user/TypeScript-Node-Starter/TypeScript-Node-Starter
tests/cases/user/TypeScript-React-Native-Starter/TypeScript-React-Native-Starter
tests/cases/user/TypeScript-Vue-Starter/TypeScript-Vue-Starter
tests/cases/user/TypeScript-WeChat-Starter/TypeScript-WeChat-Starter
tests/cases/user/create-react-app/create-react-app
tests/cases/user/fp-ts/fp-ts
tests/cases/user/webpack/webpack
tests/cases/user/puppeteer/puppeteer
tests/cases/user/axios-src/axios-src
tests/cases/user/prettier/prettier
.eslintcache

1089
day7/input Normal file

File diff suppressed because it is too large Load Diff

5
day7/package.json Normal file
View File

@ -0,0 +1,5 @@
{
"devDependencies": {
"@types/node": "^18.11.10"
}
}

156
day7/solution.ts Normal file
View File

@ -0,0 +1,156 @@
import { readFileSync } from 'fs';
import { join } from 'path';
enum ftype {
File = 1,
Directory,
}
class File {
fileType: ftype;
name: string;
size: number;
parent?: string;
children?: string[];
constructor(fileType: ftype, name: string, size: number, parent?: string, children?: string[]) {
this.fileType = fileType;
this.name = name;
this.size = size;
this.parent = parent;
this.children = children;
}
}
const fileString = readFileSync(join(__dirname, "input"), 'utf-8');
var commands = fileString.trim().split("$ ");
commands = commands.filter(val => {
return val.length > 0;
})
commands = commands.map(val => {
return val.trim();
})
var parsedCommandsSp = commands.map(val => {
return val.split("\n");
})
var parsedCommands = parsedCommandsSp.map(val => {
return val.map(vval => {
return vval.split(" ");
});
})
var files = {};
var currentDir = ""
function getDirSize(dirname: string): number {
const f = files[dirname];
if (f.fileType != ftype.Directory) {
return -1;
}
var sum = 0;
for (const c of f.children) {
if (files[c].fileType == ftype.File) {
sum += files[c].size;
} else {
sum += getDirSize(c);
}
}
return sum;
}
function generateFullPath(fname: string): string {
if (fname == '') {
return '';
}
return generateFullPath(files[fname].parent) + fname;
}
for (var i = 0; i < parsedCommands.length; i++) {
const commandSet = parsedCommands[i]
if (commandSet[0][0] == "cd") {
var dname = commandSet[0][1]
if (dname == "..") {
currentDir = files[currentDir].parent
} else {
const fname = generateFullPath(currentDir) + dname;
if (!(fname in files)) {
files[fname] = new File(
ftype.Directory,
fname,
0,
currentDir,
[]
);
if (currentDir != "") {
files[currentDir].children.push(fname);
}
}
currentDir = fname;
}
} else if (commandSet[0][0] == "ls") {
for (const fs of commandSet.slice(1)) {
const typeOrSize = fs[0];
const name = fs[1];
const fname = generateFullPath(currentDir) + name;
if (typeOrSize == 'dir') {
if (!(fname in files)) {
files[fname] = new File(
ftype.Directory,
fname,
0,
currentDir,
[]
);
if (currentDir != "") {
files[currentDir].children.push(fname);
}
}
} else {
const fsize: number = +typeOrSize;
if (!(fname in files)) {
files[fname] = new File(
ftype.File,
fname,
fsize,
currentDir,
[]
);
if (currentDir != "") {
files[currentDir].children.push(fname);
}
}
}
}
}
}
const LIMIT = 100000;
var fsum = 0;
var sizes = [];
for (const key in files) {
const f = files[key];
if (f.fileType == ftype.Directory) {
const s = getDirSize(key);
sizes.push(s);
if (s <= LIMIT) {
fsum += s;
}
}
}
console.log(fsum);
const TOTAL = 70000000;
const THRESH = 30000000;
const FREE = TOTAL - getDirSize("/");
var small = 1000000000000000;
for (const size of sizes) {
if ((FREE + size) > THRESH) {
if (size < small) {
small = size;
}
}
}
console.log(small);

7
day7/tsconfig.json Normal file
View File

@ -0,0 +1,7 @@
{
"compilerOptions": {
"types": [
"node"
]
}
}