Raw TS code for easier experimentations

This commit is contained in:
Simon Cambier 2023-12-05 22:19:28 +01:00
parent bcd1115d4c
commit afb9849c3c
4 changed files with 372 additions and 1 deletions

262
day5-ts/day5.ts Normal file
View File

@ -0,0 +1,262 @@
const input =
`seeds: 4239267129 20461805 2775736218 52390530 3109225152 741325372 1633502651 46906638 967445712 47092469 2354891449 237152885 2169258488 111184803 2614747853 123738802 620098496 291114156 2072253071 28111202
seed-to-soil map:
803774611 641364296 1132421037
248421506 1797371961 494535345
1936195648 2752993203 133687519
2069883167 2294485405 458507798
2804145277 283074539 358289757
3162435034 2886680722 1132532262
2528390965 4019212984 275754312
766543479 248421506 34653033
742956851 1773785333 23586628
801196512 2291907306 2578099
soil-to-fertilizer map:
2497067833 718912393 1047592994
3544660827 4222700866 72266430
770426288 3365742958 209338740
3698421476 2775964622 508284117
1441878450 1818019282 725791090
417593992 265113557 15217985
979765028 3760587444 462113422
2167669540 2543810372 143892547
3616927257 3284248739 81494219
4206705593 2687702919 88261703
2380194851 3575081698 116872982
0 280331542 15942291
718912393 1766505387 51513895
152480435 0 265113557
2311562087 3691954680 68632764
15942291 296273833 136538144
fertilizer-to-water map:
0 402310798 253353164
778924681 2773042028 194127973
2853824225 2967170001 585461563
3827117536 3909653920 385313376
4259877071 3552631564 35090225
973052654 3635167948 222704323
253353164 0 389964349
2230088185 778924681 571954391
1195756977 1490392659 342200935
2802042576 3857872271 51781649
643317513 389964349 12346449
4212430912 3587721789 47446159
3439285788 2385210280 387831748
1677471499 1832593594 552616686
1537957912 1350879072 139513587
water-to-light map:
1548505089 767179152 4433418
3833169479 2956286720 133538400
2966709060 3309731935 102304094
1552938507 844050660 203612289
4257043426 3089825120 37923870
2862957901 3567999512 28008008
127112704 319767838 4466599
840317941 174506417 34039792
2890965909 3596007520 40520529
15787022 2007458428 111325682
2398090681 21771313 152735104
1094590916 1294380254 4387553
517844904 840169267 3881393
2556445662 1535118242 8735340
1266005567 2376897884 172496096
874357733 1314885059 220233183
3696946976 2820064217 136222503
2271345339 208546209 111221629
703336145 477538609 136981796
389299157 1710880680 59057725
4183266377 2766992510 22982117
521726297 324234437 53105792
1438501663 1881931289 110003426
131579303 1298767807 16117252
2102535156 614520405 152658747
0 2549393980 15787022
1098978469 1543853582 167027098
3966707879 2789974627 30089590
2255193903 0 16151436
1756550796 377340229 100198380
574832089 2360386712 16511172
2382566968 1991934715 15523713
3069013154 3636528049 627933822
2766992510 3178543922 79332992
2931486438 3274509313 35222622
3996797469 4264461871 30505425
2846325502 3257876914 16632399
2033978459 771612570 68556697
4206248494 3127748990 50794932
2550825785 16151436 5619877
591343261 1769938405 111992884
448356882 1047662949 69488022
4027302894 3412036029 155963483
147696555 2118784110 241602602
1856749176 1117150971 177229283
light-to-temperature map:
2549521624 1806050718 400234502
1279003707 1469066403 336984315
2063720323 2518736018 367281175
4240496851 236622733 54470445
3737038415 1201359870 20798035
1170741345 1222157905 108262362
1925074187 1330420267 138646136
3757836450 291093178 323945285
3424587617 2206285220 312450798
236622733 2886017193 934118612
4138496410 1042644754 102000441
4081781735 1144645195 56714675
2431001498 615038463 118520126
1615988022 733558589 309086165
2949756126 3820135805 474831491
temperature-to-humidity map:
725888341 86282489 843183510
3782717746 1630698708 99613080
2529768467 2786969418 347392693
2195908552 2059541517 89214959
3062107482 2168182310 90554707
1730470902 3134362111 465437650
2964061476 2688923412 98046006
2285123511 2358509211 13167510
2877161160 3875960109 61807956
0 929465999 639605852
3484769060 2148756476 19425834
2298291021 1730311788 170053852
639605852 0 86282489
3504194894 2371676721 119346975
4275382932 3599799761 19584364
2468344873 2491023696 61423594
3623541869 1900365640 159175877
4138906810 2552447290 136476122
3918976473 3656029772 219930337
2938969116 4269874936 25092360
3882330826 3619384125 36645647
3152662189 3937768065 332106871
1630698708 2258737017 99772194
humidity-to-location map:
1426868383 2786540732 64165562
1639911414 2027746720 730664673
857589555 0 114197007
2370576087 1887556908 140189812
3396523523 1265337150 488817864
1491033945 2850706294 148877469
3885341387 2999583763 409625909
0 114197007 857589555
1293466489 1754155014 133401894
2510765899 3409209672 885757624
1265337150 2758411393 28129339`.split("\n");
// Maps are [dest source range]
let seeds = input[0].split(" ").map((o) => Number(o));
seeds.shift();
const seedsPairs: [number, number][] = [];
for (let i = 0; i < seeds.length; i += 2) {
seedsPairs.push([seeds[i], seeds[i + 1]]);
}
console.log(seedsPairs);
let seedToSoil: number[][] = [];
let soilToFertilizer: number[][] = [];
let fertilizerToWater: number[][] = [];
let waterToLight: number[][] = [];
let lightToTemp: number[][] = [];
let tempToHumidity: number[][] = [];
let humidityToLocation: number[][] = [];
let curr = seedToSoil;
// Roughly save input values
for (const line of input) {
if (line === "seed-to-soil map:") {
curr = seedToSoil;
}
if (line === "soil-to-fertilizer map:") {
curr = soilToFertilizer;
}
if (line === "fertilizer-to-water map:") {
curr = fertilizerToWater;
}
if (line === "water-to-light map:") {
curr = waterToLight;
}
if (line === "light-to-temperature map:") {
curr = lightToTemp;
}
if (line === "temperature-to-humidity map:") {
curr = tempToHumidity;
}
if (line === "humidity-to-location map:") {
curr = humidityToLocation;
}
curr.push(line.split(" ").map((o) => Number(o)));
}
// Clean and sort the maps
for (const map of [
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
tempToHumidity,
humidityToLocation,
]) {
map.shift();
}
function cleanInput(o: number[]): boolean {
return o.length === 3;
}
seedToSoil = seedToSoil.filter(cleanInput);
soilToFertilizer = soilToFertilizer.filter(cleanInput);
fertilizerToWater = fertilizerToWater.filter(cleanInput);
waterToLight = waterToLight.filter(cleanInput);
lightToTemp = lightToTemp.filter(cleanInput);
tempToHumidity = tempToHumidity.filter(cleanInput);
humidityToLocation = humidityToLocation.filter(cleanInput);
/**
* Given a source input and a sorted list of maps, calculate the destination value
* @param input
* @param maps
* @returns
*/
function getMapResult(input: number, maps: number[][]): number {
// Get the map corresponding to number
let mapped = -1;
for (const map of maps) {
const sourceFrom = map[1];
const sourceTo = map[1] + map[2] - 1;
if (input >= sourceFrom && input <= sourceTo) {
mapped = map[0] + input - sourceFrom;
}
}
if (mapped > -1) return mapped;
return input;
}
let minimalLocation = Number.MAX_VALUE;
for (const pair of seedsPairs) {
console.log(pair);
const to = pair[0] + pair[1] - 1;
for (let seed = pair[0]; seed < to; seed++) {
const soil = getMapResult(seed, seedToSoil);
const fert = getMapResult(soil, soilToFertilizer);
const water = getMapResult(fert, fertilizerToWater);
const light = getMapResult(water, waterToLight);
const temp = getMapResult(light, lightToTemp);
const hum = getMapResult(temp, tempToHumidity);
const loc = getMapResult(hum, humidityToLocation);
if (loc < minimalLocation) minimalLocation = loc;
}
}
console.log(minimalLocation);

109
day5-ts/tsconfig.json Normal file
View File

@ -0,0 +1,109 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */
/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}

View File

@ -6,7 +6,7 @@
/// <reference path="./input.ts"/>
// Maps are [dest source range]
const input = exampleInput;
const input = fullInput;
let seeds = input[0].split(" ").map((o) => Number(o));
seeds.shift();

0
day5/solution.ts Normal file
View File