Skip to content

Commit a8e70c5

Browse files
committed
refactor: avoid Array function
prefer `Array.from()` over `Array()`
1 parent b84eef0 commit a8e70c5

7 files changed

Lines changed: 9 additions & 9 deletions

File tree

2016/day/5/part/2/solve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import md5 from "@lib/md5.ts";
22

33
export default function solve(input: string) {
4-
const password = Array(8).fill("_");
4+
const password = Array.from({ length: 8 }, () => "_");
55
let integer = 0;
66
while (password.some((char) => char === "_")) {
77
let hash;

2018/day/3/part/1/solve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function parseClaims(text: string): Map<number, Rect> {
2828
function transformClaimsToClaimSquares(claims: Map<number, Rect>) {
2929
const squares: ClaimSquare[] = Array.from(
3030
{ length: 1000 },
31-
() => Array(1000),
31+
() => Array.from({ length: 1000 }),
3232
);
3333
for (const [id, { left, right, top, bottom }] of claims) {
3434
for (let x = left; x < right; x++) {

2018/day/3/part/2/solve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function parseClaims(text: string): Map<number, Rect> {
2828
function transformClaimsToClaimSquares(claims: Map<number, Rect>) {
2929
const squares: ClaimSquare[] = Array.from(
3030
{ length: 1000 },
31-
() => Array(1000),
31+
() => Array.from({ length: 1000 }),
3232
);
3333
for (const [id, { left, right, top, bottom }] of claims) {
3434
for (let x = left; x < right; x++) {

2018/day/4/part/1/solve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function parseSleepHistogramsByGuardId(text: string) {
1515
case "begins shift":
1616
guardId = Number(message.match(/\d+/));
1717
if (result.has(guardId)) break;
18-
result.set(guardId, Array(60).fill(0));
18+
result.set(guardId, Array.from({ length: 60 }, () => 0));
1919
break;
2020
case "falls asleep":
2121
minuteFellAsleep = Number(timeText.substring(15, 17));

2018/day/4/part/2/solve.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function parseSleepHistogramsByGuardId(text: string) {
1515
case "begins shift":
1616
guardId = Number(message.match(/\d+/));
1717
if (result.has(guardId)) break;
18-
result.set(guardId, Array(60).fill(0));
18+
result.set(guardId, Array.from({ length: 60 }, () => 0));
1919
break;
2020
case "falls asleep":
2121
minuteFellAsleep = Number(timeText.substring(15, 17));

2021/day/15/part/2/solve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export default function solve(input: string) {
22
const levels = (() => {
33
const levels = input.split("\n").map((line) => Array.from(line, Number));
4-
return [...Array(5).keys()].flatMap((m) =>
4+
return [...Array.from({ length: 5 }).keys()].flatMap((m) =>
55
levels.map((row) =>
6-
[...Array(5).keys()].flatMap((n) =>
6+
[...Array.from({ length: 5 }).keys()].flatMap((n) =>
77
row.map((level) => (level + m + n - 1) % 9 + 1)
88
)
99
)

2023/day/12/part/2/solve.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export default function solve(input: string) {
2828
return { conditions, counts };
2929
})
3030
.map(({ conditions, counts }) => ({
31-
conditions: Array(5).fill(conditions).join("?"),
32-
counts: Array(5).fill(counts).flat(),
31+
conditions: Array.from({ length: 5 }, () => conditions).join("?"),
32+
counts: Array.from({ length: 5 }, () => counts).flat(),
3333
}))
3434
.map(({ conditions, counts }) => countArrangements(conditions, counts))
3535
.reduce((sum, count) => sum + count, 0);

0 commit comments

Comments
 (0)