Skip to content

Commit 624c90b

Browse files
authored
[2017] Add tests for each day (#18)
- Switch to Jest for tests - Use ESLint directly (rather than semistandard) - Add missing tests for each day - Run `npm run lint-fix`
1 parent 5e8c64e commit 624c90b

Some content is hidden

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

63 files changed

+4683
-5326
lines changed

Diff for: .github/workflows/2017.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Advent of Code 2017
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- "2017/**"
9+
pull_request:
10+
paths:
11+
- "2017/**"
12+
13+
defaults:
14+
run:
15+
working-directory: "2017"
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Use Node.js 10.x
24+
uses: actions/setup-node@v1
25+
with:
26+
node-version: "10.x"
27+
- run: npm ci
28+
- run: npm test
29+
env:
30+
CI: true

Diff for: 2017/.eslintrc.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"env": {
3+
"commonjs": true,
4+
"es2021": true,
5+
"node": true
6+
},
7+
"extends": "eslint:recommended",
8+
"parserOptions": {
9+
"ecmaVersion": 12
10+
},
11+
"rules": {
12+
"indent": ["error", 2, { "SwitchCase": 1 }],
13+
"linebreak-style": ["error", "unix"],
14+
"quotes": ["error", "single", { "avoidEscape": true }],
15+
"semi": ["error", "always"]
16+
}
17+
}

Diff for: 2017/.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

Diff for: 2017/day01/index.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs');
22

33
class Day01 {
4-
static sumString (str, offset = 1) {
4+
static sumString(str, offset = 1) {
55
let sum = 0;
66
const length = str.length;
77

@@ -14,17 +14,14 @@ class Day01 {
1414
return sum;
1515
}
1616

17-
static sumStringHalfway (str) {
17+
static sumStringHalfway(str) {
1818
return this.sumString(str, Math.floor(str.length / 2));
1919
}
2020

21-
static run (input) {
21+
static run(input) {
2222
const fileContent = fs.readFileSync(input, 'utf8').trim();
2323

24-
return [
25-
this.sumString(fileContent),
26-
this.sumStringHalfway(fileContent)
27-
];
24+
return [this.sumString(fileContent), this.sumStringHalfway(fileContent)];
2825
}
2926
}
3027

Diff for: 2017/day01/index.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* global describe, expect, test */
2+
3+
const Day01 = require('.');
4+
5+
describe('Examples', () => {
6+
test('Part 1', () => {
7+
expect(Day01.sumString('1122')).toBe(3);
8+
expect(Day01.sumString('1111')).toBe(4);
9+
expect(Day01.sumString('1234')).toBe(0);
10+
expect(Day01.sumString('91212129')).toBe(9);
11+
});
12+
13+
test('Part 2', () => {
14+
expect(Day01.sumStringHalfway('1212')).toBe(6);
15+
expect(Day01.sumStringHalfway('1221')).toBe(0);
16+
expect(Day01.sumStringHalfway('123425')).toBe(4);
17+
expect(Day01.sumStringHalfway('123123')).toBe(12);
18+
expect(Day01.sumStringHalfway('12131415')).toBe(4);
19+
});
20+
});
21+
22+
test('Input', () => {
23+
const [part1, part2] = Day01.run('./day01/input');
24+
expect(part1).toBe(1102);
25+
expect(part2).toBe(1076);
26+
});

Diff for: 2017/day02/index.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs');
22

33
class Day02 {
4-
static * parseRows (str) {
4+
static *parseRows(str) {
55
let current = '';
66
let row = [];
77

@@ -20,7 +20,7 @@ class Day02 {
2020
}
2121
}
2222

23-
static minMaxDiff (row) {
23+
static minMaxDiff(row) {
2424
let min = Number.MAX_SAFE_INTEGER;
2525
let max = Number.MIN_SAFE_INTEGER;
2626
for (const num of row) {
@@ -31,7 +31,7 @@ class Day02 {
3131
return max - min;
3232
}
3333

34-
static evenlyDivisibleQuotient (row) {
34+
static evenlyDivisibleQuotient(row) {
3535
for (const num1 of row) {
3636
for (const num2 of row) {
3737
if (num1 === num2) {
@@ -47,7 +47,7 @@ class Day02 {
4747
}
4848
}
4949

50-
static run (input) {
50+
static run(input) {
5151
const fileContent = fs.readFileSync(input, 'utf8');
5252

5353
let minMaxSum = 0;
@@ -57,10 +57,7 @@ class Day02 {
5757
evenlyDivisibleSum += this.evenlyDivisibleQuotient(row);
5858
}
5959

60-
return [
61-
minMaxSum,
62-
evenlyDivisibleSum
63-
];
60+
return [minMaxSum, evenlyDivisibleSum];
6461
}
6562
}
6663

Diff for: 2017/day02/index.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* global expect, test */
2+
3+
const Day02 = require('.');
4+
5+
test('Input', () => {
6+
const [part1, part2] = Day02.run('./day02/input');
7+
expect(part1).toBe(42378);
8+
expect(part2).toBe(246);
9+
});

Diff for: 2017/day03/index.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
const fs = require('fs');
2+
13
class Day03 {
2-
static getSideLength (count) {
4+
static getSideLength(count) {
35
const length = Math.ceil(Math.sqrt(count));
46
if (length % 2 === 0) {
57
return length + 1;
@@ -8,7 +10,7 @@ class Day03 {
810
return length;
911
}
1012

11-
static findCoordinates (count) {
13+
static findCoordinates(count) {
1214
const length = this.getSideLength(count);
1315
const halfLength = Math.floor(length / 2);
1416

@@ -35,12 +37,12 @@ class Day03 {
3537
}
3638
}
3739

38-
static manhattanDistance (count) {
40+
static manhattanDistance(count) {
3941
const { x, y } = this.findCoordinates(count);
4042
return Math.abs(x) + Math.abs(y);
4143
}
4244

43-
static findIndex (x, y) {
45+
static findIndex(x, y) {
4446
let length = 0;
4547
let side = 0;
4648
let pos = 0;
@@ -71,15 +73,15 @@ class Day03 {
7173
return side * (length - 1) + pos + Math.pow(length - 2, 2) - 1;
7274
}
7375

74-
static getValidIndex (arr, index) {
76+
static getValidIndex(arr, index) {
7577
if (index < arr.length) {
7678
return arr[index];
7779
}
7880

7981
return 0;
8082
}
8183

82-
static calculateAdjacentSum (arr, x, y) {
84+
static calculateAdjacentSum(arr, x, y) {
8385
let sum = 0;
8486

8587
sum += this.getValidIndex(arr, this.findIndex(x + 1, y));
@@ -95,15 +97,15 @@ class Day03 {
9597
return sum;
9698
}
9799

98-
static * generateSums () {
100+
static *generateSums() {
99101
const arr = [];
100102
let side = 3;
101103
let x = 0;
102104
let y = 0;
103105

104106
arr.push(1);
105107

106-
for (;; side += 2) {
108+
for (; ; side += 2) {
107109
x++;
108110
yield this.calculateAdjacentSum(arr, x, y);
109111

@@ -129,21 +131,19 @@ class Day03 {
129131
}
130132
}
131133

132-
static searchSums (search) {
134+
static searchSums(search) {
133135
for (const sum of this.generateSums()) {
134136
if (sum > search) {
135137
return sum;
136138
}
137139
}
138140
}
139141

140-
static run (input) {
141-
const num = Number.parseInt(input);
142+
static run(input) {
143+
const fileContent = fs.readFileSync(input, 'utf8');
144+
const num = Number.parseInt(fileContent.trim());
142145

143-
return [
144-
this.manhattanDistance(num),
145-
this.searchSums(num)
146-
];
146+
return [this.manhattanDistance(num), this.searchSums(num)];
147147
}
148148
}
149149

Diff for: 2017/day03/index.test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* global describe, expect, test */
2+
3+
const Day03 = require('.');
4+
5+
describe('Part 1', () => {
6+
test('Part 1', () => {
7+
expect(Day03.manhattanDistance(1)).toBe(0);
8+
expect(Day03.manhattanDistance(12)).toBe(3);
9+
expect(Day03.manhattanDistance(23)).toBe(2);
10+
expect(Day03.manhattanDistance(1024)).toBe(31);
11+
});
12+
13+
test('Part 2', () => {
14+
expect(Day03.searchSums(0)).toBe(1);
15+
expect(Day03.searchSums(1)).toBe(2);
16+
expect(Day03.searchSums(3)).toBe(4);
17+
expect(Day03.searchSums(24)).toBe(25);
18+
expect(Day03.searchSums(350)).toBe(351);
19+
});
20+
});
21+
22+
test('Input', () => {
23+
const [part1, part2] = Day03.run('./day03/input');
24+
expect(part1).toBe(430);
25+
expect(part2).toBe(312453);
26+
});

Diff for: 2017/day03/input

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
312051

Diff for: 2017/day04/index.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const fs = require('fs');
22

33
class Day04 {
4-
static * parseRows (str) {
4+
static *parseRows(str) {
55
let current = '';
66

77
for (const ch of str) {
@@ -14,7 +14,7 @@ class Day04 {
1414
}
1515
}
1616

17-
static isPassphraseValid (passphrase, checkAnagrams = false) {
17+
static isPassphraseValid(passphrase, checkAnagrams = false) {
1818
const set = new Set();
1919
const words = passphrase.split(' ');
2020
for (let word of words) {
@@ -32,7 +32,7 @@ class Day04 {
3232
return true;
3333
}
3434

35-
static run (input) {
35+
static run(input) {
3636
const fileContent = fs.readFileSync(input, 'utf8');
3737

3838
let validCount = 0;
@@ -47,10 +47,7 @@ class Day04 {
4747
}
4848
}
4949

50-
return [
51-
validCount,
52-
validAnagramCount
53-
];
50+
return [validCount, validAnagramCount];
5451
}
5552
}
5653

Diff for: 2017/day04/index.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* global describe, expect, test */
2+
3+
const Day04 = require('.');
4+
5+
describe('Examples', () => {
6+
test('Part 1', () => {
7+
expect(Day04.isPassphraseValid('aa bb cc dd ee')).toBe(true);
8+
expect(!Day04.isPassphraseValid('aa bb cc dd aa')).toBe(true);
9+
expect(Day04.isPassphraseValid('aa bb cc dd aaa')).toBe(true);
10+
});
11+
12+
test('Part 2', () => {
13+
expect(Day04.isPassphraseValid('abcde fghij', true)).toBe(true);
14+
expect(!Day04.isPassphraseValid('abcde xyz ecdab', true)).toBe(true);
15+
expect(Day04.isPassphraseValid('a ab abc abd abf abj', true)).toBe(true);
16+
expect(Day04.isPassphraseValid('iiii oiii ooii oooi oooo', true)).toBe(
17+
true
18+
);
19+
expect(!Day04.isPassphraseValid('oiii ioii iioi iiio', true)).toBe(true);
20+
});
21+
});
22+
23+
test('Input', () => {
24+
const [part1, part2] = Day04.run('./day04/input');
25+
expect(part1).toBe(455);
26+
expect(part2).toBe(186);
27+
});

0 commit comments

Comments
 (0)