Skip to content

Commit 1569811

Browse files
committed
ESLint JS code
1 parent 9e47a9e commit 1569811

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+8665
-640
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
__pycache__/

advent-of-code/2015/.eslintrc.json

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true
5+
},
6+
"extends": [
7+
"airbnb-base"
8+
],
9+
"globals": {
10+
"Atomics": "readonly",
11+
"SharedArrayBuffer": "readonly"
12+
},
13+
"parserOptions": {
14+
"ecmaVersion": 11
15+
},
16+
"rules": {
17+
"linebreak-style": "off",
18+
"no-plusplus": "off",
19+
"no-param-reassign": "off",
20+
"no-console": "off",
21+
"comma-dangle": "off",
22+
"no-restricted-syntax": [
23+
"error",
24+
{
25+
"selector": "ForInStatement",
26+
"message": "for..in loops iterate over the entire prototype chain, which is virtually never what you want. Use Object.{keys,values,entries}, and iterate over the resulting array."
27+
},
28+
{
29+
"selector": "LabeledStatement",
30+
"message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
31+
},
32+
{
33+
"selector": "WithStatement",
34+
"message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
35+
}
36+
],
37+
"no-unused-expressions": [
38+
"error",
39+
{
40+
"allowTernary": true
41+
}
42+
],
43+
"max-len": [
44+
"error",
45+
100,
46+
2,
47+
{
48+
"ignoreUrls": true,
49+
"ignoreComments": true,
50+
"ignoreRegExpLiterals": true,
51+
"ignoreStrings": true,
52+
"ignoreTemplateLiterals": true
53+
}
54+
],
55+
"no-confusing-arrow": "off",
56+
"implicit-arrow-linebreak": "off",
57+
"no-bitwise": "off"
58+
}
59+
}

advent-of-code/2015/day-01/part-1/solution.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const fs = require('fs');
2-
const INPUT = fs.readFileSync(__dirname + '/input.txt').toString(); // Using absolute URL for VS Code tasks
2+
3+
const INPUT = fs.readFileSync(`${__dirname}/input.txt`).toString(); // Using absolute URL for VS Code tasks
4+
35
const result = INPUT.split('').reduce(
46
(floor, direction) => (direction === '(' ? ++floor : --floor),
57
0

advent-of-code/2015/day-01/part-2/solution.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
2-
const INPUT = fs.readFileSync(__dirname + '/input.txt').toString();
2+
3+
const INPUT = fs.readFileSync(`${__dirname}/input.txt`).toString();
34

45
let result = 0;
56

@@ -13,6 +14,8 @@ for (const [i, c] of INPUT.split('').entries()) {
1314

1415
// ALTERNATIVE SOLUTION: MORE ELEGANT BUT MUST GO THROUGH WHOLE ARRAY
1516
// let floor = 0;
16-
// let result = INPUT.split('').map(direction => direction === '(' ? ++floor : --floor).indexOf(-1) + 1;
17+
// let result = INPUT.split('').map(
18+
// direction => direction === '(' ? ++floor : --floor
19+
// ).indexOf(-1) + 1;
1720

1821
console.log(result);

advent-of-code/2015/day-02/part-1/solution.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('\n');
67

7-
console.log(INPUT);
8-
98
const result = INPUT.reduce((totalArea, element) => {
109
const [l, w, h] = element.split('x');
1110
const [lw, wh, lh] = [l * w, w * h, l * h];

advent-of-code/2015/day-02/part-2/solution.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('\n');
67

78
const result = INPUT.reduce((totalLength, element) => {
89
const sides = element.split('x').map(Number); // Number is a function, we map to Number in order to use '!==' to compare with an int.
910
const maxSideIndex = sides.indexOf(Math.max(...sides)); // We use the index because 2 sides can be the max side but we want to remove only 1.
1011
return (
11-
totalLength +
12-
sides.reduce(Math.imul) +
13-
sides.reduce(
12+
totalLength
13+
+ sides.reduce(Math.imul)
14+
+ sides.reduce(
1415
(perimeter, side, i) =>
1516
i !== maxSideIndex ? perimeter + 2 * side : perimeter,
1617
0

advent-of-code/2015/day-03/part-1/solution.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('');
67

advent-of-code/2015/day-03/part-2/solution.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('');
67
const santaDirections = INPUT.filter((item, i) => i % 2 === 0);
78
const roboDirections = INPUT.filter((item, i) => i % 2 === 1);
89

910
const houses = new Set().add('0,0');
1011

11-
const traverse = directions => {
12+
const traverse = (directions) => {
1213
const currentHouse = [0, 0];
13-
directions.forEach(direction => {
14+
directions.forEach((direction) => {
1415
// Shorter but slower alternative to the switch statement used in part-1:
1516
currentHouse[0] += ((direction === '>') | 0) - ((direction === '<') | 0);
1617
currentHouse[1] += ((direction === '^') | 0) - ((direction === 'v') | 0);

advent-of-code/2015/day-04/solution.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const crypto = require('crypto');
2+
23
const INPUT = 'iwrupvqb';
4+
35
const ZEROS_NEEDED = 5;
46

5-
const md5 = data =>
7+
const md5 = (data) =>
68
crypto
79
.createHash('md5')
810
.update(data)
911
.digest('hex'); // A message digest is a cryptographic hash function containing a string of digits created by a one-way hashing formula.
1012

11-
const isStartingWithFiveZeros = data =>
13+
const isStartingWithFiveZeros = (data) =>
1214
data.slice(0, ZEROS_NEEDED) === '0'.repeat(ZEROS_NEEDED);
1315

1416
let n = 0;

advent-of-code/2015/day-05/part-1/solution.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('\n');
67

78
const VOWELS = ['a', 'e', 'i', 'o', 'u'];
89
const DOUBLE_LETTERS = 'abcdefghijklmnopqrstuvwxyz'
910
.split('')
10-
.map(item => item + item);
11+
.map((item) => item + item);
1112
const BAD_PAIRS = ['ab', 'cd', 'pq', 'xy'];
1213

13-
const hasThreeVowels = string =>
14+
const hasThreeVowels = (string) =>
1415
string
1516
.split('')
1617
.reduce(
1718
(vowelCount, char) =>
1819
VOWELS.indexOf(char) === -1 ? vowelCount : ++vowelCount,
1920
0
2021
) >= 3;
21-
const hasDoubleLetter = string =>
22-
DOUBLE_LETTERS.some(item => string.indexOf(item) !== -1);
23-
const hasBadPair = string =>
24-
BAD_PAIRS.some(item => string.indexOf(item) !== -1);
22+
const hasDoubleLetter = (string) =>
23+
DOUBLE_LETTERS.some((item) => string.indexOf(item) !== -1);
24+
const hasBadPair = (string) =>
25+
BAD_PAIRS.some((item) => string.indexOf(item) !== -1);
2526

26-
const isNiceString = string =>
27+
const isNiceString = (string) =>
2728
hasThreeVowels(string) && hasDoubleLetter(string) && !hasBadPair(string);
2829

2930
const result = INPUT.reduce(

advent-of-code/2015/day-05/part-2/solution.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('\n');
67

7-
const hasRepeatingPair = string => /([a-z][a-z]).*\1/.test(string);
8-
const hasLetterSandwich = string => /([a-z])[a-z]\1/.test(string);
8+
const hasRepeatingPair = (string) => /([a-z][a-z]).*\1/.test(string);
9+
const hasLetterSandwich = (string) => /([a-z])[a-z]\1/.test(string);
910

10-
const isNiceString = string =>
11+
const isNiceString = (string) =>
1112
hasRepeatingPair(string) && hasLetterSandwich(string);
1213

1314
const result = INPUT.reduce(

advent-of-code/2015/day-06/part-1/solution.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const fs = require('fs');
2+
23
const INPUT = fs
3-
.readFileSync(__dirname + '/input.txt')
4+
.readFileSync(`${__dirname}/input.txt`)
45
.toString()
56
.split('\n');
7+
68
const COMMANDS_REGEX = /(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/;
79
const GRID_LENGTH = 1000;
810

9-
const parseCommand = string => {
11+
const parseCommand = (string) => {
1012
const command = string.match(COMMANDS_REGEX);
1113
return {
1214
command: command[1],
@@ -19,17 +21,20 @@ const parseCommand = string => {
1921

2022
const lights = new Uint8Array(GRID_LENGTH * GRID_LENGTH); // Contents initialized to 0.
2123

22-
INPUT.forEach(str => {
24+
INPUT.forEach((str) => {
2325
const commandObj = parseCommand(str);
2426

2527
for (let x = commandObj.x1; x <= commandObj.x2; x++) {
2628
for (let y = commandObj.y1; y <= commandObj.y2; y++) {
2729
const i = GRID_LENGTH * x + y;
2830

29-
if (commandObj.command === 'turn on') lights[i] = 1;
30-
else if (commandObj.command === 'turn off') lights[i] = 0;
31-
else if (commandObj.command === 'toggle')
31+
if (commandObj.command === 'turn on') {
32+
lights[i] = 1;
33+
} else if (commandObj.command === 'turn off') {
34+
lights[i] = 0;
35+
} else if (commandObj.command === 'toggle') {
3236
lights[i] = lights[i] === 0 ? 1 : 0;
37+
}
3338
}
3439
}
3540
});

advent-of-code/2015/day-06/part-2/solution.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const fs = require('fs');
2-
const INPUT = fs.readFileSync(__dirname + '/input.txt', 'utf-8').split('\n');
2+
3+
const INPUT = fs.readFileSync(`${__dirname}/input.txt`, 'utf-8').split('\n');
4+
35
const COMMANDS_REGEX = /(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/;
46
const GRID_LENGTH = 1000;
57

6-
const parseCommand = string => {
8+
const parseCommand = (string) => {
79
const command = string.match(COMMANDS_REGEX);
810
return {
911
command: command[1],
@@ -16,7 +18,7 @@ const parseCommand = string => {
1618

1719
const lights = new Uint8Array(GRID_LENGTH * GRID_LENGTH);
1820

19-
INPUT.forEach(str => {
21+
INPUT.forEach((str) => {
2022
const commandObj = parseCommand(str);
2123

2224
for (let x = commandObj.x1; x <= commandObj.x2; x++) {

advent-of-code/2015/day-07/solution.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const fs = require('fs');
2-
const INPUT = fs.readFileSync(__dirname + '/input.txt', 'utf-8').split('\n');
2+
3+
const INPUT = fs.readFileSync(`${__dirname}/input.txt`, 'utf-8').split('\n');
4+
35
const COMMAND_REGEX = /[A-Z]+/g;
46
const ARGUMENTS_REGEX = /[a-z0-9]+/g;
57

@@ -10,31 +12,31 @@ const WIRES = new Map();
1012
const BITWISE_METHODS = {
1113
AND: (a, b) => a & b,
1214
OR: (a, b) => a | b,
13-
NOT: a => ~a,
15+
NOT: (a) => ~a,
1416
LSHIFT: (a, b) => a << b,
1517
RSHIFT: (a, b) => a >> b
1618
};
1719

1820
// Parse instruction and return object with command, arguments and destination wire
19-
const parseInstruction = instruction => {
21+
const parseInstruction = (instruction) => {
2022
const command = instruction.match(COMMAND_REGEX); // e.g. ['OR'], can also be null
2123
const args = instruction.match(ARGUMENTS_REGEX); // e.g. ['eh'], or ['hg, '3']
2224
const destination = args.pop();
2325

2426
return {
2527
command: command && command[0],
26-
args: args.map(arg => (isNaN(Number(arg)) ? arg : Number(arg))),
27-
destination: destination
28+
args: args.map((arg) => (Number.isNaN(Number(arg)) ? arg : Number(arg))),
29+
destination
2830
};
2931
};
3032

3133
// Calculate value for a wire recursively
32-
const calculateWire = wireName => {
34+
const calculateWire = (wireName) => {
3335
const wire = WIRES.get(wireName);
3436

3537
if (typeof wireName === 'number') return wireName;
3638
if (typeof wire === 'number') return wire;
37-
else if (typeof wire === 'undefined') return undefined;
39+
if (typeof wire === 'undefined') return undefined;
3840

3941
if (wire.command) {
4042
WIRES.set(
@@ -51,7 +53,7 @@ const calculateWire = wireName => {
5153
return WIRES.get(wireName);
5254
};
5355

54-
INPUT.forEach(instruction => {
56+
INPUT.forEach((instruction) => {
5557
const parsedInstruction = parseInstruction(instruction);
5658
WIRES.set(parsedInstruction.destination, {
5759
command: parsedInstruction.command,

advent-of-code/2015/day-08/part-1/solution.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
const fs = require('fs');
2-
const INPUT = fs.readFileSync(__dirname + '/input.txt', 'utf-8').split('\n');
32

4-
// The eval() function evaluates JavaScript code represented as a string.
5-
// eval() is a dangerous function, which executes the code it's passed with the privileges of the caller.
6-
// If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code.
3+
const INPUT = fs.readFileSync(`${__dirname}/input.txt`, 'utf-8').split('\n');
74

85
const result = INPUT.reduce(
9-
(acc, line) => acc + (line.length - eval(line).length),
6+
(acc, line) => acc + (line.length - eval(line).length), // eslint-disable-line no-eval
107
0
118
);
129

advent-of-code/2015/day-08/part-2/solution.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const fs = require('fs');
2-
const INPUT = fs.readFileSync(__dirname + '/input.txt', 'utf-8').split('\n');
2+
3+
const INPUT = fs.readFileSync(`${__dirname}/input.txt`, 'utf-8').split('\n');
34

45
const result = INPUT.reduce(
56
(acc, line) =>
6-
acc +
7-
(2 + line.replace(/\\/g, '\\\\').replace(/"/g, '\\"').length - line.length),
7+
acc
8+
+ (2 + line.replace(/\\/g, '\\\\').replace(/"/g, '\\"').length - line.length),
89
0
910
);
1011

0 commit comments

Comments
 (0)