2020-14 part 2 in javascript
This commit is contained in:
parent
c3e29b9d80
commit
81cd59c03a
3 changed files with 117 additions and 0 deletions
44
2020/14-Docking_Data/.eslintrc.json
Normal file
44
2020/14-Docking_Data/.eslintrc.json
Normal file
|
@ -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"] }
|
||||
]
|
||||
}
|
||||
}
|
11
2020/14-Docking_Data/package.json
Normal file
11
2020/14-Docking_Data/package.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
62
2020/14-Docking_Data/second.js
Normal file
62
2020/14-Docking_Data/second.js
Normal file
|
@ -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));
|
Loading…
Add table
Reference in a new issue