Skip to content

Commit f4f2fa4

Browse files
committed
Format files with Prettier
1 parent 00e73fd commit f4f2fa4

File tree

24 files changed

+345
-146
lines changed

24 files changed

+345
-146
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ jobs:
1010
- uses: oven-sh/setup-bun@v2
1111
- run: bun install
1212
- run: bun lint
13+
- run: bun format:check

index.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/* eslint-disable unicorn/no-process-exit */
2-
import zeroFill from 'zero-fill';
3-
import path from 'node:path';
4-
import {fileURLToPath} from 'node:url';
52
import {fork} from 'node:child_process';
63
import fs from 'node:fs/promises';
7-
import logSymbols from 'log-symbols';
4+
import path from 'node:path';
5+
import {fileURLToPath} from 'node:url';
86
// eslint-disable-next-line import/no-unresolved
97
import got from 'got';
10-
import {CookieJar} from 'tough-cookie';
8+
import logSymbols from 'log-symbols';
119
import makeDir from 'make-dir';
10+
import {CookieJar} from 'tough-cookie';
11+
import zeroFill from 'zero-fill';
1212
import config from './config.js';
1313

1414
async function fileExists(filePath) {
@@ -24,7 +24,10 @@ async function downloadInputFile(year, dayNumber, inputFilePath) {
2424
const unpaddedDayNumber = Number(dayNumber);
2525
const inputUrl = `https://adventofcode.com/${year}/day/${unpaddedDayNumber}/input`;
2626
const cookieJar = new CookieJar();
27-
await cookieJar.setCookie(`session=${config.cookieSession}`, 'https://adventofcode.com');
27+
await cookieJar.setCookie(
28+
`session=${config.cookieSession}`,
29+
'https://adventofcode.com',
30+
);
2831
const {body: inputContent} = await got(inputUrl, {cookieJar});
2932

3033
// Save input file
@@ -50,12 +53,10 @@ async function checkAndCreateDayDirectories(dayDirectoryPath) {
5053
console.log(logSymbols.success, 'Created day directory');
5154
}
5255

53-
const yearInput = process.argv.length > 3
54-
? process.argv[2]
55-
: new Date().getFullYear();
56-
let dayNumberInput = process.argv.length > 3
57-
? process.argv[3]
58-
: process.argv[2];
56+
const yearInput =
57+
process.argv.length > 3 ? process.argv[2] : new Date().getFullYear();
58+
let dayNumberInput =
59+
process.argv.length > 3 ? process.argv[3] : process.argv[2];
5960
const today = new Date();
6061
const isWithinDecember = today.getMonth() === 11 && today.getDate() <= 25;
6162

@@ -91,11 +92,20 @@ await checkAndCreateDayDirectories(dayDirectoryPath);
9192
const dayFileExists = await fileExists(dayFilePath);
9293

9394
if (!dayFileExists) {
94-
console.error(logSymbols.error, `File \`./solutions/${yearInput}/${dayNumber}/${dayNumber}.js\` does not exist`);
95+
console.error(
96+
logSymbols.error,
97+
`File \`./solutions/${yearInput}/${dayNumber}/${dayNumber}.js\` does not exist`,
98+
);
9599
console.log('Creating day file...');
96-
const dayTemplateContent = await fs.readFile(path.join(directoryPath, 'template.js'), 'utf8');
100+
const dayTemplateContent = await fs.readFile(
101+
path.join(directoryPath, 'template.js'),
102+
'utf8',
103+
);
97104
await fs.writeFile(dayFilePath, dayTemplateContent);
98-
console.log(logSymbols.success, `Created day file (\`./solutions/${yearInput}/${dayNumber}/${dayNumber}.js\`)`);
105+
console.log(
106+
logSymbols.success,
107+
`Created day file (\`./solutions/${yearInput}/${dayNumber}/${dayNumber}.js\`)`,
108+
);
99109
}
100110

101111
const inputFileExists = await fileExists(dayInputFilePath);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"test": "echo \"Error: no test specified\" && exit 1",
99
"lint": "eslint .",
10+
"format:check": "prettier . --check",
1011
"dev": "nodemon -e js,txt index.js"
1112
},
1213
"repository": {

prettier.config.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @type {import("prettier").Config} */
2+
export default {
3+
arrowParens: 'avoid',
4+
bracketSpacing: false,
5+
semi: true,
6+
singleQuote: true,
7+
quoteProps: 'as-needed',
8+
trailingComma: 'all',
9+
printWidth: 80,
10+
useTabs: true,
11+
overrides: [
12+
{
13+
files: '*.yaml',
14+
options: {
15+
useTabs: false,
16+
tabWidth: 2,
17+
},
18+
},
19+
],
20+
plugins: ['@ianvs/prettier-plugin-sort-imports'],
21+
};

solutions/2021/01/01.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {readInput, sum} from '../../../util.js';
55
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
66
const input = await readInput(directoryPath);
77

8-
const measurements = input
9-
.trim()
10-
.split('\n')
11-
.map(Number);
8+
const measurements = input.trim().split('\n').map(Number);
129

1310
let measurementIncreases = 0;
1411

@@ -20,7 +17,10 @@ for (const [index, measurement] of measurements.entries()) {
2017
}
2118
}
2219

23-
console.log('Measurements larger than the previous measurement:', measurementIncreases);
20+
console.log(
21+
'Measurements larger than the previous measurement:',
22+
measurementIncreases,
23+
);
2424

2525
let slidingMeasurementIncreases = 0;
2626

@@ -35,4 +35,7 @@ for (let index = 1; index < measurements.length; index++) {
3535
}
3636
}
3737

38-
console.log('Sliding measurements larger than previous measurement:', slidingMeasurementIncreases);
38+
console.log(
39+
'Sliding measurements larger than previous measurement:',
40+
slidingMeasurementIncreases,
41+
);

solutions/2022/02/02.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ function calculateScoreRoundOne(round) {
4141
return outcomeScore + shapeScore;
4242
}
4343

44-
const scoreOne = sum(fileContent.split('\n')
45-
.filter(line => line !== '')
46-
.map(round => calculateScoreRoundOne(round)));
44+
const scoreOne = sum(
45+
fileContent
46+
.split('\n')
47+
.filter(line => line !== '')
48+
.map(round => calculateScoreRoundOne(round)),
49+
);
4750
console.log('Score round 1:', scoreOne);
4851

4952
const outcomeDifference = {
@@ -67,7 +70,10 @@ function calculateScoreRoundTwo(round) {
6770
return outcomeScore + shapeScore;
6871
}
6972

70-
const scoreTwo = sum(fileContent.split('\n')
71-
.filter(line => line !== '')
72-
.map(round => calculateScoreRoundTwo(round)));
73+
const scoreTwo = sum(
74+
fileContent
75+
.split('\n')
76+
.filter(line => line !== '')
77+
.map(round => calculateScoreRoundTwo(round)),
78+
);
7379
console.log('Score round 2:', scoreTwo);

solutions/2022/03/03.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ function convertCharToPriority(char) {
1414
}
1515

1616
const priorities = rucksackContents.map(rucksackContent => {
17-
const firstHalf = rucksackContent.slice(0, rucksackContent.length / 2).split('');
18-
const secondHalf = rucksackContent.slice(rucksackContent.length / 2, rucksackContent.length).split('');
19-
const commonItemType = firstHalf.find(itemType => secondHalf.includes(itemType));
17+
const firstHalf = rucksackContent
18+
.slice(0, rucksackContent.length / 2)
19+
.split('');
20+
const secondHalf = rucksackContent
21+
.slice(rucksackContent.length / 2, rucksackContent.length)
22+
.split('');
23+
const commonItemType = firstHalf.find(itemType =>
24+
secondHalf.includes(itemType),
25+
);
2026
const itemPriority = convertCharToPriority(commonItemType);
2127
return itemPriority;
2228
});

solutions/2022/04/04.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ const fileContent = await readInput(directoryPath);
88
const pairs = fileContent
99
.split('\n')
1010
.filter(line => line !== '')
11-
.map(pair => pair.split(',').map(sections => sections.split('-').map(Number)));
12-
const fullyContainingAssignmentPairs = pairs.filter(pair => (
13-
(pair[0][0] >= pair[1][0] && pair[0][1] <= pair[1][1])
14-
|| (pair[0][0] <= pair[1][0] && pair[0][1] >= pair[1][1])
15-
)).length;
11+
.map(pair =>
12+
pair.split(',').map(sections => sections.split('-').map(Number)),
13+
);
14+
const fullyContainingAssignmentPairs = pairs.filter(
15+
pair =>
16+
(pair[0][0] >= pair[1][0] && pair[0][1] <= pair[1][1]) ||
17+
(pair[0][0] <= pair[1][0] && pair[0][1] >= pair[1][1]),
18+
).length;
1619

17-
console.log('Fully containing assignment pairs:', fullyContainingAssignmentPairs);
20+
console.log(
21+
'Fully containing assignment pairs:',
22+
fullyContainingAssignmentPairs,
23+
);
1824

1925
function rangeToArray(range) {
2026
const array = [];
@@ -29,6 +35,12 @@ function rangeToArray(range) {
2935
const partlyContainingAssignmentPairs = pairs.filter(pair => {
3036
const firstRange = rangeToArray(pair[0]);
3137
const secondRange = rangeToArray(pair[1]);
32-
return firstRange.find(item => secondRange.includes(item)) || secondRange.find(item => firstRange.includes(item));
38+
return (
39+
firstRange.find(item => secondRange.includes(item)) ||
40+
secondRange.find(item => firstRange.includes(item))
41+
);
3342
}).length;
34-
console.log('Partly containing assignment pairs:', partlyContainingAssignmentPairs);
43+
console.log(
44+
'Partly containing assignment pairs:',
45+
partlyContainingAssignmentPairs,
46+
);

solutions/2022/05/05.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ for (const rearrangement of rearrangements) {
3737
}
3838
}
3939

40-
const rearrangedTopCrates = Object.values(arrangedStacks).map(stack => stack.at(-1)).join('');
40+
const rearrangedTopCrates = Object.values(arrangedStacks)
41+
.map(stack => stack.at(-1))
42+
.join('');
4143
console.log('Rearranged top crates:', rearrangedTopCrates);
4244

4345
const multipleArrangedStacks = {};
@@ -47,7 +49,8 @@ for (const stack of stacks.slice(1)) {
4749
if (stack[i] === ' ') continue;
4850

4951
// Create array if not set already
50-
if (multipleArrangedStacks[counter] === undefined) multipleArrangedStacks[counter] = [];
52+
if (multipleArrangedStacks[counter] === undefined)
53+
multipleArrangedStacks[counter] = [];
5154

5255
multipleArrangedStacks[counter].push(stack[i]);
5356
}
@@ -58,10 +61,18 @@ for (const rearrangement of rearrangements) {
5861
const from = rearrangement.split('from ')[1].split(' ')[0];
5962
const to = rearrangement.split('to ')[1].split(' ')[0];
6063

61-
const movingElements = multipleArrangedStacks[from].slice(multipleArrangedStacks[from].length - amount, multipleArrangedStacks[from].length);
64+
const movingElements = multipleArrangedStacks[from].slice(
65+
multipleArrangedStacks[from].length - amount,
66+
multipleArrangedStacks[from].length,
67+
);
6268
multipleArrangedStacks[to].push(...movingElements);
63-
multipleArrangedStacks[from].splice(multipleArrangedStacks[from].length - amount, multipleArrangedStacks[from].length);
69+
multipleArrangedStacks[from].splice(
70+
multipleArrangedStacks[from].length - amount,
71+
multipleArrangedStacks[from].length,
72+
);
6473
}
6574

66-
const multipleRearrangedTopCrates = Object.values(multipleArrangedStacks).map(stack => stack.at(-1)).join('');
75+
const multipleRearrangedTopCrates = Object.values(multipleArrangedStacks)
76+
.map(stack => stack.at(-1))
77+
.join('');
6778
console.log('Multiple rearranged top crates:', multipleRearrangedTopCrates);

solutions/2022/06/06.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ function findDistinctCharactersPosition(numberOfDistinctCharacters) {
1414
const currentCharacter = input[index];
1515

1616
if (distinctCharacters.includes(currentCharacter)) {
17-
distinctCharacters = distinctCharacters.slice(distinctCharacters.indexOf(currentCharacter) + 1);
17+
distinctCharacters = distinctCharacters.slice(
18+
distinctCharacters.indexOf(currentCharacter) + 1,
19+
);
1820
}
1921

2022
distinctCharacters.push(currentCharacter);

solutions/2022/07/07.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import {readInput, sum} from '../../../util.js';
66
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
77
const input = await readInput(directoryPath);
88

9-
const lines = input
10-
.split('\n')
11-
.filter(line => line !== '');
9+
const lines = input.split('\n').filter(line => line !== '');
1210

1311
function updateDeep(currentObject, line, currentDirectoryPath) {
1412
if (currentDirectoryPath.length === 1) {
@@ -78,14 +76,21 @@ function sumDirectory(directory, parent) {
7876
}
7977

8078
const usedSpace = sumDirectory(fileSystem['/'], '/');
81-
console.log('Sum of directories below 100k size:', sum(directoriesMost100k.map(directory => directory.size)));
79+
console.log(
80+
'Sum of directories below 100k size:',
81+
sum(directoriesMost100k.map(directory => directory.size)),
82+
);
8283

8384
const totalSpace = 70_000_000;
8485
const minimumAvailableSpace = 30_000_000;
8586
const availableSpace = totalSpace - usedSpace;
8687
const neededSpace = minimumAvailableSpace - availableSpace;
87-
const sortedSummedDirectories = directoriesWithSummedSizes.sort((a, b) => b.size - a.size);
88+
const sortedSummedDirectories = directoriesWithSummedSizes.sort(
89+
(a, b) => b.size - a.size,
90+
);
8891

8992
// Smallest directory with needed space
90-
const directoryToDelete = sortedSummedDirectories.reverse().find(directory => directory.size >= neededSpace);
93+
const directoryToDelete = sortedSummedDirectories
94+
.reverse()
95+
.find(directory => directory.size >= neededSpace);
9196
console.log('Directory to delete:', directoryToDelete);

solutions/2022/08/08.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const treeGrid = input
1212
.map(line => [...line]);
1313

1414
// Set initial to all trees at the edge
15-
let visibleTrees = (treeGrid.length * 2) + ((treeGrid[0].length * 2) - 4);
15+
let visibleTrees = treeGrid.length * 2 + (treeGrid[0].length * 2 - 4);
1616

1717
function isVisibleOnLeftSide(treeHeight, xPosition, yPosition) {
1818
for (let x = xPosition - 1; x >= 0; x--) {
@@ -49,10 +49,11 @@ function isVisibleOnBottom(treeHeight, xPosition, yPosition) {
4949
for (let i = 1; i < treeGrid.length - 1; i++) {
5050
for (let j = 1; j < treeGrid[i].length - 1; j++) {
5151
const currentTree = Number(treeGrid[i][j]);
52-
const isCurrentTreeVisible = isVisibleOnLeftSide(currentTree, j, i)
53-
|| isVisibleOnRightSide(currentTree, j, i)
54-
|| isVisibleOnTop(currentTree, j, i)
55-
|| isVisibleOnBottom(currentTree, j, i);
52+
const isCurrentTreeVisible =
53+
isVisibleOnLeftSide(currentTree, j, i) ||
54+
isVisibleOnRightSide(currentTree, j, i) ||
55+
isVisibleOnTop(currentTree, j, i) ||
56+
isVisibleOnBottom(currentTree, j, i);
5657

5758
if (isCurrentTreeVisible) visibleTrees++;
5859
}
@@ -114,14 +115,17 @@ for (let i = 1; i < treeGrid.length - 1; i++) {
114115
for (let j = 1; j < treeGrid[i].length - 1; j++) {
115116
const currentTree = Number(treeGrid[i][j]);
116117

117-
const scenicScore = findLeftScenicScore(currentTree, j, i)
118-
* findRightScenicScore(currentTree, j, i)
119-
* findTopScenicScore(currentTree, j, i)
120-
* findBottomScenicScore(currentTree, j, i);
118+
const scenicScore =
119+
findLeftScenicScore(currentTree, j, i) *
120+
findRightScenicScore(currentTree, j, i) *
121+
findTopScenicScore(currentTree, j, i) *
122+
findBottomScenicScore(currentTree, j, i);
121123

122124
scenicScores.push({currentTree, scenicScore});
123125
}
124126
}
125127

126-
const sortedScenicScores = scenicScores.sort((a, b) => b.scenicScore - a.scenicScore);
128+
const sortedScenicScores = scenicScores.sort(
129+
(a, b) => b.scenicScore - a.scenicScore,
130+
);
127131
console.log('Highest scenic score:', sortedScenicScores[0]);

solutions/2022/09/09.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ function simulateMovements() {
110110

111111
simulateMovements();
112112

113-
console.log('Distinct positions tail visited:', tailVisitedPositions.at(-1).size);
113+
console.log(
114+
'Distinct positions tail visited:',
115+
tailVisitedPositions.at(-1).size,
116+
);
114117

115118
// Reset tails and visited positions
116119
head.x = 0;
@@ -126,4 +129,7 @@ for (let index = 0; index < 9; index++) {
126129

127130
simulateMovements();
128131

129-
console.log('Distinct positions tail #9 visited:', tailVisitedPositions.at(-1).size);
132+
console.log(
133+
'Distinct positions tail #9 visited:',
134+
tailVisitedPositions.at(-1).size,
135+
);

0 commit comments

Comments
 (0)