Compare commits

...

No commits in common. "2019" and "main" have entirely different histories.
2019 ... main

120 changed files with 3896 additions and 1635600 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
**/node_modules/
node_modules/
yarn-*.cjs

View file

@ -1,100 +0,0 @@
74099
50130
81867
55356
73088
73706
55902
113399
129578
78051
117663
137454
66285
115389
50547
51588
115792
91085
118882
109486
135616
107771
90105
101182
54766
86615
91261
104321
121607
82197
68626
111255
136080
87509
70125
91180
75925
53492
96853
115081
121621
87461
116030
67335
61282
112424
106785
142243
110564
56983
131420
116534
117376
147088
117628
53964
73163
106736
76217
128590
116138
66841
109265
106285
64013
78357
125640
145761
139426
127558
135076
130989
68054
134669
144482
125870
112818
60193
107162
112557
115972
50890
148652
89547
120228
85967
103941
130915
129496
66401
87018
149539
105847
60981
82610
134396
121711
142655
104400
103752

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.6",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,20 +0,0 @@
import * as fs from "fs";
import * as readline from "readline";
async function main() {
const readStream = fs.createReadStream("input");
const lines = readline.createInterface(readStream);
const fuelReqs: number[] = [];
for await (const line of lines) {
const mass = parseInt(line, 10);
if (isNaN(mass)) {
throw new Error(`Invalid value ${mass}`);
}
fuelReqs.push(Math.floor(mass / 3) - 2);
}
return fuelReqs.reduce((x, y, i) => x + y);
}
main().then(console.log);

View file

@ -1,25 +0,0 @@
import * as fs from "fs";
import * as readline from "readline";
async function main() {
const readStream = fs.createReadStream("input");
const lines = readline.createInterface(readStream);
const fuelReqs: number[] = [];
for await (const line of lines) {
let mass = parseInt(line, 10);
if (isNaN(mass)) {
throw new Error(`Invalid value ${mass}`);
}
mass = Math.floor(mass / 3) - 2;
while (mass > 0) {
fuelReqs.push(mass);
mass = Math.floor(mass / 3) - 2;
}
}
return fuelReqs.reduce((x, y) => x + y, 0);
}
main().then(console.log);

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.6":
version "14.14.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f"
integrity sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,205 +0,0 @@
// @link https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
function gcd(a: number, b: number): number {
if (b === 0) return a;
return gcd(b, a % b);
}
/**
* Given an x,y point, returns an angle 0-360
* such that the top of the circle is 0, and then
* we rotate clockwise.
*
* So in the below ascii circle, if you start at
* point `0`, then you'll visit the points 1, 2, 3,
* etc., in order.
*
* 9 0 1
* 8 2
* 7 3
* 6 5 4
*
* In addition, my plane has negative y's going up
* and positive y's going down.
*
* -y ^
* |
* -x <---+---> +x
* |
* +y v
*/
function coordToAngle([x, y]: [number, number]) {
let deg = (Math.atan2(-y, x) * 180) / Math.PI;
// Pretty sure this can be simplified with a modulus, but can't see it
if (deg <= 90 && deg >= 0) {
deg = Math.abs(deg - 90);
} else if (deg < 0) {
deg = Math.abs(deg) + 90;
} else {
deg = 450 - deg;
}
return deg;
}
export class Grid {
min_x: number;
min_y: number;
max_x: number;
max_y: number;
grid: number[][]; // @TODO
asteroids: [number, number][];
constructor(input: (1|0)[][]) {
// `1` is an asteroid, `0` is open space
this.grid = JSON.parse(JSON.stringify(input));
this.asteroids = this.getAsteroidsList();
this.min_x = 0;
this.min_y = 0;
this.max_x = this.grid[0].length - 1;
this.max_y = this.grid.length - 1;
}
getAsteroidsList() {
let asteroids: [number, number][] = [];
for (let y = 0; y < this.grid.length; y++) {
for (let x = 0; x < this.grid[y].length; x++) {
if (this.grid[y][x]) {
asteroids.push([x, y]);
}
}
}
return asteroids;
}
getVectorsFromPoint(coord: [number, number], sorted_clockwise = false) {
let slopes: Record<string, boolean> = {};
const [x1, y1] = coord;
// This isn't optimized (its O(n^2)) but it works for grids of this size.
for (let y2 = 0; y2 <= this.max_y; y2++) {
for (let x2 = 0; x2 <= this.max_x; x2++) {
if (x1 === x2 && y1 === y2) {
continue;
}
let dy = y2 - y1;
let dx = x2 - x1;
let divisor = Math.abs(gcd(dy, dx));
dy /= divisor;
dx /= divisor;
// Technically I'm storing inverse slopes of `x/y` but that is so my `map` function below spits out `[x, y]` coords
slopes[`${dx}/${dy}`] = true;
}
}
const vectors_to_travel = Object.keys(slopes).map((slope_str) =>
slope_str.split("/").map((v) => parseInt(v, 10))
) as [number, number][];
if (sorted_clockwise) {
vectors_to_travel.sort((p1, p2) => {
let p1_d = coordToAngle(p1);
let p2_d = coordToAngle(p2);
return p1_d - p2_d;
});
}
return vectors_to_travel;
}
// Part one
getAsteroidWithHighestCountInLineOfSight() {
let best_count = -1;
let best_coords = null;
for (let asteroid of this.asteroids) {
let vectors = this.getVectorsFromPoint(asteroid);
let count = vectors
.map((vector) => (this.getCollisionAlongVector(asteroid, vector) ? 1 : 0))
.reduce((a: number, b) => a + b, 0);
if (count > best_count) {
best_count = count;
best_coords = asteroid;
}
}
return {
best_count,
best_coords,
};
}
// Part two
vaporizeAsteroidsFrom(start_from: [number, number] | null): number {
if (!start_from) {
({ best_coords: start_from } = this.getAsteroidWithHighestCountInLineOfSight());
const throw_error = (): [number, number] => {
throw new Error("start_from is null");
};
start_from = start_from ?? throw_error();
}
let total_vaporized = 0;
let vaporized = [];
do {
let clockwise_vectors_from_start = this.getVectorsFromPoint(start_from, true);
for (let vector of clockwise_vectors_from_start) {
let collision_coord = this.getCollisionAlongVector(start_from, vector);
if (collision_coord) {
total_vaporized++;
this.vaporize(collision_coord);
vaporized.push(collision_coord);
}
if (total_vaporized === 200) {
let [x, y] = collision_coord ?? [0, 0];
return x * 100 + y;
}
}
// } while (this.sumAllAsteroids() > 1);
} while (total_vaporized < 200);
return -Infinity;
}
getCollisionAlongVector(from: [number, number], vector: [number, number]) {
let collision_coord: [number, number] | null = null;
const [x, y] = from;
const [vx, vy] = vector;
let new_x = x + vx;
let new_y = y + vy;
while (this.pointInGrid(new_x, new_y)) {
if (this.grid[new_y][new_x]) {
collision_coord = [new_x, new_y];
break;
}
new_x += vx;
new_y += vy;
}
return collision_coord;
}
vaporize([x, y]: [number, number]) {
this.grid[y][x] = 0;
}
pointInGrid(x: number, y: number) {
return x >= this.min_x && x <= this.max_x && y >= this.min_y && y <= this.max_y;
}
sumAllAsteroids() {
let sum = 0;
for (let y = 0; y < this.grid.length; y++) {
for (let x = 0; x < this.grid[y].length; x++) {
sum += this.grid[y][x];
}
}
return sum;
}
}

View file

@ -1,36 +0,0 @@
....#...####.#.#...........#........
#####..#.#.#......#####...#.#...#...
##.##..#.#.#.....#.....##.#.#..#....
...#..#...#.##........#..#.......#.#
#...##...###...###..#...#.....#.....
##.......#.....#.........#.#....#.#.
..#...#.##.##.....#....##..#......#.
..###..##..#..#...#......##...#....#
##..##.....#...#.#...#......#.#.#..#
...###....#..#.#......#...#.......#.
#....#...##.......#..#.......#..#...
#...........#.....#.....#.#...#.##.#
###..#....####..#.###...#....#..#...
##....#.#..#.#......##.......#....#.
..#.#....#.#.#..#...#.##.##..#......
...#.....#......#.#.#.##.....#..###.
..#.#.###.......#..#.#....##.....#..
.#.#.#...#..#.#..##.#..........#...#
.....#.#.#...#..#..#...###.#...#.#..
#..#..#.....#.##..##...##.#.....#...
....##....#.##...#..........#.##....
...#....###.#...##........##.##..##.
#..#....#......#......###...........
##...#..#.##.##..##....#..#..##..#.#
.#....#..##.....#.#............##...
.###.........#....#.##.#..#.#..#.#..
#...#..#...#.#.#.....#....#......###
#...........##.#....#.##......#.#..#
....#...#..#...#.####...#.#..#.##...
......####.....#..#....#....#....#.#
.##.#..###..####...#.......#.#....#.
#.###....#....#..........#.....###.#
...#......#....##...##..#..#...###..
..#...###.###.........#.#..#.#..#...
.#.#.............#.#....#...........
..#...#.###...##....##.#.#.#....#.#.

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier grid.ts solution_a.ts solution_b.ts --write --print-width=120",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.8",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,12 +0,0 @@
import {Grid} from './grid'
import * as fs from "fs";
const input = fs.readFileSync("input").toString()
.split('\n')
.map(row => row.split('').map(v => (v === '#' ? 1 : 0)));
async function main() {
const grid = new Grid(input)
return grid.getAsteroidWithHighestCountInLineOfSight()
}
main().then(console.log).catch(console.error);

View file

@ -1,12 +0,0 @@
import {Grid} from './grid'
import * as fs from "fs";
const input = fs.readFileSync("input").toString()
.split('\n')
.map(row => row.split('').map(v => (v === '#' ? 1 : 0)));
async function main() {
const grid = new Grid(input)
return grid.vaporizeAsteroidsFrom(grid.getAsteroidWithHighestCountInLineOfSight().best_coords)
}
main().then(console.log).catch(console.error);

File diff suppressed because one or more lines are too long

View file

@ -1 +0,0 @@
3,8,1005,8,305,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,1002,8,1,29,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,1002,8,1,50,1,104,20,10,1,1102,6,10,1006,0,13,3,8,102,-1,8,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,83,1,1102,0,10,1006,0,96,2,1004,19,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,0,8,10,4,10,101,0,8,116,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,102,1,8,138,1006,0,60,1,1008,12,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,168,1006,0,14,1006,0,28,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,0,8,10,4,10,101,0,8,195,2,1005,9,10,1006,0,29,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,1002,8,1,224,2,1009,8,10,1,3,5,10,3,8,1002,8,-1,10,101,1,10,10,4,10,108,1,8,10,4,10,102,1,8,254,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,277,1,1003,18,10,1,1104,1,10,101,1,9,9,1007,9,957,10,1005,10,15,99,109,627,104,0,104,1,21101,0,666681062292,1,21102,322,1,0,1105,1,426,21101,847073883028,0,1,21102,333,1,0,1105,1,426,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,0,179356855319,1,21102,1,380,0,1105,1,426,21102,1,179356998696,1,21102,1,391,0,1105,1,426,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,988669698816,1,21101,0,414,0,1106,0,426,21102,1,868494500628,1,21102,425,1,0,1106,0,426,99,109,2,21202,-1,1,1,21102,1,40,2,21102,457,1,3,21102,1,447,0,1105,1,490,109,-2,2105,1,0,0,1,0,0,1,109,2,3,10,204,-1,1001,452,453,468,4,0,1001,452,1,452,108,4,452,10,1006,10,484,1102,0,1,452,109,-2,2105,1,0,0,109,4,1201,-1,0,489,1207,-3,0,10,1006,10,507,21102,0,1,-3,22101,0,-3,1,21202,-2,1,2,21101,1,0,3,21102,1,526,0,1106,0,531,109,-4,2105,1,0,109,5,1207,-3,1,10,1006,10,554,2207,-4,-2,10,1006,10,554,22101,0,-4,-4,1106,0,622,21201,-4,0,1,21201,-3,-1,2,21202,-2,2,3,21102,573,1,0,1106,0,531,21202,1,1,-4,21101,1,0,-1,2207,-4,-2,10,1006,10,592,21102,1,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,614,22101,0,-1,1,21102,614,1,0,105,1,489,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0

File diff suppressed because it is too large Load diff

View file

@ -1,385 +0,0 @@
import * as fs from "fs";
import { relative } from "path";
const fsPromises = fs.promises;
import * as readline from "readline";
import { factory } from "typescript";
const rl = readline.createInterface(process.stdin, process.stdout);
enum ParameterMode {
position = 0,
immediate = 1,
relative = 2,
}
enum Instruction {
add = 1,
mul = 2,
inp = 3,
out = 4,
jnz = 5,
jz = 6,
lt = 7,
eq = 8,
rel = 9,
end = 99,
}
type Memory = Array<OpCode>;
class OpCode {
private _executable: boolean = false;
private _literalValue: number = 0;
private _instruction?: Instruction;
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
constructor(value: number);
constructor(value: string);
constructor(value: string | number) {
let valueNumber: number;
let valueString: string;
if (typeof value === "number") {
valueNumber = value;
valueString = value.toString();
} else {
valueString = value;
valueNumber = parseInt(value);
}
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
valueString = valueString.padStart(5, "0");
this._literalValue = valueNumber;
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
Math.floor(valueNumber / 10000) % 10,
Math.floor(valueNumber / 1000) % 10,
Math.floor(valueNumber / 100) % 10,
];
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
this._parameterModes = parameterModes;
this._instruction = valueNumber % 100;
} else {
return;
}
this._executable = true;
}
}
get executable() {
if (this._instruction === undefined || this._parameterModes === undefined) {
this._executable = false;
}
return this._executable;
}
get literalValue() {
return this._literalValue;
}
get instruction() {
return this._instruction;
}
get parameterModes() {
return this._parameterModes;
}
toString() {
return this.literalValue.toString();
}
}
class Machine {
private readonly initialMemory: Memory;
private memory: Memory;
constructor(program: Memory) {
this.initialMemory = program;
this.memory = program;
}
parseParam(value: number, mode: ParameterMode, relativeOffset: number) {
switch (mode) {
case ParameterMode.immediate:
return value;
case ParameterMode.position:
return this.memory[value]?.literalValue ?? 0;
case ParameterMode.relative:
return this.memory[value + relativeOffset]?.literalValue ?? 0;
}
}
async run(
inputHandler: (context: Context) => Promise<OpCode>,
outputHandler: (context: Context) => Promise<unknown>,
debug: boolean = false
) {
let index = 0;
let relative = 0;
let debugIteration = 0;
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
let output: number[] = [];
try {
while (index < this.memory.length) {
/*for (let i = 0; i < this.memory.length; i++) {
this.memory[i] = this.memory[i] ?? new OpCode(0);
}*/
if (debug) {
debugLog!.write(
`${(++debugIteration).toString().padStart(5, "0")};${relative
.toString()
.padStart(8, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
const opCode = this.memory[index];
if (!opCode.executable) {
throw new Error(`Tried executing non-executable value ${opCode.literalValue} at index ${index}`);
}
const parameterModes = opCode.parameterModes!;
const valueA = this.memory[index + 1]?.literalValue ?? 0;
const valueB = this.memory[index + 2]?.literalValue ?? 0;
const valueC = this.memory[index + 3]?.literalValue ?? 0;
const paramA = this.parseParam(valueA, parameterModes[2 - 0], relative);
const paramB = this.parseParam(valueB, parameterModes[2 - 1], relative);
const paramC = this.parseParam(valueC, parameterModes[2 - 2], relative);
if (debug) {
console.debug(`${opCode.literalValue}(${valueA}, ${valueB}, ${valueC});${this.memory[1028]?.literalValue}`);
}
const context: Context = {
parameterModes,
valueA,
valueB,
valueC,
paramA,
paramB,
paramC,
debug,
index,
relative,
opCode,
output,
};
switch (opCode.instruction) {
case Instruction.add:
if (debug) {
console.debug(
`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA + paramB);
} else {
this.memory[valueC] = new OpCode(paramA + paramB);
}
index += 4;
break;
case Instruction.mul:
if (debug) {
console.debug(
`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC} - OpCode ${
opCode.literalValue
}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA * paramB);
} else {
this.memory[valueC] = new OpCode(paramA * paramB);
}
index += 4;
break;
case Instruction.inp:
let input = await inputHandler(context);
if (parameterModes[2 - 0] === ParameterMode.relative) {
this.memory[valueA + relative] = input;
} else {
this.memory[valueA] = input;
}
index += 2;
break;
case Instruction.out:
outputHandler(context);
index += 2;
break;
case Instruction.jnz:
if (debug) {
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
}
if (paramA !== 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.jz:
if (debug) {
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
}
if (paramA === 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.lt:
if (debug) {
console.debug(
`Checking if ${paramA}<${paramB} and writing result to ${
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA < paramB ? 1 : 0);
} else {
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
}
index += 4;
break;
case Instruction.eq:
if (debug) {
console.debug(
`Checking if ${paramA}==${paramB} and writing result to ${
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA === paramB ? 1 : 0);
} else {
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
}
index += 4;
break;
case Instruction.rel:
if (debug) {
console.debug(
`Adjusting relative offset from ${relative} +${paramA} to ${relative + paramA} - OpCode ${
opCode.literalValue
}`
);
}
relative += paramA;
index += 2;
break;
case Instruction.end:
// console.log("Final output:", output);
return this.memory;
}
}
} catch (e) {
console.debug(`Execution failed at index ${index}. Dumping memory.`);
if (debug) {
debugLog!.write(
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
throw e;
}
// console.log("Final output:", output);
return this.memory;
}
reset() {
this.memory = this.initialMemory;
}
static memdump(memory: Memory) {
return memory.map((op) => op.toString()).join(",");
}
}
interface Context {
parameterModes: [ParameterMode, ParameterMode, ParameterMode];
valueA: number;
valueB: number;
valueC: number;
paramA: number;
paramB: number;
paramC: number;
debug: boolean;
index: number;
relative: number;
opCode: OpCode;
output: number[];
}
async function main(code: string): Promise<[number, string]> {
let painted = new Set<string>();
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
const machine = new Machine(memdump);
async function input(context: Context) {
if (context.debug) {
console.debug("inputting", field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
}
return new OpCode(field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
}
async function output(context: Context) {
if (context.debug) {
console.debug(`Outputting ${context.paramA} - OpCode ${context.opCode.literalValue}`);
}
context.output.push(context.paramA);
if (awaiting === "color") {
awaiting = "turning";
const newColor = context.paramA === 1 ? "⬜" : "⬛";
field[coords[1]][coords[0]] = newColor;
} else {
awaiting = "color";
let turning = context.paramA === 0 ? -1 : context.paramA;
if (!painted.has(`${coords[0]};${coords[1]}`)) {
painted.add(`${coords[0]};${coords[1]}`);
}
facing = ((facing + turning + 4) % 4) as 0 | 1 | 2 | 3;
switch (facing) {
case 0:
coords[1] = coords[1] - 1;
break;
case 1:
coords[0] = coords[0] + 1;
break;
case 2:
coords[1] = coords[1] + 1;
break;
case 3:
coords[0] = coords[0] - 1;
break;
}
}
}
let field: ("⬜" | "⬛")[][] = Array<("⬜" | "⬛")[]>(86)
.fill([])
.map((_) => []);
let coords: [number, number] = [0, 72];
let facing: 0 | 1 | 2 | 3 = 0;
let awaiting: "turning" | "color" = "color";
await machine.run(input, output);
rl.close();
return [painted.size, field.map((line) => line.join("")).join("\n")];
}
const input = fs.readFileSync("input").toString();
main(input)
.then(([count, field]) => {
console.log(count);
// console.log(field);
})
.catch((...args) => {
rl.close();
console.error(args);
});

View file

@ -1,386 +0,0 @@
import * as fs from "fs";
import { relative } from "path";
const fsPromises = fs.promises;
import * as readline from "readline";
import { factory } from "typescript";
const rl = readline.createInterface(process.stdin, process.stdout);
enum ParameterMode {
position = 0,
immediate = 1,
relative = 2,
}
enum Instruction {
add = 1,
mul = 2,
inp = 3,
out = 4,
jnz = 5,
jz = 6,
lt = 7,
eq = 8,
rel = 9,
end = 99,
}
type Memory = Array<OpCode>;
class OpCode {
private _executable: boolean = false;
private _literalValue: number = 0;
private _instruction?: Instruction;
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
constructor(value: number);
constructor(value: string);
constructor(value: string | number) {
let valueNumber: number;
let valueString: string;
if (typeof value === "number") {
valueNumber = value;
valueString = value.toString();
} else {
valueString = value;
valueNumber = parseInt(value);
}
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
valueString = valueString.padStart(5, "0");
this._literalValue = valueNumber;
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
Math.floor(valueNumber / 10000) % 10,
Math.floor(valueNumber / 1000) % 10,
Math.floor(valueNumber / 100) % 10,
];
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
this._parameterModes = parameterModes;
this._instruction = valueNumber % 100;
} else {
return;
}
this._executable = true;
}
}
get executable() {
if (this._instruction === undefined || this._parameterModes === undefined) {
this._executable = false;
}
return this._executable;
}
get literalValue() {
return this._literalValue;
}
get instruction() {
return this._instruction;
}
get parameterModes() {
return this._parameterModes;
}
toString() {
return this.literalValue.toString();
}
}
class Machine {
private readonly initialMemory: Memory;
private memory: Memory;
constructor(program: Memory) {
this.initialMemory = program;
this.memory = program;
}
parseParam(value: number, mode: ParameterMode, relativeOffset: number) {
switch (mode) {
case ParameterMode.immediate:
return value;
case ParameterMode.position:
return this.memory[value]?.literalValue ?? 0;
case ParameterMode.relative:
return this.memory[value + relativeOffset]?.literalValue ?? 0;
}
}
async run(
inputHandler: (context: Context) => Promise<OpCode>,
outputHandler: (context: Context) => Promise<unknown>,
debug: boolean = false
) {
let index = 0;
let relative = 0;
let debugIteration = 0;
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
let output: number[] = [];
try {
while (index < this.memory.length) {
/*for (let i = 0; i < this.memory.length; i++) {
this.memory[i] = this.memory[i] ?? new OpCode(0);
}*/
if (debug) {
debugLog!.write(
`${(++debugIteration).toString().padStart(5, "0")};${relative
.toString()
.padStart(8, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
const opCode = this.memory[index];
if (!opCode.executable) {
throw new Error(`Tried executing non-executable value ${opCode.literalValue} at index ${index}`);
}
const parameterModes = opCode.parameterModes!;
const valueA = this.memory[index + 1]?.literalValue ?? 0;
const valueB = this.memory[index + 2]?.literalValue ?? 0;
const valueC = this.memory[index + 3]?.literalValue ?? 0;
const paramA = this.parseParam(valueA, parameterModes[2 - 0], relative);
const paramB = this.parseParam(valueB, parameterModes[2 - 1], relative);
const paramC = this.parseParam(valueC, parameterModes[2 - 2], relative);
if (debug) {
console.debug(`${opCode.literalValue}(${valueA}, ${valueB}, ${valueC});${this.memory[1028]?.literalValue}`);
}
const context: Context = {
parameterModes,
valueA,
valueB,
valueC,
paramA,
paramB,
paramC,
debug,
index,
relative,
opCode,
output,
};
switch (opCode.instruction) {
case Instruction.add:
if (debug) {
console.debug(
`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA + paramB);
} else {
this.memory[valueC] = new OpCode(paramA + paramB);
}
index += 4;
break;
case Instruction.mul:
if (debug) {
console.debug(
`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC} - OpCode ${
opCode.literalValue
}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA * paramB);
} else {
this.memory[valueC] = new OpCode(paramA * paramB);
}
index += 4;
break;
case Instruction.inp:
let input = await inputHandler(context);
if (parameterModes[2 - 0] === ParameterMode.relative) {
this.memory[valueA + relative] = input;
} else {
this.memory[valueA] = input;
}
index += 2;
break;
case Instruction.out:
outputHandler(context);
index += 2;
break;
case Instruction.jnz:
if (debug) {
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
}
if (paramA !== 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.jz:
if (debug) {
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
}
if (paramA === 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.lt:
if (debug) {
console.debug(
`Checking if ${paramA}<${paramB} and writing result to ${
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA < paramB ? 1 : 0);
} else {
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
}
index += 4;
break;
case Instruction.eq:
if (debug) {
console.debug(
`Checking if ${paramA}==${paramB} and writing result to ${
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA === paramB ? 1 : 0);
} else {
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
}
index += 4;
break;
case Instruction.rel:
if (debug) {
console.debug(
`Adjusting relative offset from ${relative} +${paramA} to ${relative + paramA} - OpCode ${
opCode.literalValue
}`
);
}
relative += paramA;
index += 2;
break;
case Instruction.end:
// console.log("Final output:", output);
return this.memory;
}
}
} catch (e) {
console.debug(`Execution failed at index ${index}. Dumping memory.`);
if (debug) {
debugLog!.write(
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
throw e;
}
// console.log("Final output:", output);
return this.memory;
}
reset() {
this.memory = this.initialMemory;
}
static memdump(memory: Memory) {
return memory.map((op) => op.toString()).join(",");
}
}
interface Context {
parameterModes: [ParameterMode, ParameterMode, ParameterMode];
valueA: number;
valueB: number;
valueC: number;
paramA: number;
paramB: number;
paramC: number;
debug: boolean;
index: number;
relative: number;
opCode: OpCode;
output: number[];
}
async function main(code: string): Promise<[number, string]> {
let painted = new Set<string>();
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
const machine = new Machine(memdump);
async function input(context: Context) {
if (context.debug) {
console.debug("inputting", field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
}
return new OpCode(field[coords[1]][coords[0]] === "⬜" ? 1 : 0);
}
async function output(context: Context) {
if (context.debug) {
console.debug(`Outputting ${context.paramA} - OpCode ${context.opCode.literalValue}`);
}
context.output.push(context.paramA);
if (awaiting === "color") {
awaiting = "turning";
const newColor = context.paramA === 1 ? "⬜" : "⬛";
field[coords[1]][coords[0]] = newColor;
} else {
awaiting = "color";
let turning = context.paramA === 0 ? -1 : context.paramA;
if (!painted.has(`${coords[0]};${coords[1]}`)) {
painted.add(`${coords[0]};${coords[1]}`);
}
facing = ((facing + turning + 4) % 4) as 0 | 1 | 2 | 3;
switch (facing) {
case 0:
coords[1] = coords[1] - 1;
break;
case 1:
coords[0] = coords[0] + 1;
break;
case 2:
coords[1] = coords[1] + 1;
break;
case 3:
coords[0] = coords[0] - 1;
break;
}
}
}
let field: ("⬜" | "⬛")[][] = Array<("⬜" | "⬛")[]>(7)
.fill([])
.map((_) => Array(43).fill("⬛"));
let coords: [number, number] = [0, 1];
let facing: 0 | 1 | 2 | 3 = 0;
let awaiting: "turning" | "color" = "color";
field[coords[1]][coords[0]] = "⬜"
await machine.run(input, output);
rl.close();
return [painted.size, field.map((line) => line.join("")).join("\n")];
}
const input = fs.readFileSync("input").toString();
main(input)
.then(([count, field]) => {
console.log(count);
console.log(field);
})
.catch((...args) => {
rl.close();
console.error(args);
});

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1 +0,0 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,5,19,23,1,23,5,27,2,27,10,31,1,5,31,35,2,35,6,39,1,6,39,43,2,13,43,47,2,9,47,51,1,6,51,55,1,55,9,59,2,6,59,63,1,5,63,67,2,67,13,71,1,9,71,75,1,75,9,79,2,79,10,83,1,6,83,87,1,5,87,91,1,6,91,95,1,95,13,99,1,10,99,103,2,6,103,107,1,107,5,111,1,111,13,115,1,115,13,119,1,13,119,123,2,123,13,127,1,127,6,131,1,131,9,135,1,5,135,139,2,139,6,143,2,6,143,147,1,5,147,151,1,151,2,155,1,9,155,0,99,2,14,0,0

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.6",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,47 +0,0 @@
import * as fs from 'fs';
const input = fs.readFileSync("input").toString().split(",").map(num => parseInt(num));
async function main() {
let memory = input.slice();
// Restore integrity
memory[1] = 12
memory[2] = 2
//
let index = 0;
let [a,b,x] = [0,0,0];
while(index < memory.length && memory[index] !== 99) {
switch(memory[index]){
case 1:
// .add
[a, b, x] = memory.slice(index+1, index+4);
memory[x] = memory[a] + memory[b];
index += 4;
break;
case 2:
// .mul
[a, b, x] = memory.slice(index+1, index+4);
memory[x] = memory[a] * memory[b];
index += 4;
break;
case 99:
// .end
return memory[0];
default:
// .hcf
throw new Error("Halt and catch fire");
}
}
console.warn("Reached end of code without halting instruction!")
return memory[0]
}
main().then(console.log);

View file

@ -1,64 +0,0 @@
import * as fs from 'fs';
const input = fs.readFileSync("input").toString().split(",").map(num => parseInt(num));
async function run(noun: number, verb: number) {
let memory = input.slice();
// Restore integrity
memory[1] = noun
memory[2] = verb
//
let index = 0;
let [a,b,x] = [0,0,0];
while(index < memory.length && memory[index] !== 99) {
switch(memory[index]){
case 1:
// .add
[a, b, x] = memory.slice(index+1, index+4);
memory[x] = memory[a] + memory[b];
index += 4;
break;
case 2:
// .mul
[a, b, x] = memory.slice(index+1, index+4);
memory[x] = memory[a] * memory[b];
index += 4;
break;
case 99:
// .end
return memory[0];
default:
// .hcf
throw new Error("Halt and catch fire");
}
}
return memory[0]
}
async function main(){
let noun = 0;
let verb = 0;
while(noun < 99) {
while(verb < 99) {
console.log(`Testing [${noun}, ${verb}]`)
if(await run(noun,verb) === 19690720) {
console.log("Success!")
return 100*noun+verb
}
verb++;
}
noun++;
verb = 0;
}
throw new Error("Found no solution!")
}
main().then(console.log);

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.6":
version "14.14.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f"
integrity sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1,2 +0,0 @@
R999,D467,L84,D619,L49,U380,R287,U80,R744,D642,L340,U738,R959,U710,R882,U861,L130,D354,L579,D586,R798,D735,L661,D453,L828,U953,R604,D834,R921,D348,R620,U775,R364,U552,L221,U119,R590,U29,L267,D745,L128,U468,L978,D717,R883,D227,R691,D330,L33,U520,L862,D132,R99,U400,L455,U737,L603,U220,L689,U131,R158,D674,R617,D287,R422,U734,L73,U327,L525,D245,R849,D692,R114,U136,R762,D5,R329,U429,L849,U748,R816,U556,R614,D412,R416,D306,R307,U826,R880,U936,L164,U984,L689,D934,R790,D14,R561,D736,L3,D442,R301,D520,L451,U76,R844,D307,L144,D800,L462,D138,R956,U225,L393,D186,L924,D445,L86,D640,L920,D877,L197,U191,L371,D701,R826,D282,R856,D412,L788,D417,R69,D678,R978,D268,L268,U112,L69,U164,L748,U191,R227,D227,R59,U749,R134,U779,R865,U247,R55,D567,R821,U799,R937,D942,L445,D571,R685,D111,R107,D769,R269,D968,R102,U335,R538,U125,L725,D654,R451,D242,R777,U813,R799,D786,L804,U313,L322,U771,R219,U316,L973,U963,R84,D289,R825,D299,L425,D49,R995,D486,R550,D789,R735,D501,R966,U955,R432,U635,L353,D600,R675,D236,R864,U322,R719,D418,L877,U833,R839,D634,L533,D438,L734,U130,L578,U498,L984,D413,L615,U40,L699,U656,L653,U419,R856,U882,R30,D266,R386,D692,L210,U802,L390,U753,L406,U338,R743,D320,L125,U204,R391,U537,R887,D194,L302,U400,R510,U92,L310,D382,R597,U498,R851,D357,L568,U800,R918,D106,R673,D735,L86,D67,R398,D677,R355,D501,L909,D133,R729,D293,L498,U222,R832,U671,R751,U36,R422,U840,L636,D476,L292,D105,L239,U199,R669,U736,L345,D911,L277,U452,L979,D153,R882,U604,R602,U495,L311,U453,L215,D713,R873
L996,U773,L865,D472,L988,D570,L388,U458,L87,U885,L115,U55,R75,U582,R695,U883,R83,U285,R96,D244,L647,D359,R136,U107,R912,U871,L844,U395,L63,U899,L205,D137,R549,U221,L859,D429,L809,U127,R304,U679,L511,U144,R926,U95,L805,U811,R42,D248,L546,D644,L551,D897,R368,D391,L224,U164,L490,D991,L146,D615,R536,U247,R10,U998,L957,D233,R706,D926,R760,U438,R270,D983,R134,U738,L262,U301,L480,D635,L702,D715,R479,U500,R19,D291,R368,U203,R305,D999,R106,U355,L683,D298,R90,U968,L254,D936,R89,U496,R253,U688,R99,U637,L783,D451,R673,D762,R997,D50,L488,U551,L871,U388,R949,D371,R584,D908,L880,U523,R557,U436,R520,U587,L56,U18,R397,U541,R660,D444,R51,U187,R221,U902,R726,U303,R97,D817,R185,D218,L240,D67,L259,U334,R821,U629,R21,D970,R282,U155,R555,D678,L99,D570,R863,D405,R941,U584,L303,D109,L871,U180,R595,D226,L670,D943,L127,U647,R452,D570,R75,D284,R414,U404,R515,U993,R408,U488,R890,D318,L415,U969,R769,D976,L732,U1,R489,U655,R930,U638,R649,D254,R161,U287,L659,D26,L477,D821,L124,U538,R17,D711,L203,U888,R904,U648,L908,D65,L215,U283,R698,U28,R72,U214,R499,D89,R489,D58,R949,D91,L710,U960,L755,D402,L27,D873,R61,U607,R57,D548,R369,U622,L244,U19,R61,D606,R928,D968,R10,D988,R816,U500,R915,D400,R546,D283,L627,D919,L259,U337,R374,U795,L355,D989,L224,D77,L872,U901,R476,U765,L320,U768,L937,D437,R141,D822,L326,D324,L498,U994,L518,D857,R973,D681,L710,D590,L879,U499,R488,D151,L242,U988,L944,U683,L24,U491,R823,D246,R872,D654,R28,U581,L142,U31,R435,D686,L147,D102,R952,D607,L959,D929,L46

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.6",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,61 +0,0 @@
import * as fs from "fs";
const input = fs
.readFileSync("input")
.toString()
.split("\n")
.map((inp) => inp.split(","));
async function trace(ops: string[]) {
let board: boolean[][] = [];
let x = 0;
let y = 0;
for (const op of ops) {
const dir = op.substring(0, 1);
let len = parseInt(op.substring(1));
switch (dir) {
case "R":
while (len-- > 0) {
(board[y] ?? (board[y] = []))[++x] = true;
}
break;
case "L":
while (len-- > 0) {
(board[y] ?? (board[y] = []))[--x] = true;
}
break;
case "U":
while (len-- > 0) {
y++;
(board[y] ?? (board[y] = []))[x] = true;
}
break;
case "D":
while (len-- > 0) {
y--;
(board[y] ?? (board[y] = []))[x] = true;
}
break;
}
}
return board;
}
async function main() {
const [wireA, wireB] = [await trace(input[0]), await trace(input[1])];
const crossings: [number, number][] = [];
for (let y = 0; y < Math.min(wireA.length, wireB.length); y++) {
for (let x = 0; x < Math.min(wireA[y].length, wireB[y].length); x++) {
if (wireA[y][x] && wireB[y][x]) {
crossings.push([y, x]);
}
}
}
return crossings;
}
main().then((crossings) =>
console.log(Math.min(...crossings.map((coords) => coords[0] + coords[1])))
);

View file

@ -1,74 +0,0 @@
import * as fs from "fs";
const input = fs
.readFileSync("input")
.toString()
.split("\n")
.map((inp) => inp.split(","));
async function trace(ops: string[]) {
let board: number[][] = [];
let steps = 0;
let x = 0;
let y = 0;
for (const op of ops) {
const dir = op.substring(0, 1);
let len = parseInt(op.substring(1));
switch (dir) {
case "R":
while (len-- > 0) {
(board[y] ?? (board[y] = []))[++x] = Math.min(
board[y][x] ?? Infinity,
++steps
);
}
break;
case "L":
while (len-- > 0) {
(board[y] ?? (board[y] = []))[--x] = Math.min(
board[y][x] ?? Infinity,
++steps
);
}
break;
case "U":
while (len-- > 0) {
y++;
(board[y] ?? (board[y] = []))[x] = Math.min(
board[y][x] ?? Infinity,
++steps
);
}
break;
case "D":
while (len-- > 0) {
y--;
(board[y] ?? (board[y] = []))[x] = Math.min(
board[y][x] ?? Infinity,
++steps
);
}
break;
}
}
return board;
}
async function main() {
const [wireA, wireB] = [await trace(input[0]), await trace(input[1])];
const crossings: [y: number, x: number, val: number][] = [];
for (let y = 0; y < Math.min(wireA.length, wireB.length); y++) {
for (let x = 0; x < Math.min(wireA[y].length, wireB[y].length); x++) {
if (wireA[y][x] > 0 && wireB[y][x] > 0) {
crossings.push([y, x, wireA[y][x] + wireB[y][x]]);
}
}
}
return crossings.sort(
(crossingA, crossingB) => crossingA[2] - crossingB[2]
)[0];
}
main().then(console.log);

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "Node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.6":
version "14.14.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f"
integrity sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1 +0,0 @@
153517-630395

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.6",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,43 +0,0 @@
import * as fs from "fs";
const input = fs.readFileSync("input").toString();
function verifyNotDecreasing(pass: string) {
const digits = pass.split("").map((digit) => parseInt(digit));
for (let i = 0; i < digits.length - 1; i++) {
if (digits[i + 1] < digits[i]) {
return false;
}
}
return true;
}
function verifyHasDouble(pass: string) {
const digits = pass.split("").map((digit) => parseInt(digit));
for (let i = 0; i < digits.length - 1; i++) {
if (digits[i + 1] === digits[i]) {
return true;
}
}
return false;
}
function verifyPass(pass: number) {
return (
!pass.toString().startsWith("0") &&
verifyNotDecreasing(pass.toString()) &&
verifyHasDouble(pass.toString())
);
}
async function main() {
const [min, max] = input.split("-").map((val) => parseInt(val));
let results = 0;
for (let i = min; i <= max; i++) {
if (verifyPass(i)) {
results++;
}
}
return results;
}
main().then(console.log);

View file

@ -1,47 +0,0 @@
import * as fs from "fs";
const input = fs.readFileSync("input").toString();
function verifyNotDecreasing(pass: string) {
const digits = pass.split("").map((digit) => parseInt(digit));
for (let i = 0; i < digits.length - 1; i++) {
if (digits[i + 1] < digits[i]) {
return false;
}
}
return true;
}
function verifyHasDoubleButNotMore(pass: string) {
const digits = pass.split("").map((digit) => parseInt(digit));
for (let i = 0; i < digits.length; i++) {
if (
digits[i] === digits[i + 1] &&
digits[i] !== digits[i + 2] &&
digits[i] !== digits[i - 1]
) {
return true;
}
}
return false;
}
function verifyPass(pass: number) {
return (
pass >= 1e5 &&
verifyNotDecreasing(pass.toString()) &&
verifyHasDoubleButNotMore(pass.toString())
);
}
async function main() {
const [min, max] = input.split("-").map((val) => parseInt(val));
let results = 0;
for (let i = min; i <= max; i++) {
if (verifyPass(i)) {
results++;
}
}
return results;
}
main().then(console.log);

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.6":
version "14.14.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f"
integrity sha512-6QlRuqsQ/Ox/aJEQWBEJG7A9+u7oSYl3mem/K8IzxXG/kAGbV1YPD9Bg9Zw3vyxC/YP+zONKwy8hGkSt1jxFMw==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1 +0,0 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1101,40,27,224,101,-67,224,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1101,33,38,225,1102,84,60,225,1101,65,62,225,1002,36,13,224,1001,224,-494,224,4,224,1002,223,8,223,1001,224,3,224,1,223,224,223,1102,86,5,224,101,-430,224,224,4,224,1002,223,8,223,101,6,224,224,1,223,224,223,1102,23,50,225,1001,44,10,224,101,-72,224,224,4,224,102,8,223,223,101,1,224,224,1,224,223,223,102,47,217,224,1001,224,-2303,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1102,71,84,225,101,91,40,224,1001,224,-151,224,4,224,1002,223,8,223,1001,224,5,224,1,223,224,223,1101,87,91,225,1102,71,19,225,1,92,140,224,101,-134,224,224,4,224,1002,223,8,223,101,1,224,224,1,224,223,223,2,170,165,224,1001,224,-1653,224,4,224,1002,223,8,223,101,5,224,224,1,223,224,223,1101,49,32,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1107,226,677,224,1002,223,2,223,1006,224,329,101,1,223,223,8,226,226,224,1002,223,2,223,1005,224,344,101,1,223,223,1007,677,226,224,102,2,223,223,1005,224,359,101,1,223,223,8,226,677,224,102,2,223,223,1005,224,374,101,1,223,223,1107,677,677,224,1002,223,2,223,1005,224,389,1001,223,1,223,108,226,677,224,102,2,223,223,1005,224,404,1001,223,1,223,108,677,677,224,1002,223,2,223,1006,224,419,101,1,223,223,107,677,677,224,102,2,223,223,1006,224,434,101,1,223,223,108,226,226,224,1002,223,2,223,1006,224,449,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,464,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,479,1001,223,1,223,1108,677,677,224,1002,223,2,223,1005,224,494,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,509,101,1,223,223,1007,677,677,224,1002,223,2,223,1005,224,524,101,1,223,223,7,677,226,224,1002,223,2,223,1005,224,539,101,1,223,223,1107,677,226,224,102,2,223,223,1006,224,554,101,1,223,223,107,226,677,224,1002,223,2,223,1005,224,569,101,1,223,223,107,226,226,224,1002,223,2,223,1005,224,584,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,599,1001,223,1,223,1008,677,677,224,102,2,223,223,1006,224,614,101,1,223,223,7,226,677,224,102,2,223,223,1005,224,629,101,1,223,223,1008,226,677,224,1002,223,2,223,1006,224,644,101,1,223,223,1007,226,226,224,1002,223,2,223,1005,224,659,1001,223,1,223,1008,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226

View file

@ -1,17 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write --print-width=120",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.8",
"readline": "^1.3.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,207 +0,0 @@
import * as fs from "fs";
const fsPromises = fs.promises;
import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
enum ParameterMode {
position = 0,
immediate = 1,
}
enum Instruction {
add = 1,
mul = 2,
inp = 3,
out = 4,
end = 99,
}
type Memory = Array<OpCode>;
class OpCode {
private _executable: boolean = false;
private _literalValue: number = 0;
private _instruction?: Instruction;
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
constructor(value: number);
constructor(value: string);
constructor(value: string | number) {
let valueNumber: number;
let valueString: string;
if (typeof value === "number") {
valueNumber = value;
valueString = value.toString();
} else {
valueString = value;
valueNumber = parseInt(value);
}
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
valueString = valueString.padStart(5, "0");
this._literalValue = valueNumber;
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
Math.floor(valueNumber / 10000) % 10,
Math.floor(valueNumber / 1000) % 10,
Math.floor(valueNumber / 100) % 10,
];
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
this._parameterModes = parameterModes;
this._instruction = valueNumber % 100;
} else {
return;
}
this._executable = true;
}
}
get executable() {
if (this._instruction === undefined || this._parameterModes === undefined) {
this._executable = false;
}
return this._executable;
}
get literalValue() {
return this._literalValue;
}
get instruction() {
return this._instruction;
}
get parameterModes() {
return this._parameterModes;
}
toString() {
return this.literalValue.toString();
}
}
class Machine {
private readonly initialMemory: Memory;
private memory: Memory;
constructor(program: Memory) {
this.initialMemory = program;
this.memory = program;
}
async run(debug: boolean = false) {
let index = 0;
let debugIteration = 0;
const debugLog = await fsPromises.open("out.log", "w");
let output: number[] = [];
try {
while (index < this.memory.length) {
/*for (let i = 0; i < this.memory.length; i++) {
this.memory[i] = this.memory[i] ?? new OpCode(0);
}*/
if (debug) {
debugLog.write(
`${(++debugIteration).toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
const opCode = this.memory[index];
if (!opCode.executable) {
throw new Error(`Tried executing non-executable value ${this.memory[index]} at index ${index}`);
}
const parameterModes = opCode.parameterModes!;
const valueA = this.memory[index + 1]?.literalValue ?? 0;
const valueB = this.memory[index + 2]?.literalValue ?? 0;
const valueC = this.memory[index + 3]?.literalValue ?? 0;
const paramA = parameterModes[2] === ParameterMode.immediate ? valueA : this.memory[valueA]?.literalValue ?? 0;
const paramB = parameterModes[1] === ParameterMode.immediate ? valueB : this.memory[valueB]?.literalValue ?? 0;
const paramC = parameterModes[0] === ParameterMode.immediate ? valueC : this.memory[valueC]?.literalValue ?? 0;
/*console.debug(
opCode,
this.memory.slice(index, index + 4).map((val) => val.literalValue)
);*/
switch (opCode.instruction) {
case Instruction.add:
if (debug) {
console.debug(`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA + paramB);
index += 4;
break;
case Instruction.mul:
if (debug) {
console.debug(`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA * paramB);
index += 4;
break;
case Instruction.inp:
this.memory[valueA] = await new Promise<OpCode>((resolve, reject) => {
rl.question(`[${index.toString().padStart(5, "0")}]> `, (answer) => {
try {
const op = new OpCode(answer);
if (debug) {
console.debug(`Writing opCode with value ${op.literalValue} to ${valueA}`);
}
resolve(op);
} catch (e) {
reject(e);
}
});
});
index += 2;
break;
case Instruction.out:
if (debug) {
console.debug(`Outputting ${paramA}`);
}
output.push(paramA);
index += 2;
break;
case Instruction.end:
console.log("Final output:", output);
return this.memory;
}
}
} catch (e) {
console.debug(`Execution failed at index ${index}. Dumping memory.`);
debugLog.write(
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
throw e;
}
console.log("Final output:", output);
return this.memory;
}
reset() {
this.memory = this.initialMemory;
}
static memdump(memory: Memory) {
return memory.map((op) => op.toString()).join(",");
}
}
async function main(code: string) {
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
const machine = new Machine(memdump);
const result = await machine.run();
rl.close();
return; // result ? Machine.memdump(result) : result;
}
const input = fs.readFileSync("input").toString();
main(input)
.then(console.log)
.catch((...args) => {
rl.close();
console.error(args);
});

View file

@ -1,247 +0,0 @@
import * as fs from "fs";
const fsPromises = fs.promises;
import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
enum ParameterMode {
position = 0,
immediate = 1,
}
enum Instruction {
add = 1,
mul = 2,
inp = 3,
out = 4,
jnz = 5,
jz = 6,
lt = 7,
eq = 8,
end = 99,
}
type Memory = Array<OpCode>;
class OpCode {
private _executable: boolean = false;
private _literalValue: number = 0;
private _instruction?: Instruction;
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
constructor(value: number);
constructor(value: string);
constructor(value: string | number) {
let valueNumber: number;
let valueString: string;
if (typeof value === "number") {
valueNumber = value;
valueString = value.toString();
} else {
valueString = value;
valueNumber = parseInt(value);
}
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
valueString = valueString.padStart(5, "0");
this._literalValue = valueNumber;
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
Math.floor(valueNumber / 10000) % 10,
Math.floor(valueNumber / 1000) % 10,
Math.floor(valueNumber / 100) % 10,
];
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
this._parameterModes = parameterModes;
this._instruction = valueNumber % 100;
} else {
return;
}
this._executable = true;
}
}
get executable() {
if (this._instruction === undefined || this._parameterModes === undefined) {
this._executable = false;
}
return this._executable;
}
get literalValue() {
return this._literalValue;
}
get instruction() {
return this._instruction;
}
get parameterModes() {
return this._parameterModes;
}
toString() {
return this.literalValue.toString();
}
}
class Machine {
private readonly initialMemory: Memory;
private memory: Memory;
constructor(program: Memory) {
this.initialMemory = program;
this.memory = program;
}
async run(debug: boolean = false) {
let index = 0;
let debugIteration = 0;
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
let output: number[] = [];
try {
while (index < this.memory.length) {
/*for (let i = 0; i < this.memory.length; i++) {
this.memory[i] = this.memory[i] ?? new OpCode(0);
}*/
if (debug) {
debugLog!.write(
`${(++debugIteration).toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
const opCode = this.memory[index];
if (!opCode.executable) {
throw new Error(`Tried executing non-executable value ${this.memory[index]} at index ${index}`);
}
const parameterModes = opCode.parameterModes!;
const valueA = this.memory[index + 1]?.literalValue ?? 0;
const valueB = this.memory[index + 2]?.literalValue ?? 0;
const valueC = this.memory[index + 3]?.literalValue ?? 0;
const paramA = parameterModes[2] === ParameterMode.immediate ? valueA : this.memory[valueA]?.literalValue ?? 0;
const paramB = parameterModes[1] === ParameterMode.immediate ? valueB : this.memory[valueB]?.literalValue ?? 0;
const paramC = parameterModes[0] === ParameterMode.immediate ? valueC : this.memory[valueC]?.literalValue ?? 0;
/*console.debug(
opCode,
this.memory.slice(index, index + 4).map((val) => val.literalValue)
);*/
switch (opCode.instruction) {
case Instruction.add:
if (debug) {
console.debug(`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA + paramB);
index += 4;
break;
case Instruction.mul:
if (debug) {
console.debug(`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA * paramB);
index += 4;
break;
case Instruction.inp:
this.memory[valueA] = await new Promise<OpCode>((resolve, reject) => {
rl.question(`[${index.toString().padStart(5, "0")}]> `, (answer) => {
try {
const op = new OpCode(answer);
if (debug) {
console.debug(`Writing opCode with value ${op.literalValue} to ${valueA}`);
}
resolve(op);
} catch (e) {
reject(e);
}
});
});
index += 2;
break;
case Instruction.out:
if (debug) {
console.debug(`Outputting ${paramA}`);
}
output.push(paramA);
index += 2;
break;
case Instruction.jnz:
if (debug) {
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB}`);
}
if (paramA !== 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.jz:
if (debug) {
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB}`);
}
if (paramA === 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.lt:
if (debug) {
console.debug(`Testing if ${paramA}<${paramB}, writing result to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
index += 4;
break;
case Instruction.eq:
if (debug) {
console.debug(`Testing if ${paramA}==${paramB}, writing result to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
index += 4;
break;
case Instruction.end:
console.log("Final output:", output);
return this.memory;
}
}
} catch (e) {
console.debug(`Execution failed at index ${index}. Dumping memory.`);
if (debug) {
debugLog!.write(
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
throw e;
}
console.log("Final output:", output);
return this.memory;
}
reset() {
this.memory = this.initialMemory;
}
static memdump(memory: Memory) {
return memory.map((op) => op.toString()).join(",");
}
}
async function main(code: string) {
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
const machine = new Machine(memdump);
const result = await machine.run();
rl.close();
return; // result ? Machine.memdump(result) : result;
}
const input = fs.readFileSync("input").toString();
main(input)
.then(console.log)
.catch((...args) => {
rl.close();
console.error(args);
});

View file

@ -1,72 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.8":
version "14.14.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.8.tgz#2127bd81949a95c8b7d3240f3254352d72563aec"
integrity sha512-z/5Yd59dCKI5kbxauAJgw6dLPzW+TNOItNE00PkpzNwUIEwdj/Lsqwq94H5DdYBX7C13aRA0CY32BK76+neEUA==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
readline@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c"
integrity sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw=
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

File diff suppressed because it is too large Load diff

View file

@ -1,38 +0,0 @@
import * as fs from "fs";
const input = fs.readFileSync("input").toString();
async function main() {
let orbits = new Map<string, string>();
input.split("\r\n").forEach((line) => orbits.set(line.split(")")[1], line.split(")")[0]));
let distanceToYou = new Map<string, number>();
let orbit = orbits.get("YOU")!;
let distance = 0;
while (true) {
distanceToYou.set(orbit, distance);
const newOrbit = orbits.get(orbit);
if (newOrbit !== undefined) {
orbit = newOrbit;
distance++;
} else {
break;
}
}
orbit = orbits.get("SAN")!;
distance = 0;
while (true) {
let pathValue = distanceToYou.get(orbit);
if (pathValue !== undefined) {
distance += pathValue;
break;
}
orbit = orbits.get(orbit)!;
distance++;
}
return distance;
}
main().then(console.log).catch(console.error);

View file

@ -1,6 +0,0 @@
import * as fs from "fs";
const input = fs.readFileSync("input");
async function main() {}
main().then(console.log).catch(console.error);

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": false, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1 +0,0 @@
3,8,1001,8,10,8,105,1,0,0,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99

View file

@ -1,8 +0,0 @@
00001;00000;[3,8,1001,8,10,8,105,1,19,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00002;00002;[3,8,1001,8,10,8,105,1,9,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00003;00006;[3,8,1001,8,10,8,105,1,19,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00004;00422;[3,8,1001,8,10,8,105,1,19,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00005;00424;[3,8,1001,8,10,8,105,1,19,0,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00006;00428;[3,8,1001,8,10,8,105,1,19,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00007;00430;[3,8,1001,8,10,8,105,1,19,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]
00007;00430;[3,8,1001,8,10,8,105,1,19,1,21,34,51,64,73,98,179,260,341,422,99999,3,9,102,4,9,9,1001,9,4,9,4,9,99,3,9,1001,9,4,9,1002,9,3,9,1001,9,5,9,4,9,99,3,9,101,5,9,9,102,5,9,9,4,9,99,3,9,101,5,9,9,4,9,99,3,9,1002,9,5,9,1001,9,3,9,102,2,9,9,101,5,9,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,99]

View file

@ -1,17 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write --print-width=120",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.8",
"readline": "^1.3.0",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,287 +0,0 @@
import * as fs from "fs";
const fsPromises = fs.promises;
import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
enum ParameterMode {
position = 0,
immediate = 1,
}
enum Instruction {
add = 1,
mul = 2,
inp = 3,
out = 4,
jnz = 5,
jz = 6,
lt = 7,
eq = 8,
end = 99,
}
type Memory = Array<OpCode>;
class OpCode {
private _executable: boolean = false;
private _literalValue: number = 0;
private _instruction?: Instruction;
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
constructor(value: number);
constructor(value: string);
constructor(value: string | number) {
let valueNumber: number;
let valueString: string;
if (typeof value === "number") {
valueNumber = value;
valueString = value.toString();
} else {
valueString = value;
valueNumber = parseInt(value);
}
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
valueString = valueString.padStart(5, "0");
this._literalValue = valueNumber;
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
Math.floor(valueNumber / 10000) % 10,
Math.floor(valueNumber / 1000) % 10,
Math.floor(valueNumber / 100) % 10,
];
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
this._parameterModes = parameterModes;
this._instruction = valueNumber % 100;
} else {
return;
}
this._executable = true;
}
}
get executable() {
if (this._instruction === undefined || this._parameterModes === undefined) {
this._executable = false;
}
return this._executable;
}
get literalValue() {
return this._literalValue;
}
get instruction() {
return this._instruction;
}
get parameterModes() {
return this._parameterModes;
}
toString() {
return this.literalValue.toString();
}
}
class Machine {
private readonly initialMemory: Memory;
private memory: Memory;
constructor(program: Memory) {
this.initialMemory = program;
this.memory = program;
}
async run(simulatedInput: string[], debug: boolean = false) {
let index = 0;
let debugIteration = 0;
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
let output: number[] = [];
try {
while (index < this.memory.length) {
/*for (let i = 0; i < this.memory.length; i++) {
this.memory[i] = this.memory[i] ?? new OpCode(0);
}*/
if (debug) {
debugLog!.write(
`${(++debugIteration).toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
const opCode = this.memory[index];
if (!opCode.executable) {
throw new Error(`Tried executing non-executable value ${this.memory[index]} at index ${index}`);
}
const parameterModes = opCode.parameterModes!;
const valueA = this.memory[index + 1]?.literalValue ?? 0;
const valueB = this.memory[index + 2]?.literalValue ?? 0;
const valueC = this.memory[index + 3]?.literalValue ?? 0;
const paramA = parameterModes[2] === ParameterMode.immediate ? valueA : this.memory[valueA]?.literalValue ?? 0;
const paramB = parameterModes[1] === ParameterMode.immediate ? valueB : this.memory[valueB]?.literalValue ?? 0;
const paramC = parameterModes[0] === ParameterMode.immediate ? valueC : this.memory[valueC]?.literalValue ?? 0;
/*console.debug(
opCode,
this.memory.slice(index, index + 4).map((val) => val.literalValue)
);*/
switch (opCode.instruction) {
case Instruction.add:
if (debug) {
console.debug(`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA + paramB);
index += 4;
break;
case Instruction.mul:
if (debug) {
console.debug(`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA * paramB);
index += 4;
break;
case Instruction.inp:
/*this.memory[valueA] = await new Promise<OpCode>((resolve, reject) => {
rl.question(`[${index.toString().padStart(5, "0")}]> `, (answer) => {
try {
const op = new OpCode(answer);
resolve(op);
} catch (e) {
reject(e);
}
});
});*/
if (simulatedInput.length === 0) throw new Error("Asked for more input than was given!");
if (debug) console.debug(simulatedInput, simulatedInput[0]);
let inputValue = simulatedInput.shift()!;
if (debug) {
console.debug(`Writing opCode with value ${inputValue} to ${valueA}`);
}
this.memory[valueA] = new OpCode(inputValue);
index += 2;
break;
case Instruction.out:
if (debug) {
console.debug(`Outputting ${paramA}`);
}
output.push(paramA);
index += 2;
break;
case Instruction.jnz:
if (debug) {
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB}`);
}
if (paramA !== 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.jz:
if (debug) {
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB}`);
}
if (paramA === 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.lt:
if (debug) {
console.debug(`Testing if ${paramA}<${paramB}, writing result to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
index += 4;
break;
case Instruction.eq:
if (debug) {
console.debug(`Testing if ${paramA}==${paramB}, writing result to ${valueC}`);
}
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
index += 4;
break;
case Instruction.end:
return output;
default:
throw new Error(`HCF: Not implemented <${opCode.instruction}>`);
}
}
} catch (e) {
// console.debug(`Execution failed at index ${index}. Dumping memory.`);
if (debug) {
debugLog!.write(
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
throw e;
}
return output;
}
reset() {
this.memory = this.initialMemory;
}
static memdump(memory: Memory) {
return memory.map((op) => op.toString()).join(",");
}
}
function permute<T>(inputArr: T[]) {
let result: T[][] = [];
function _permute(arr: T[], m: T[] = []) {
if (arr.length === 0) {
result.push(m);
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
_permute(curr.slice(), m.concat(next));
}
}
}
_permute(inputArr);
return result;
}
async function main(code: string) {
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
const machine = new Machine(memdump);
let maxResult: number = -Infinity;
const permutations = permute([4, 3, 2, 1, 0].map((n) => n.toString()));
for (const value of permutations) {
try {
let curResult = 0;
let result: number = -Infinity;
for (const setting of value) {
result = (await machine.run([setting, curResult.toString()], maxResult === -Infinity))[0];
curResult = result;
}
maxResult = Math.max(result, maxResult);
machine.reset();
console.debug("Testing", value, "got", result);
} catch {
console.error(value);
}
}
rl.close();
return maxResult;
}
const input = fs.readFileSync("input").toString();
main(input)
.then(console.log)
.catch((...args) => {
rl.close();
console.error(args);
});

View file

@ -1,2 +0,0 @@
// DNF
// USED https://www.philipp-doblhofer.at/en/blog/advent-of-code-2019-day-7/

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": false, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,72 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.8":
version "14.14.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.8.tgz#2127bd81949a95c8b7d3240f3254352d72563aec"
integrity sha512-z/5Yd59dCKI5kbxauAJgw6dLPzW+TNOItNE00PkpzNwUIEwdj/Lsqwq94H5DdYBX7C13aRA0CY32BK76+neEUA==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
readline@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/readline/-/readline-1.3.0.tgz#c580d77ef2cfc8752b132498060dc9793a7ac01c"
integrity sha1-xYDXfvLPyHUrEySYBg3JeTp6wBw=
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

File diff suppressed because one or more lines are too long

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write --print-width=120",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.8",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,17 +0,0 @@
import * as fs from "fs";
const input = fs.readFileSync("input").toString();
async function main() {
let layers = input.replace(/(.{150})/g, "$1,").split(",");
let layers2: [number, number, number, string][] = layers.map((layer) => [
layer.match(/0/g)?.length ?? 0,
layer.match(/1/g)?.length ?? 0,
layer.match(/2/g)?.length ?? 0,
layer,
]);
let selected = layers2.sort((a, b) => a[0] - b[0])[1];
return [selected, selected[0] + selected[1] + selected[2], selected[1] * selected[2]];
}
main().then(console.log).catch(console.error);

View file

@ -1,19 +0,0 @@
import * as fs from "fs";
const input = fs.readFileSync("input").toString();
async function main() {
let image: string[] = Array(6 * 25).fill("2"); //Array(6).map(_=>Array(25))
let layers = input.replace(/(.{150})/g, "$1,").split(",");
for (const layer of layers) {
for (let i = 0; i < layer.length; i++) {
if(image[i] === "2") {
image[i] = layer[i]
}
}
}
return image.join("").replace(/(.{25})/g, "$1\n");
}
main().then(console.log).catch(console.error);

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": false, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.8":
version "14.14.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.8.tgz#2127bd81949a95c8b7d3240f3254352d72563aec"
integrity sha512-z/5Yd59dCKI5kbxauAJgw6dLPzW+TNOItNE00PkpzNwUIEwdj/Lsqwq94H5DdYBX7C13aRA0CY32BK76+neEUA==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1 +0,0 @@
1102,34463338,34463338,63,1007,63,34463338,63,1005,63,53,1102,3,1,1000,109,988,209,12,9,1000,209,6,209,3,203,0,1008,1000,1,63,1005,63,65,1008,1000,2,63,1005,63,904,1008,1000,0,63,1005,63,58,4,25,104,0,99,4,0,104,0,99,4,17,104,0,99,0,0,1101,0,31,1019,1101,25,0,1008,1102,35,1,1009,1102,422,1,1029,1102,1,21,1005,1102,1,734,1027,1102,29,1,1000,1101,32,0,1018,1102,28,1,1016,1101,0,38,1015,1101,0,378,1023,1101,30,0,1017,1102,1,381,1022,1101,0,37,1006,1102,1,1,1021,1101,0,24,1011,1102,1,23,1002,1101,0,0,1020,1101,0,20,1007,1101,427,0,1028,1101,26,0,1014,1101,27,0,1010,1101,0,39,1001,1101,34,0,1012,1102,1,36,1013,1101,0,33,1003,1101,804,0,1025,1101,737,0,1026,1102,1,809,1024,1102,1,22,1004,109,9,1201,-7,0,63,1008,63,20,63,1005,63,205,1001,64,1,64,1106,0,207,4,187,1002,64,2,64,109,2,21102,40,1,1,1008,1012,40,63,1005,63,233,4,213,1001,64,1,64,1106,0,233,1002,64,2,64,109,4,1208,-7,25,63,1005,63,255,4,239,1001,64,1,64,1106,0,255,1002,64,2,64,109,-24,1207,10,38,63,1005,63,271,1105,1,277,4,261,1001,64,1,64,1002,64,2,64,109,25,21107,41,40,-3,1005,1013,293,1105,1,299,4,283,1001,64,1,64,1002,64,2,64,109,5,1205,-1,311,1106,0,317,4,305,1001,64,1,64,1002,64,2,64,109,-23,1202,6,1,63,1008,63,22,63,1005,63,339,4,323,1105,1,343,1001,64,1,64,1002,64,2,64,109,1,2101,0,2,63,1008,63,37,63,1005,63,367,1001,64,1,64,1106,0,369,4,349,1002,64,2,64,109,29,2105,1,-5,1106,0,387,4,375,1001,64,1,64,1002,64,2,64,109,-26,2101,0,0,63,1008,63,23,63,1005,63,409,4,393,1106,0,413,1001,64,1,64,1002,64,2,64,109,26,2106,0,0,4,419,1106,0,431,1001,64,1,64,1002,64,2,64,109,-17,21108,42,42,6,1005,1017,453,4,437,1001,64,1,64,1106,0,453,1002,64,2,64,109,7,21101,43,0,-8,1008,1010,44,63,1005,63,477,1001,64,1,64,1105,1,479,4,459,1002,64,2,64,109,-7,1206,10,495,1001,64,1,64,1106,0,497,4,485,1002,64,2,64,109,-5,2108,36,0,63,1005,63,513,1106,0,519,4,503,1001,64,1,64,1002,64,2,64,109,3,2102,1,-5,63,1008,63,22,63,1005,63,541,4,525,1105,1,545,1001,64,1,64,1002,64,2,64,109,3,1207,-6,38,63,1005,63,567,4,551,1001,64,1,64,1105,1,567,1002,64,2,64,109,-15,2107,20,8,63,1005,63,585,4,573,1106,0,589,1001,64,1,64,1002,64,2,64,109,-1,1208,5,36,63,1005,63,609,1001,64,1,64,1106,0,611,4,595,1002,64,2,64,109,30,21101,44,0,-7,1008,1019,44,63,1005,63,633,4,617,1106,0,637,1001,64,1,64,1002,64,2,64,109,-25,1201,0,0,63,1008,63,39,63,1005,63,659,4,643,1105,1,663,1001,64,1,64,1002,64,2,64,109,27,1206,-8,677,4,669,1106,0,681,1001,64,1,64,1002,64,2,64,109,-28,2108,29,0,63,1005,63,703,4,687,1001,64,1,64,1106,0,703,1002,64,2,64,109,5,21107,45,46,7,1005,1012,725,4,709,1001,64,1,64,1106,0,725,1002,64,2,64,109,30,2106,0,-8,1105,1,743,4,731,1001,64,1,64,1002,64,2,64,109,-22,21102,46,1,4,1008,1017,44,63,1005,63,767,1001,64,1,64,1105,1,769,4,749,1002,64,2,64,109,-15,1202,10,1,63,1008,63,23,63,1005,63,793,1001,64,1,64,1106,0,795,4,775,1002,64,2,64,109,19,2105,1,7,4,801,1105,1,813,1001,64,1,64,1002,64,2,64,109,6,1205,-2,827,4,819,1106,0,831,1001,64,1,64,1002,64,2,64,109,-20,2107,22,2,63,1005,63,851,1001,64,1,64,1106,0,853,4,837,1002,64,2,64,109,20,21108,47,44,-8,1005,1015,869,1105,1,875,4,859,1001,64,1,64,1002,64,2,64,109,-22,2102,1,4,63,1008,63,23,63,1005,63,899,1001,64,1,64,1106,0,901,4,881,4,64,99,21101,0,27,1,21102,915,1,0,1106,0,922,21201,1,28703,1,204,1,99,109,3,1207,-2,3,63,1005,63,964,21201,-2,-1,1,21101,0,942,0,1106,0,922,22101,0,1,-1,21201,-2,-3,1,21101,957,0,0,1105,1,922,22201,1,-1,-2,1105,1,968,21201,-2,0,-2,109,-3,2105,1,0

View file

@ -1,316 +0,0 @@
import * as fs from "fs";
import { relative } from "path";
const fsPromises = fs.promises;
import * as readline from "readline";
const rl = readline.createInterface(process.stdin, process.stdout);
enum ParameterMode {
position = 0,
immediate = 1,
relative = 2,
}
enum Instruction {
add = 1,
mul = 2,
inp = 3,
out = 4,
jnz = 5,
jz = 6,
lt = 7,
eq = 8,
rel = 9,
end = 99,
}
type Memory = Array<OpCode>;
class OpCode {
private _executable: boolean = false;
private _literalValue: number = 0;
private _instruction?: Instruction;
private _parameterModes?: [a: ParameterMode, b: ParameterMode, c: ParameterMode];
constructor(value: number);
constructor(value: string);
constructor(value: string | number) {
let valueNumber: number;
let valueString: string;
if (typeof value === "number") {
valueNumber = value;
valueString = value.toString();
} else {
valueString = value;
valueNumber = parseInt(value);
}
if (isNaN(valueNumber)) throw new Error(`Invalid value '${value}' for OpCode.`);
valueString = valueString.padStart(5, "0");
this._literalValue = valueNumber;
if (valueNumber >= 0 && valueNumber <= 99999 && valueNumber % 100 in Instruction) {
const parameterModes: [ParameterMode, ParameterMode, ParameterMode] = [
Math.floor(valueNumber / 10000) % 10,
Math.floor(valueNumber / 1000) % 10,
Math.floor(valueNumber / 100) % 10,
];
if (parameterModes.filter((val) => val in ParameterMode).length === 3) {
this._parameterModes = parameterModes;
this._instruction = valueNumber % 100;
} else {
return;
}
this._executable = true;
}
}
get executable() {
if (this._instruction === undefined || this._parameterModes === undefined) {
this._executable = false;
}
return this._executable;
}
get literalValue() {
return this._literalValue;
}
get instruction() {
return this._instruction;
}
get parameterModes() {
return this._parameterModes;
}
toString() {
return this.literalValue.toString();
}
}
class Machine {
private readonly initialMemory: Memory;
private memory: Memory;
constructor(program: Memory) {
this.initialMemory = program;
this.memory = program;
}
parseParam(value: number, mode: ParameterMode, relativeOffset: number) {
switch (mode) {
case ParameterMode.immediate:
return value;
case ParameterMode.position:
return this.memory[value]?.literalValue ?? 0;
case ParameterMode.relative:
return this.memory[value + relativeOffset]?.literalValue ?? 0;
}
}
async run(debug: boolean = false) {
let index = 0;
let relative = 0;
let debugIteration = 0;
const debugLog = debug ? await fsPromises.open("out.log", "w") : undefined;
let output: number[] = [];
try {
while (index < this.memory.length) {
/*for (let i = 0; i < this.memory.length; i++) {
this.memory[i] = this.memory[i] ?? new OpCode(0);
}*/
if (debug) {
debugLog!.write(
`${(++debugIteration).toString().padStart(5, "0")};${relative
.toString()
.padStart(8, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
const opCode = this.memory[index];
if (!opCode.executable) {
throw new Error(`Tried executing non-executable value ${opCode.literalValue} at index ${index}`);
}
const parameterModes = opCode.parameterModes!;
const valueA = this.memory[index + 1]?.literalValue ?? 0;
const valueB = this.memory[index + 2]?.literalValue ?? 0;
const valueC = this.memory[index + 3]?.literalValue ?? 0;
const paramA = this.parseParam(valueA, parameterModes[2 - 0], relative);
const paramB = this.parseParam(valueB, parameterModes[2 - 1], relative);
const paramC = this.parseParam(valueC, parameterModes[2 - 2], relative);
if (debug) {
console.debug(`${opCode.literalValue}(${valueA}, ${valueB}, ${valueC});${this.memory[1028]?.literalValue}`);
}
switch (opCode.instruction) {
case Instruction.add:
if (debug) {
console.debug(
`Adding ${paramA}+${paramB} and writing ${paramA + paramB} to ${valueC} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA + paramB);
} else {
this.memory[valueC] = new OpCode(paramA + paramB);
}
index += 4;
break;
case Instruction.mul:
if (debug) {
console.debug(
`Multiplying ${paramA}*${paramB} and writing ${paramA * paramB} to ${valueC} - OpCode ${
opCode.literalValue
}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA * paramB);
} else {
this.memory[valueC] = new OpCode(paramA * paramB);
}
index += 4;
break;
case Instruction.inp:
let input = await new Promise<OpCode>((resolve, reject) => {
rl.question(`[${index.toString().padStart(5, "0")}]> `, (answer) => {
try {
const op = new OpCode(answer);
if (debug) {
console.debug(
`Writing opCode with value ${op.literalValue} to ${
parameterModes[2 - 0] === ParameterMode.relative ? valueA + relative : valueA
} - OpCode ${opCode.literalValue}`
);
}
resolve(op);
} catch (e) {
reject(e);
}
});
});
if (parameterModes[2 - 0] === ParameterMode.relative) {
this.memory[valueA + relative] = input;
} else {
this.memory[valueA] = input;
}
index += 2;
break;
case Instruction.out:
if (debug) {
console.debug(`Outputting ${paramA} - OpCode ${opCode.literalValue}`);
}
output.push(paramA);
index += 2;
break;
case Instruction.jnz:
if (debug) {
console.debug(`Testing if ${paramA}!=0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
}
if (paramA !== 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.jz:
if (debug) {
console.debug(`Testing if ${paramA}==0, if so, jumping to ${paramB} - OpCode ${opCode.literalValue}`);
}
if (paramA === 0) {
index = paramB;
} else {
index += 3;
}
break;
case Instruction.lt:
if (debug) {
console.debug(
`Checking if ${paramA}<${paramB} and writing result to ${
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA < paramB ? 1 : 0);
} else {
this.memory[valueC] = new OpCode(paramA < paramB ? 1 : 0);
}
index += 4;
break;
case Instruction.eq:
if (debug) {
console.debug(
`Checking if ${paramA}==${paramB} and writing result to ${
parameterModes[2 - 2] === ParameterMode.relative ? valueC + relative : valueC
} - OpCode ${opCode.literalValue}`
);
}
if (parameterModes[2 - 2] === ParameterMode.relative) {
this.memory[valueC + relative] = new OpCode(paramA === paramB ? 1 : 0);
} else {
this.memory[valueC] = new OpCode(paramA === paramB ? 1 : 0);
}
index += 4;
break;
case Instruction.rel:
if (debug) {
console.debug(
`Adjusting relative offset from ${relative} +${paramA} to ${relative + paramA} - OpCode ${
opCode.literalValue
}`
);
}
relative += paramA;
index += 2;
break;
case Instruction.end:
console.log("Final output:", output);
return this.memory;
}
}
} catch (e) {
console.debug(`Execution failed at index ${index}. Dumping memory.`);
if (debug) {
debugLog!.write(
`${debugIteration.toString().padStart(5, "0")};${index.toString().padStart(5, "0")};[${this.memory
.map((op) => op.literalValue)
.join(",")}]\n`
);
}
throw e;
}
console.log("Final output:", output);
return this.memory;
}
reset() {
this.memory = this.initialMemory;
}
static memdump(memory: Memory) {
return memory.map((op) => op.toString()).join(",");
}
}
async function main(code: string) {
const memdump: Memory = code.split(",").map((val) => new OpCode(val));
const machine = new Machine(memdump);
const result = await machine.run();
rl.close();
return; // result ? Machine.memdump(result) : result;
}
const input = fs.readFileSync("input").toString();
main(input)
.then(console.log)
.catch((...args) => {
rl.close();
console.error(args);
});

View file

@ -1 +0,0 @@
// Same as solution_a but with input 2 instead of 1

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": false, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.8":
version "14.14.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.8.tgz#2127bd81949a95c8b7d3240f3254352d72563aec"
integrity sha512-z/5Yd59dCKI5kbxauAJgw6dLPzW+TNOItNE00PkpzNwUIEwdj/Lsqwq94H5DdYBX7C13aRA0CY32BK76+neEUA==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

File diff suppressed because one or more lines are too long

View file

@ -1,5 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
yarn-path ".yarn/releases/yarn-1.22.10.cjs"

View file

@ -1,16 +0,0 @@
{
"scripts": {
"lint": "prettier solution_a.ts solution_b.ts --write --print-width=120",
"a": "ts-node solution_a.ts",
"b": "ts-node solution_b.ts"
},
"devDependencies": {
"prettier": "^2.1.2"
},
"dependencies": {
"@types/node": "^14.14.8",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
},
"license": "WTFPL"
}

View file

@ -1,69 +0,0 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "ES2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "CommonJS", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
"sourceMap": false, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
"noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

View file

@ -1,67 +0,0 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@types/node@^14.14.8":
version "14.14.8"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.8.tgz#2127bd81949a95c8b7d3240f3254352d72563aec"
integrity sha512-z/5Yd59dCKI5kbxauAJgw6dLPzW+TNOItNE00PkpzNwUIEwdj/Lsqwq94H5DdYBX7C13aRA0CY32BK76+neEUA==
arg@^4.1.0:
version "4.1.3"
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089"
integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==
buffer-from@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
diff@^4.0.1:
version "4.0.2"
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==
make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
prettier@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==
source-map-support@^0.5.17:
version "0.5.19"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.19.tgz#a98b62f86dcaf4f67399648c085291ab9e8fed61"
integrity sha512-Wonm7zOCIJzBGQdB+thsPar0kYuCIzYvxZwlBa87yi/Mdjv7Tip2cyVbLj5o0cFPN4EVkuTwb3GDDyUx2DGnGw==
dependencies:
buffer-from "^1.0.0"
source-map "^0.6.0"
source-map@^0.6.0:
version "0.6.1"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
ts-node@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-9.0.0.tgz#e7699d2a110cc8c0d3b831715e417688683460b3"
integrity sha512-/TqB4SnererCDR/vb4S/QvSZvzQMJN8daAslg7MeaiHvD8rDZsSfXmNeNumyZZzMned72Xoq/isQljYSt8Ynfg==
dependencies:
arg "^4.1.0"
diff "^4.0.1"
make-error "^1.1.1"
source-map-support "^0.5.17"
yn "3.1.1"
typescript@^4.0.5:
version "4.0.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.5.tgz#ae9dddfd1069f1cb5beb3ef3b2170dd7c1332389"
integrity sha512-ywmr/VrTVCmNTJ6iV2LwIrfG1P+lv6luD8sUJs+2eI9NLGigaN+nUQc13iHqisq7bra9lnmUSYqbJvegraBOPQ==
yn@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

1
Python/day1/input Normal file

File diff suppressed because one or more lines are too long

132
Python/day1/solution.ipynb Normal file
View file

@ -0,0 +1,132 @@
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.6 64-bit",
"metadata": {
"interpreter": {
"hash": "1ddf53fa84779e14f0db18342168679c9c165a81c5f324dac828befab228d68d"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read()"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"ups = puzzle_input.count(\"(\")\n",
"downs = puzzle_input.count(\")\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"end_floor = ups - downs"
]
},
{
"source": [
"print(end_floor)"
],
"cell_type": "code",
"metadata": {},
"execution_count": 13,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"138\n"
]
}
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"current_floor = 0"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"index = 0\n",
"while current_floor >= 0 and index < len(puzzle_input):\n",
" if puzzle_input[index] == \"(\":\n",
" current_floor += 1\n",
" elif puzzle_input[index] == \")\":\n",
" current_floor -= 1\n",
" index += 1"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1771\n"
]
}
],
"source": [
"print(index)"
]
}
]
}

1000
Python/day2/input Normal file

File diff suppressed because it is too large Load diff

104
Python/day2/solution.ipynb Normal file
View file

@ -0,0 +1,104 @@
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.6 64-bit",
"metadata": {
"interpreter": {
"hash": "1ddf53fa84779e14f0db18342168679c9c165a81c5f324dac828befab228d68d"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read().split(\"\\n\")"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1606483\n"
]
}
],
"source": [
"wrapping_paper = 0\n",
"for packet in puzzle_input:\n",
" l, w, h = map(lambda val: int(val), packet.split(\"x\"))\n",
" # the surface area of the box, which is 2*l*w + 2*w*h + 2*h*l\n",
" sides = [l*w*2, w*h*2, l*h*2]\n",
" # extra paper for each present: the area of the smallest side.\n",
" wrapping_paper += sum(sides) + int(min(sides)/2)\n",
"print(wrapping_paper)"
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"3842356\n"
]
}
],
"source": [
"ribbon = 0\n",
"for packet in puzzle_input:\n",
" l, w, h = map(lambda val: int(val), packet.split(\"x\"))\n",
" # the feet of ribbon required for the perfect bow is equal to the cubic feet of volume of the present\n",
" ribbon += l*w*h\n",
" # The ribbon required to wrap a present is the shortest distance around its sides, or the smallest perimeter of any one face\n",
" x, y = sorted([l, w, h])[0:2]\n",
" ribbon += 2*x + 2*y\n",
"print(ribbon)"
]
}
]
}

1
Python/day3/input Normal file

File diff suppressed because one or more lines are too long

123
Python/day3/solution.ipynb Normal file
View file

@ -0,0 +1,123 @@
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.6 64-bit",
"metadata": {
"interpreter": {
"hash": "1ddf53fa84779e14f0db18342168679c9c165a81c5f324dac828befab228d68d"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read()"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2572\n"
]
}
],
"source": [
"delivered = {(0,0)}\n",
"\n",
"x, y = 0, 0\n",
"ops = {\n",
" \"^\": [+0, +1],\n",
" \"v\": [-0, -1],\n",
" \"<\": [-1, -0],\n",
" \">\": [+1, +0]\n",
"}\n",
"for char in puzzle_input:\n",
" dx, dy = ops[char]\n",
" x += dx\n",
" y += dy\n",
" delivered.add((x, y))\n",
"print(len(delivered))"
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2631\n"
]
}
],
"source": [
"delivered = {(0,0)}\n",
"\n",
"santa = (0, 0)\n",
"robo_santa = (0, 0)\n",
"ops = {\n",
" \"^\": [+0, +1],\n",
" \"v\": [-0, -1],\n",
" \"<\": [-1, -0],\n",
" \">\": [+1, +0]\n",
"}\n",
"robot = False\n",
"for char in puzzle_input:\n",
" dx, dy = ops[char]\n",
" if robot:\n",
" robo_santa = (robo_santa[0]+dx, robo_santa[1]+dy)\n",
" delivered.add(robo_santa)\n",
" else:\n",
" santa = (santa[0]+dx, santa[1]+dy)\n",
" delivered.add(santa)\n",
" robot = not robot\n",
"print(len(delivered))"
]
}
]
}

1
Python/day4/input Normal file
View file

@ -0,0 +1 @@
yzbqklnj

112
Python/day4/solution.ipynb Normal file
View file

@ -0,0 +1,112 @@
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.6 64-bit",
"metadata": {
"interpreter": {
"hash": "1ddf53fa84779e14f0db18342168679c9c165a81c5f324dac828befab228d68d"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import hashlib"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read()"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"tags": []
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"282749\n"
]
}
],
"source": [
"current_hash = \"\"\n",
"i = 0\n",
"while not current_hash[0:5] == \"00000\":\n",
" i += 1\n",
" md5 = hashlib.md5(f\"{puzzle_input}{i}\".encode(\"utf-8\"))\n",
" current_hash = md5.hexdigest()\n",
"print(i)"
]
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"9962624\n"
]
}
],
"source": [
"current_hash = \"\"\n",
"i = 0\n",
"while not current_hash[0:6] == \"000000\":\n",
" i += 1\n",
" md5 = hashlib.md5(f\"{puzzle_input}{i}\".encode(\"utf-8\"))\n",
" current_hash = md5.hexdigest()\n",
"print(i)"
]
}
]
}

1000
Python/day5/input Normal file

File diff suppressed because it is too large Load diff

119
Python/day5/solution.ipynb Normal file
View file

@ -0,0 +1,119 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read().split(\"\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 1 #"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"238\n"
]
}
],
"source": [
"double_letter_test = re.compile(r\"(.)\\1\", re.IGNORECASE)\n",
"unnice_test = re.compile(r\"ab|cd|pq|xy\", re.IGNORECASE)\n",
"\n",
"def filter_function_part_one(line: str) -> bool:\n",
" return (sum(map(line.lower().count, \"aeiou\")) >= 3) and bool(not unnice_test.search(line)) and bool(double_letter_test.search(line))\n",
"\n",
"filtered_lines = list(filter(\n",
" filter_function_part_one,\n",
" puzzle_input\n",
"))\n",
"print(len(filtered_lines))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 2 #"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"69\n"
]
}
],
"source": [
"def filter_function_part_two(line: str) -> bool:\n",
" return re.findall(r\"(..).*\\1\", line) and re.findall(r\"(.).\\1\", line)\n",
"\n",
"filtered_lines = filter(\n",
" filter_function_part_two,\n",
" puzzle_input\n",
")\n",
"print(len(list(filtered_lines)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

300
Python/day6/input Normal file
View file

@ -0,0 +1,300 @@
turn off 660,55 through 986,197
turn off 341,304 through 638,850
turn off 199,133 through 461,193
toggle 322,558 through 977,958
toggle 537,781 through 687,941
turn on 226,196 through 599,390
turn on 240,129 through 703,297
turn on 317,329 through 451,798
turn on 957,736 through 977,890
turn on 263,530 through 559,664
turn on 158,270 through 243,802
toggle 223,39 through 454,511
toggle 544,218 through 979,872
turn on 313,306 through 363,621
toggle 173,401 through 496,407
toggle 333,60 through 748,159
turn off 87,577 through 484,608
turn on 809,648 through 826,999
toggle 352,432 through 628,550
turn off 197,408 through 579,569
turn off 1,629 through 802,633
turn off 61,44 through 567,111
toggle 880,25 through 903,973
turn on 347,123 through 864,746
toggle 728,877 through 996,975
turn on 121,895 through 349,906
turn on 888,547 through 931,628
toggle 398,782 through 834,882
turn on 966,850 through 989,953
turn off 891,543 through 914,991
toggle 908,77 through 916,117
turn on 576,900 through 943,934
turn off 580,170 through 963,206
turn on 184,638 through 192,944
toggle 940,147 through 978,730
turn off 854,56 through 965,591
toggle 717,172 through 947,995
toggle 426,987 through 705,998
turn on 987,157 through 992,278
toggle 995,774 through 997,784
turn off 796,96 through 845,182
turn off 451,87 through 711,655
turn off 380,93 through 968,676
turn on 263,468 through 343,534
turn on 917,936 through 928,959
toggle 478,7 through 573,148
turn off 428,339 through 603,624
turn off 400,880 through 914,953
toggle 679,428 through 752,779
turn off 697,981 through 709,986
toggle 482,566 through 505,725
turn off 956,368 through 993,516
toggle 735,823 through 783,883
turn off 48,487 through 892,496
turn off 116,680 through 564,819
turn on 633,865 through 729,930
turn off 314,618 through 571,922
toggle 138,166 through 936,266
turn on 444,732 through 664,960
turn off 109,337 through 972,497
turn off 51,432 through 77,996
turn off 259,297 through 366,744
toggle 801,130 through 917,544
toggle 767,982 through 847,996
turn on 216,507 through 863,885
turn off 61,441 through 465,731
turn on 849,970 through 944,987
toggle 845,76 through 852,951
toggle 732,615 through 851,936
toggle 251,128 through 454,778
turn on 324,429 through 352,539
toggle 52,450 through 932,863
turn off 449,379 through 789,490
turn on 317,319 through 936,449
toggle 887,670 through 957,838
toggle 671,613 through 856,664
turn off 186,648 through 985,991
turn off 471,689 through 731,717
toggle 91,331 through 750,758
toggle 201,73 through 956,524
toggle 82,614 through 520,686
toggle 84,287 through 467,734
turn off 132,367 through 208,838
toggle 558,684 through 663,920
turn on 237,952 through 265,997
turn on 694,713 through 714,754
turn on 632,523 through 862,827
turn on 918,780 through 948,916
turn on 349,586 through 663,976
toggle 231,29 through 257,589
toggle 886,428 through 902,993
turn on 106,353 through 236,374
turn on 734,577 through 759,684
turn off 347,843 through 696,912
turn on 286,699 through 964,883
turn on 605,875 through 960,987
turn off 328,286 through 869,461
turn off 472,569 through 980,848
toggle 673,573 through 702,884
turn off 398,284 through 738,332
turn on 158,50 through 284,411
turn off 390,284 through 585,663
turn on 156,579 through 646,581
turn on 875,493 through 989,980
toggle 486,391 through 924,539
turn on 236,722 through 272,964
toggle 228,282 through 470,581
toggle 584,389 through 750,761
turn off 899,516 through 900,925
turn on 105,229 through 822,846
turn off 253,77 through 371,877
turn on 826,987 through 906,992
turn off 13,152 through 615,931
turn on 835,320 through 942,399
turn on 463,504 through 536,720
toggle 746,942 through 786,998
turn off 867,333 through 965,403
turn on 591,477 through 743,692
turn off 403,437 through 508,908
turn on 26,723 through 368,814
turn on 409,485 through 799,809
turn on 115,630 through 704,705
turn off 228,183 through 317,220
toggle 300,649 through 382,842
turn off 495,365 through 745,562
turn on 698,346 through 744,873
turn on 822,932 through 951,934
toggle 805,30 through 925,421
toggle 441,152 through 653,274
toggle 160,81 through 257,587
turn off 350,781 through 532,917
toggle 40,583 through 348,636
turn on 280,306 through 483,395
toggle 392,936 through 880,955
toggle 496,591 through 851,934
turn off 780,887 through 946,994
turn off 205,735 through 281,863
toggle 100,876 through 937,915
turn on 392,393 through 702,878
turn on 956,374 through 976,636
toggle 478,262 through 894,775
turn off 279,65 through 451,677
turn on 397,541 through 809,847
turn on 444,291 through 451,586
toggle 721,408 through 861,598
turn on 275,365 through 609,382
turn on 736,24 through 839,72
turn off 86,492 through 582,712
turn on 676,676 through 709,703
turn off 105,710 through 374,817
toggle 328,748 through 845,757
toggle 335,79 through 394,326
toggle 193,157 through 633,885
turn on 227,48 through 769,743
toggle 148,333 through 614,568
toggle 22,30 through 436,263
toggle 547,447 through 688,969
toggle 576,621 through 987,740
turn on 711,334 through 799,515
turn on 541,448 through 654,951
toggle 792,199 through 798,990
turn on 89,956 through 609,960
toggle 724,433 through 929,630
toggle 144,895 through 201,916
toggle 226,730 through 632,871
turn off 760,819 through 828,974
toggle 887,180 through 940,310
toggle 222,327 through 805,590
turn off 630,824 through 885,963
turn on 940,740 through 954,946
turn on 193,373 through 779,515
toggle 304,955 through 469,975
turn off 405,480 through 546,960
turn on 662,123 through 690,669
turn off 615,238 through 750,714
turn on 423,220 through 930,353
turn on 329,769 through 358,970
toggle 590,151 through 704,722
turn off 884,539 through 894,671
toggle 449,241 through 984,549
toggle 449,260 through 496,464
turn off 306,448 through 602,924
turn on 286,805 through 555,901
toggle 722,177 through 922,298
toggle 491,554 through 723,753
turn on 80,849 through 174,996
turn off 296,561 through 530,856
toggle 653,10 through 972,284
toggle 529,236 through 672,614
toggle 791,598 through 989,695
turn on 19,45 through 575,757
toggle 111,55 through 880,871
turn off 197,897 through 943,982
turn on 912,336 through 977,605
toggle 101,221 through 537,450
turn on 101,104 through 969,447
toggle 71,527 through 587,717
toggle 336,445 through 593,889
toggle 214,179 through 575,699
turn on 86,313 through 96,674
toggle 566,427 through 906,888
turn off 641,597 through 850,845
turn on 606,524 through 883,704
turn on 835,775 through 867,887
toggle 547,301 through 897,515
toggle 289,930 through 413,979
turn on 361,122 through 457,226
turn on 162,187 through 374,746
turn on 348,461 through 454,675
turn off 966,532 through 985,537
turn on 172,354 through 630,606
turn off 501,880 through 680,993
turn off 8,70 through 566,592
toggle 433,73 through 690,651
toggle 840,798 through 902,971
toggle 822,204 through 893,760
turn off 453,496 through 649,795
turn off 969,549 through 990,942
turn off 789,28 through 930,267
toggle 880,98 through 932,434
toggle 568,674 through 669,753
turn on 686,228 through 903,271
turn on 263,995 through 478,999
toggle 534,675 through 687,955
turn off 342,434 through 592,986
toggle 404,768 through 677,867
toggle 126,723 through 978,987
toggle 749,675 through 978,959
turn off 445,330 through 446,885
turn off 463,205 through 924,815
turn off 417,430 through 915,472
turn on 544,990 through 912,999
turn off 201,255 through 834,789
turn off 261,142 through 537,862
turn off 562,934 through 832,984
turn off 459,978 through 691,980
turn off 73,911 through 971,972
turn on 560,448 through 723,810
turn on 204,630 through 217,854
turn off 91,259 through 611,607
turn on 877,32 through 978,815
turn off 950,438 through 974,746
toggle 426,30 through 609,917
toggle 696,37 through 859,201
toggle 242,417 through 682,572
turn off 388,401 through 979,528
turn off 79,345 through 848,685
turn off 98,91 through 800,434
toggle 650,700 through 972,843
turn off 530,450 through 538,926
turn on 428,559 through 962,909
turn on 78,138 through 92,940
toggle 194,117 through 867,157
toggle 785,355 through 860,617
turn off 379,441 through 935,708
turn off 605,133 through 644,911
toggle 10,963 through 484,975
turn off 359,988 through 525,991
turn off 509,138 through 787,411
toggle 556,467 through 562,773
turn on 119,486 through 246,900
turn on 445,561 through 794,673
turn off 598,681 through 978,921
turn off 974,230 through 995,641
turn off 760,75 through 800,275
toggle 441,215 through 528,680
turn off 701,636 through 928,877
turn on 165,753 through 202,780
toggle 501,412 through 998,516
toggle 161,105 through 657,395
turn on 113,340 through 472,972
toggle 384,994 through 663,999
turn on 969,994 through 983,997
turn on 519,600 through 750,615
turn off 363,899 through 948,935
turn on 271,845 through 454,882
turn off 376,528 through 779,640
toggle 767,98 through 854,853
toggle 107,322 through 378,688
turn off 235,899 through 818,932
turn on 445,611 through 532,705
toggle 629,387 through 814,577
toggle 112,414 through 387,421
toggle 319,184 through 382,203
turn on 627,796 through 973,940
toggle 602,45 through 763,151
turn off 441,375 through 974,545
toggle 871,952 through 989,998
turn on 717,272 through 850,817
toggle 475,711 through 921,882
toggle 66,191 through 757,481
turn off 50,197 through 733,656
toggle 83,575 through 915,728
turn on 777,812 through 837,912
turn on 20,984 through 571,994
turn off 446,432 through 458,648
turn on 715,871 through 722,890
toggle 424,675 through 740,862
toggle 580,592 through 671,900
toggle 296,687 through 906,775

152
Python/day6/solution.ipynb Normal file
View file

@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def toggle(val: bool) -> bool:\n",
" return not val\n",
"\n",
"def turn_off(val: bool) -> bool:\n",
" return False\n",
"\n",
"def turn_on(val: bool) -> bool:\n",
" return True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 1 #"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"400410\n"
]
}
],
"source": [
"lights = [[False]*1000 for i in range(1000)]\n",
"for command in puzzle_input.split(\"\\n\"):\n",
" if re.search(\"toggle\", command):\n",
" method = toggle\n",
" elif re.search(\"on\", command):\n",
" method = turn_on\n",
" else:\n",
" method = turn_off\n",
" \n",
" points = re.search(r\"(\\d+,\\d+) through (\\d+,\\d+)\", command)\n",
" point_a = tuple(map(int, points.group(1).split(\",\")))\n",
" point_b = tuple(map(int, points.group(2).split(\",\")))\n",
" \n",
" for a in range(point_a[0], point_b[0]+1):\n",
" for b in range(point_a[1], point_b[1]+1):\n",
" lights[a][b] = method(lights[a][b])\n",
"\n",
"print(list(item for sublist in lights for item in sublist).count(True))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 2 #"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15343601\n"
]
}
],
"source": [
"def toggle(val: bool) -> bool:\n",
" return val+2\n",
"\n",
"def turn_off(val: bool) -> bool:\n",
" return max(val-1, 0)\n",
"\n",
"def turn_on(val: bool) -> bool:\n",
" return val+1\n",
"\n",
"lights = [[0]*1000 for i in range(1000)]\n",
"for command in puzzle_input.split(\"\\n\"):\n",
" if re.search(\"toggle\", command):\n",
" method = toggle\n",
" elif re.search(\"on\", command):\n",
" method = turn_on\n",
" else:\n",
" method = turn_off\n",
" \n",
" points = re.search(r\"(\\d+,\\d+) through (\\d+,\\d+)\", command)\n",
" point_a = tuple(map(int, points.group(1).split(\",\")))\n",
" point_b = tuple(map(int, points.group(2).split(\",\")))\n",
" \n",
" for a in range(point_a[0], point_b[0]+1):\n",
" for b in range(point_a[1], point_b[1]+1):\n",
" lights[a][b] = method(lights[a][b])\n",
"\n",
"print(sum(list(item for sublist in lights for item in sublist)))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View file

@ -0,0 +1,55 @@
{
"metadata": {
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6-final"
},
"orig_nbformat": 2,
"kernelspec": {
"name": "python3",
"display_name": "Python 3.8.6 64-bit",
"metadata": {
"interpreter": {
"hash": "1ddf53fa84779e14f0db18342168679c9c165a81c5f324dac828befab228d68d"
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2,
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Get puzzle input from input file\n",
"\n",
"puzzle_input = open(\"input\").read()"
]
},
{
"source": [
"# Part 1 #"
],
"cell_type": "markdown",
"metadata": {}
},
{
"source": [
"# Part 2 #"
],
"cell_type": "markdown",
"metadata": {}
}
]
}

339
TypeScript/day7/input Normal file
View file

@ -0,0 +1,339 @@
bn RSHIFT 2 -> bo
lf RSHIFT 1 -> ly
fo RSHIFT 3 -> fq
cj OR cp -> cq
fo OR fz -> ga
t OR s -> u
lx -> a
NOT ax -> ay
he RSHIFT 2 -> hf
lf OR lq -> lr
lr AND lt -> lu
dy OR ej -> ek
1 AND cx -> cy
hb LSHIFT 1 -> hv
1 AND bh -> bi
ih AND ij -> ik
c LSHIFT 1 -> t
ea AND eb -> ed
km OR kn -> ko
NOT bw -> bx
ci OR ct -> cu
NOT p -> q
lw OR lv -> lx
NOT lo -> lp
fp OR fv -> fw
o AND q -> r
dh AND dj -> dk
ap LSHIFT 1 -> bj
bk LSHIFT 1 -> ce
NOT ii -> ij
gh OR gi -> gj
kk RSHIFT 1 -> ld
lc LSHIFT 1 -> lw
lb OR la -> lc
1 AND am -> an
gn AND gp -> gq
lf RSHIFT 3 -> lh
e OR f -> g
lg AND lm -> lo
ci RSHIFT 1 -> db
cf LSHIFT 1 -> cz
bn RSHIFT 1 -> cg
et AND fe -> fg
is OR it -> iu
kw AND ky -> kz
ck AND cl -> cn
bj OR bi -> bk
gj RSHIFT 1 -> hc
iu AND jf -> jh
NOT bs -> bt
kk OR kv -> kw
ks AND ku -> kv
hz OR ik -> il
b RSHIFT 1 -> v
iu RSHIFT 1 -> jn
fo RSHIFT 5 -> fr
be AND bg -> bh
ga AND gc -> gd
hf OR hl -> hm
ld OR le -> lf
as RSHIFT 5 -> av
fm OR fn -> fo
hm AND ho -> hp
lg OR lm -> ln
NOT kx -> ky
kk RSHIFT 3 -> km
ek AND em -> en
NOT ft -> fu
NOT jh -> ji
jn OR jo -> jp
gj AND gu -> gw
d AND j -> l
et RSHIFT 1 -> fm
jq OR jw -> jx
ep OR eo -> eq
lv LSHIFT 15 -> lz
NOT ey -> ez
jp RSHIFT 2 -> jq
eg AND ei -> ej
NOT dm -> dn
jp AND ka -> kc
as AND bd -> bf
fk OR fj -> fl
dw OR dx -> dy
lj AND ll -> lm
ec AND ee -> ef
fq AND fr -> ft
NOT kp -> kq
ki OR kj -> kk
cz OR cy -> da
as RSHIFT 3 -> au
an LSHIFT 15 -> ar
fj LSHIFT 15 -> fn
1 AND fi -> fj
he RSHIFT 1 -> hx
lf RSHIFT 2 -> lg
kf LSHIFT 15 -> kj
dz AND ef -> eh
ib OR ic -> id
lf RSHIFT 5 -> li
bp OR bq -> br
NOT gs -> gt
fo RSHIFT 1 -> gh
bz AND cb -> cc
ea OR eb -> ec
lf AND lq -> ls
NOT l -> m
hz RSHIFT 3 -> ib
NOT di -> dj
NOT lk -> ll
jp RSHIFT 3 -> jr
jp RSHIFT 5 -> js
NOT bf -> bg
s LSHIFT 15 -> w
eq LSHIFT 1 -> fk
jl OR jk -> jm
hz AND ik -> im
dz OR ef -> eg
1 AND gy -> gz
la LSHIFT 15 -> le
br AND bt -> bu
NOT cn -> co
v OR w -> x
d OR j -> k
1 AND gd -> ge
ia OR ig -> ih
NOT go -> gp
NOT ed -> ee
jq AND jw -> jy
et OR fe -> ff
aw AND ay -> az
ff AND fh -> fi
ir LSHIFT 1 -> jl
gg LSHIFT 1 -> ha
x RSHIFT 2 -> y
db OR dc -> dd
bl OR bm -> bn
ib AND ic -> ie
x RSHIFT 3 -> z
lh AND li -> lk
ce OR cd -> cf
NOT bb -> bc
hi AND hk -> hl
NOT gb -> gc
1 AND r -> s
fw AND fy -> fz
fb AND fd -> fe
1 AND en -> eo
z OR aa -> ab
bi LSHIFT 15 -> bm
hg OR hh -> hi
kh LSHIFT 1 -> lb
cg OR ch -> ci
1 AND kz -> la
gf OR ge -> gg
gj RSHIFT 2 -> gk
dd RSHIFT 2 -> de
NOT ls -> lt
lh OR li -> lj
jr OR js -> jt
au AND av -> ax
0 -> c
he AND hp -> hr
id AND if -> ig
et RSHIFT 5 -> ew
bp AND bq -> bs
e AND f -> h
ly OR lz -> ma
1 AND lu -> lv
NOT jd -> je
ha OR gz -> hb
dy RSHIFT 1 -> er
iu RSHIFT 2 -> iv
NOT hr -> hs
as RSHIFT 1 -> bl
kk RSHIFT 2 -> kl
b AND n -> p
ln AND lp -> lq
cj AND cp -> cr
dl AND dn -> do
ci RSHIFT 2 -> cj
as OR bd -> be
ge LSHIFT 15 -> gi
hz RSHIFT 5 -> ic
dv LSHIFT 1 -> ep
kl OR kr -> ks
gj OR gu -> gv
he RSHIFT 5 -> hh
NOT fg -> fh
hg AND hh -> hj
b OR n -> o
jk LSHIFT 15 -> jo
gz LSHIFT 15 -> hd
cy LSHIFT 15 -> dc
kk RSHIFT 5 -> kn
ci RSHIFT 3 -> ck
at OR az -> ba
iu RSHIFT 3 -> iw
ko AND kq -> kr
NOT eh -> ei
aq OR ar -> as
iy AND ja -> jb
dd RSHIFT 3 -> df
bn RSHIFT 3 -> bp
1 AND cc -> cd
at AND az -> bb
x OR ai -> aj
kk AND kv -> kx
ao OR an -> ap
dy RSHIFT 3 -> ea
x RSHIFT 1 -> aq
eu AND fa -> fc
kl AND kr -> kt
ia AND ig -> ii
df AND dg -> di
NOT fx -> fy
k AND m -> n
bn RSHIFT 5 -> bq
km AND kn -> kp
dt LSHIFT 15 -> dx
hz RSHIFT 2 -> ia
aj AND al -> am
cd LSHIFT 15 -> ch
hc OR hd -> he
he RSHIFT 3 -> hg
bn OR by -> bz
NOT kt -> ku
z AND aa -> ac
NOT ak -> al
cu AND cw -> cx
NOT ie -> if
dy RSHIFT 2 -> dz
ip LSHIFT 15 -> it
de OR dk -> dl
au OR av -> aw
jg AND ji -> jj
ci AND ct -> cv
dy RSHIFT 5 -> eb
hx OR hy -> hz
eu OR fa -> fb
gj RSHIFT 3 -> gl
fo AND fz -> gb
1 AND jj -> jk
jp OR ka -> kb
de AND dk -> dm
ex AND ez -> fa
df OR dg -> dh
iv OR jb -> jc
x RSHIFT 5 -> aa
NOT hj -> hk
NOT im -> in
fl LSHIFT 1 -> gf
hu LSHIFT 15 -> hy
iq OR ip -> ir
iu RSHIFT 5 -> ix
NOT fc -> fd
NOT el -> em
ck OR cl -> cm
et RSHIFT 3 -> ev
hw LSHIFT 1 -> iq
ci RSHIFT 5 -> cl
iv AND jb -> jd
dd RSHIFT 5 -> dg
as RSHIFT 2 -> at
NOT jy -> jz
af AND ah -> ai
1 AND ds -> dt
jx AND jz -> ka
da LSHIFT 1 -> du
fs AND fu -> fv
jp RSHIFT 1 -> ki
iw AND ix -> iz
iw OR ix -> iy
eo LSHIFT 15 -> es
ev AND ew -> ey
ba AND bc -> bd
fp AND fv -> fx
jc AND je -> jf
et RSHIFT 2 -> eu
kg OR kf -> kh
iu OR jf -> jg
er OR es -> et
fo RSHIFT 2 -> fp
NOT ca -> cb
bv AND bx -> by
u LSHIFT 1 -> ao
cm AND co -> cp
y OR ae -> af
bn AND by -> ca
1 AND ke -> kf
jt AND jv -> jw
fq OR fr -> fs
dy AND ej -> el
NOT kc -> kd
ev OR ew -> ex
dd OR do -> dp
NOT cv -> cw
gr AND gt -> gu
dd RSHIFT 1 -> dw
NOT gw -> gx
NOT iz -> ja
1 AND io -> ip
NOT ag -> ah
b RSHIFT 5 -> f
NOT cr -> cs
kb AND kd -> ke
jr AND js -> ju
cq AND cs -> ct
il AND in -> io
NOT ju -> jv
du OR dt -> dv
dd AND do -> dq
b RSHIFT 2 -> d
jm LSHIFT 1 -> kg
NOT dq -> dr
bo OR bu -> bv
gk OR gq -> gr
he OR hp -> hq
NOT h -> i
hf AND hl -> hn
gv AND gx -> gy
x AND ai -> ak
bo AND bu -> bw
hq AND hs -> ht
hz RSHIFT 1 -> is
gj RSHIFT 5 -> gm
g AND i -> j
gk AND gq -> gs
dp AND dr -> ds
b RSHIFT 3 -> e
gl AND gm -> go
gl OR gm -> gn
y AND ae -> ag
hv OR hu -> hw
1674 -> b
ab AND ad -> ae
NOT ac -> ad
1 AND ht -> hu
NOT hn -> ho

Some files were not shown because too many files have changed in this diff Show more