aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
Diffstat (limited to '2020')
-rw-r--r--2020/14-Docking_Data/.eslintrc.json44
-rw-r--r--2020/14-Docking_Data/package.json11
-rw-r--r--2020/14-Docking_Data/second.js62
3 files changed, 117 insertions, 0 deletions
diff --git a/2020/14-Docking_Data/.eslintrc.json b/2020/14-Docking_Data/.eslintrc.json
new file mode 100644
index 0000000..47dd5e6
--- /dev/null
+++ b/2020/14-Docking_Data/.eslintrc.json
@@ -0,0 +1,44 @@
+{
+ "env": {
+ "es2021": true,
+ "node": true
+ },
+ "extends": [
+ "eslint:recommended",
+ "plugin:node/recommended"
+ ],
+ "overrides": [
+ {
+ "files": ["*.js"],
+ "rules": {
+ "no-constant-condition": "off"
+ }
+ }
+ ],
+ "parserOptions": {
+ "ecmaVersion": "latest",
+ "sourceType": "module"
+ },
+ "rules": {
+ "indent": [
+ "error",
+ "tab"
+ ],
+ "linebreak-style": [
+ "error",
+ "unix"
+ ],
+ "quotes": [
+ "error",
+ "single"
+ ],
+ "semi": [
+ "error",
+ "always"
+ ],
+ "node/no-unsupported-features/es-syntax": [
+ "error",
+ { "ignores": ["modules"] }
+ ]
+ }
+}
diff --git a/2020/14-Docking_Data/package.json b/2020/14-Docking_Data/package.json
new file mode 100644
index 0000000..23be024
--- /dev/null
+++ b/2020/14-Docking_Data/package.json
@@ -0,0 +1,11 @@
+{
+ "type": "module",
+ "engines": {
+ "node": ">=18.10.0"
+ },
+ "dependencies": {
+ "eslint": "^8.30.0",
+ "eslint-plugin-node": "^11.1.0",
+ "jslint": "^0.12.1"
+ }
+}
diff --git a/2020/14-Docking_Data/second.js b/2020/14-Docking_Data/second.js
new file mode 100644
index 0000000..e4d270f
--- /dev/null
+++ b/2020/14-Docking_Data/second.js
@@ -0,0 +1,62 @@
+import * as fs from 'fs';
+
+const setMask = /mask = ([10X]+)/;
+const operation = /mem\[(\d+)] = (\d+)/;
+
+function applyMask(addr, mask) {
+ let ret = [parseInt(addr).toString(2).padStart(36, '0')];
+ for (let i=0; i<36; ++i) {
+ switch (mask.charAt(i)) {
+ case '1':
+ ret = ret.map(a => a.substring(0, i) + '1' + a.substring(i+1));
+ break;
+ case 'X':
+ ret = ret.reduce((acc, a) => acc.concat([
+ a.substring(0, i) + '1' + a.substring(i+1),
+ a.substring(0, i) + '0' + a.substring(i+1),
+ ]), []);
+ }
+ }
+ return ret.map(a => parseInt(a, 2));
+}
+
+function processMasks(lines) {
+ let ret = new Map();
+ let mask = '';
+ lines.forEach(line => {
+ const m = line.match(setMask);
+ if (m) {
+ mask = m[1];
+ } else {
+ const o = line.match(operation);
+ const addr = o[1];
+ const value = parseInt(o[2]);
+ applyMask(addr, mask).forEach(r => ret.set(parseInt(r), value));
+ }
+ });
+ return ret;
+}
+
+function load(filename) {
+ return processMasks(fs.readFileSync(filename, 'utf8')
+ .trim()
+ .split('\n')
+ );
+}
+
+let example = load('example2');
+let input = load('input');
+
+function solve(input) {
+ let sum = 0;
+ input.forEach(value => sum += value);
+ return sum;
+}
+
+const exampleOutput = solve(example);
+if (exampleOutput !== 208) {
+ console.log('Example failed with ' + exampleOutput);
+ process.exit(1); // eslint-disable-line
+}
+
+console.log(solve(input));