Skip to content

Commit 7ea366e

Browse files
committed
linting
1 parent cc93cd5 commit 7ea366e

File tree

3 files changed

+99
-102
lines changed

3 files changed

+99
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
function calculateFrequency(input) {
2-
let result = 0;
3-
input.split(/\r?\n/).forEach(change => {
4-
result += parseInt(change);
5-
});
6-
return result;
2+
let result = 0;
3+
input.split(/\r?\n/).forEach(change => {
4+
result += parseInt(change);
5+
});
6+
return result;
77

8-
//Better:
9-
// return input
10-
// .split('\n')
11-
// .map((x) => parseInt(x))
12-
// .reduce((a, b) => a + b, 0);
13-
}
8+
//Better:
9+
// return input
10+
// .split('\n')
11+
// .map((x) => parseInt(x))
12+
// .reduce((a, b) => a + b, 0);
13+
}
1414

1515
function calculateFrequencyFoundTwice(input) {
16-
const frequencies = new Set([0]);
17-
let result = 0;
18-
let repetitionFound = false;
16+
const frequencies = new Set([0]);
17+
let result = 0;
18+
let repetitionFound = false;
1919

20-
const changes = input
20+
const changes = input
2121
.split('\n')
2222
.map((x) => parseInt(x));
2323

24-
while (!repetitionFound) {
25-
for (let i=0; i<changes.length; i++) {
26-
result += changes[i];
27-
if (frequencies.has(result)) {
28-
repetitionFound = true;
29-
break;
30-
}
31-
frequencies.add(result);
32-
}
24+
while (!repetitionFound) {
25+
for (let i=0; i<changes.length; i++) {
26+
result += changes[i];
27+
if (frequencies.has(result)) {
28+
repetitionFound = true;
29+
break;
30+
}
31+
frequencies.add(result);
3332
}
34-
return result;
3533
}
34+
return result;
35+
}
3636

3737
module.exports.calculateFrequency = calculateFrequency;
3838
module.exports.calculateFrequencyFoundTwice = calculateFrequencyFoundTwice;

day-01-chronal-calibration/test.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('Day 1: Chronal Calibration', () => {
2020
+1
2121
+1`;
2222

23-
expect(chronalCalibration.calculateFrequency(changes)).to.equal(3);
23+
expect(chronalCalibration.calculateFrequency(changes)).to.equal(3);
2424
});
2525

2626
it('should calculate frequency from +1, +1, -2', () => {
@@ -29,7 +29,7 @@ describe('Day 1: Chronal Calibration', () => {
2929
+1
3030
-2`;
3131

32-
expect(chronalCalibration.calculateFrequency(changes)).to.equal(0);
32+
expect(chronalCalibration.calculateFrequency(changes)).to.equal(0);
3333
});
3434

3535
it('should calculate frequency from -1, -2, -3', () => {
@@ -47,60 +47,60 @@ describe('Day 1: Chronal Calibration', () => {
4747
});
4848
});
4949

50-
describe('Part Two', () => {
51-
it('should calculate first duplicate frequency from +1, -2, +3, +1', () => {
52-
const changes =
50+
describe('Part Two', () => {
51+
it('should calculate first duplicate frequency from +1, -2, +3, +1', () => {
52+
const changes =
5353
`+1
5454
-2
5555
+3
5656
+1`;
5757

58-
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(2);
59-
});
58+
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(2);
59+
});
6060

61-
it('should calculate first duplicate frequency from +1, -1', () => {
62-
const changes =
61+
it('should calculate first duplicate frequency from +1, -1', () => {
62+
const changes =
6363
`+1
6464
-1`;
6565

66-
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(0);
67-
});
66+
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(0);
67+
});
6868

69-
it('should calculate first duplicate frequency from +3, +3, +4, -2, -4', () => {
70-
const changes =
69+
it('should calculate first duplicate frequency from +3, +3, +4, -2, -4', () => {
70+
const changes =
7171
`+3
7272
+3
7373
+4
7474
-2
7575
-4`;
7676

77-
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(10);
78-
});
77+
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(10);
78+
});
7979

80-
it('should calculate first duplicate frequency from -6, +3, +8, +5, -6', () => {
81-
const changes =
80+
it('should calculate first duplicate frequency from -6, +3, +8, +5, -6', () => {
81+
const changes =
8282
`-6
8383
+3
8484
+8
8585
+5
8686
-6`;
8787

88-
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(5);
89-
});
88+
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(5);
89+
});
9090

91-
it('should calculate first duplicate frequency from +7, +7, -2, -7, -4', () => {
92-
const changes =
91+
it('should calculate first duplicate frequency from +7, +7, -2, -7, -4', () => {
92+
const changes =
9393
`+7
9494
+7
9595
-2
9696
-7
9797
-4`;
9898

99-
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(14);
100-
});
99+
expect(chronalCalibration.calculateFrequencyFoundTwice(changes)).to.equal(14);
100+
});
101101

102-
it('should calculate first duplicate frequency from Input file', () => {
103-
const input = fs.readFileSync('day-01-chronal-calibration/input.txt').toString();
104-
expect(chronalCalibration.calculateFrequencyFoundTwice(input)).to.equal(709);
105-
});
102+
it('should calculate first duplicate frequency from Input file', () => {
103+
const input = fs.readFileSync('day-01-chronal-calibration/input.txt').toString();
104+
expect(chronalCalibration.calculateFrequencyFoundTwice(input)).to.equal(709);
106105
});
106+
});

day-02-inventory-management-system/inventory-management.js

+47-50
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,58 @@
11
function calculateChecksum(input) {
2-
const boxIds = input
3-
.split('\n')
4-
.map((x) => x.trim());
5-
6-
let twoTimes = 0;
7-
let threeTimes = 0;
8-
9-
boxIds.forEach(id => {
10-
let map = new Map();
11-
id.split('').forEach(char => {
12-
if (map.has(char)) {
13-
map.set(char, map.get(char)+1);
14-
} else {
15-
map.set(char, 1);
16-
}
17-
});
18-
19-
const counts = Array.from(map.values());
20-
let found2 = Object.values(counts).includes(2);
21-
let found3 = counts.filter(count => count === 3).length > 0;
22-
23-
if (found2) {
24-
twoTimes++;
25-
}
26-
if (found3) {
27-
threeTimes++;
28-
}
2+
const boxIds = input
3+
.split('\n')
4+
.map((x) => x.trim());
5+
6+
let twoTimes = 0;
7+
let threeTimes = 0;
8+
9+
boxIds.forEach(id => {
10+
let map = new Map();
11+
id.split('').forEach(char => {
12+
if (map.has(char)) {
13+
map.set(char, map.get(char) + 1);
14+
} else {
15+
map.set(char, 1);
16+
}
2917
});
30-
return twoTimes * threeTimes;
18+
19+
const counts = Array.from(map.values());
20+
let found2 = Object.values(counts).includes(2);
21+
let found3 = counts.filter(count => count === 3).length > 0;
22+
23+
if (found2) {
24+
twoTimes++;
25+
}
26+
if (found3) {
27+
threeTimes++;
28+
}
29+
});
30+
return twoTimes * threeTimes;
3131
}
3232

3333
function findCommonLetters(input) {
3434

35-
const ids = input
36-
.split('\n')
37-
.map((x) => x.trim());
38-
39-
for (let i = 0; i < ids.length; i++) {
40-
for (let j = 0; j < ids.length; j++) {
41-
//Calculate number of diffs
42-
let diffs = 0;
43-
for (var x = 0; x < ids.length && diffs < 2; x++) {
44-
if (ids[i].charAt(x) != ids[j].charAt(x)) {
45-
diffs++;
46-
}
47-
}
48-
if (diffs == 1) {
49-
let match = "";
50-
for (var x = 0; x < ids.length && diffs < 2; x++) {
51-
if (ids[i].charAt(x) == ids[j].charAt(x)) {
52-
match = match + ids[i].charAt(x);
53-
}
54-
}
55-
return match;
56-
}
35+
const ids = input
36+
.split('\n')
37+
.map((x) => x.trim());
38+
39+
for (let i = 0; i < ids.length; i++) {
40+
for (let j = 0; j < ids.length; j++) {
41+
//Calculate number of diffs
42+
let diffs = 0;
43+
let common = '';
44+
for (var x = 0; x < ids.length && diffs < 2; x++) {
45+
if (ids[i].charAt(x) != ids[j].charAt(x)) {
46+
diffs++;
47+
} else {
48+
common = common + ids[i].charAt(x);
5749
}
50+
}
51+
if (diffs == 1) {
52+
return common;
53+
}
5854
}
55+
}
5956
}
6057

6158
module.exports.calculateChecksum = calculateChecksum;

0 commit comments

Comments
 (0)